wrap LaTeX command in environment

邮差的信 提交于 2020-03-17 07:53:07

问题


How can I wrap a LaTeX command in an environment? In essence, how can I turn \somecommand{contents} into \begin{somecommand} contents \end{somecommand}? I have tried the obvious in creating a new environment as such:

\newenvironment{somecommand}[0]{
  \somecommand{
}
{
  }
}

but this causes problems with the curly brackets. Let me give a more concrete example. Say that you want to create the environment very-important and you want to use the command emph to accomplish this. An straightforward (but incorrect) solution would be to write something as

\newenvironment{very-important}[0]{
  \emph{
}
{
  }
}

The problem here is that the command works with the information that is found inside the environment, so it is not one of the opening commands of the environment, nor is it a closing command of the environment. The question is then: how can you do this?


回答1:


It seems that now I can guess what is the question.

\newenvironment{very-important}{\startimportant}{}
\def\startimportant#1\end{\emph{#1}\end}

\begin{very-important}
Something
\end{very-important}

This solution works well. But IMHO it is bad idea to wrap all text in the environment. Why? There are two ways to do something with the text. For example, you want to change the font and use italic.

  • The first method. \textit{sentence written in italics}
  • The second method. {\it sentence written in italics\/}

What is the difference? The thing is that first method use the second one. \it macro changes the font and the brace } changes it back. \textit macro reads the full argument, changes the font and inserts the argument again: \textit is defined roughly as follows (not exactly).

\def\texit#1{{\it#1\/}}

The first method is always doing extra work. It reads the argument twice. Almost always, you can make changes and then you can everything back.

Eventually why do you use the environment? Use macros.

\veryimportant{
   Any thought
}



回答2:


This can be done with the environ package as follows:

\usepackage{environ}
...
\NewEnviron{very-important}{\emph{\BODY}}

\BODY contains the body of the environment, and environments may be nested. See the documentation for more details.




回答3:


A Simpler way could be:

\newenvironment{somecommand}[0]{ \somecommand\bgroup } { \egroup }

Explanation: \bgroup works like { and \egroup works like }.




回答4:


New environment somecommand defines the macro \somecommand. You can not use macro with the same name \somecommand inside.

Moreover you should write

\newenvironment{name}{openning command}{closing commands}

rather than

\newenvironment{somecommand}[0]{ \somecommand{ } { } }

You obviously have a problem with closing commands.




回答5:


define command

\newcommnad{eqn}{1}{\begin{equation}#1\end{equation}}

will change

\eqn{x^2=y}

to

\begin{equation}
  x^2=y
\end{equation}

I think



来源:https://stackoverflow.com/questions/1872951/wrap-latex-command-in-environment

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!