wrap LaTeX command in environment

后端 未结 5 1983
谎友^
谎友^ 2021-02-05 06:22

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 obvi

5条回答
  •  抹茶落季
    2021-02-05 06:35

    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
    }
    

提交回复
热议问题