7

Despite the fact that tex.SX is warning me that this seems to be a subjective question, it isn't. (I don't think it's a dup either, this was the closest I found.) I want to know precisely what happens if I do the following.

  1. Define an environment, or use one already provided. Call it thm.
  2. Recall that it's suppose to be used like

    \begin{thm}
    stuff
    \end{thm}
    
  3. Get lazy and do this instead:

    \thm{stuff}
    

I see that at the very least, the numbering works right but apparently I never leave the environment. But that is just my looking at the pdf; I don't really have any idea what is going on when I do this. Any specifics would be appreciated, thanks!

kcrisman
  • 415

2 Answers2

12

Nice question, with an answer that's not going to be short, even if a short answer to the question “can I use the macro form of an environment?” would be possible, quoting Marcel Marceau in Mel Brooks' “Silent Movie”: NON.

First of all, I recall that \newenvironment{foo}[<n>]{<start>}{<end>} defines the macros \foo and \endfoo, but just because it has to, as TeX only knows macros.

Such macros can be used in defining other environments, but should never appear in the body of a document. And when I say never, I mean it.

Therefore code such as

\newenvironment{foo}
  {a}
  {b}
\newenvironment{baz}
  {c\foo}
  {d\endfoo}

is legal and even slightly better than \newenvironment{baz}{c\begin{foo}}{d\end{foo}}, because it saves a level of grouping and will give a better error message in case \end{baz} is misplaced.

As a number of questions on the same subject shows, using \foo or \baz in a document can lead to very unexpected results. For instance,

\abstract{Text...}

will, with most document classes, leads to wrong typesetting of the whole document. See The abstract environment changes the \linewidth for the whole document that has several questions linked to it as duplicate.

Other cases are even more blatantly wrong: if you try

\align{
<eq 1> \\
<eq 2> \\
<eq 3>}

according to your attempt, you'll have a very nasty surprise.

The body of the environment is almost never an argument to a command and, even when it is, it is not an argument to the macro having the environment's name.

Your “lazy” example \thm{stuff} will setup things for a theorem: step the counter, typeset the label and change the font to italics. Such is the job of \thm. In this case { and } don't delimit an argument, but just form a group; not a big deal, because “stuff” will be typeset anyway. However, italics will continue.

The macros \begin and \end are not just placeholders: they do several actions. They open and close a group, so font or line width changes will be local (see the abstract example and the note about the font with \thm). They also set the current environment name, in order to help you in case nesting is improper. The \end part also adds some bookkeeping at the end, which most basic environments such as list hook at.

I'm the last one to say that being lazy is bad, but the above remarks should be enough to stop you trying being that lazy.

The converse, that is, using a macro as an environment, can work. Mostly it does, in some occasions it just seems to work; therefore I don't recommend doing it.

egreg
  • 1,121,712
  • Will there be different (better?) results if i group the begin-macro like this: {\abstract Text...}? – MaPePeR May 24 '16 at 07:47
  • @MaPePeR No: the \endabstract macro would not be executed, which does important things. Some envs might work with this trick, some won't (again, try align); you have to look into each one to see whether it works: why bother? – egreg May 24 '16 at 07:50
  • @MaPePeR Just use the tools like you are supposed to. Don't use an environment as a simple command, don't use a screwdriver as a hammer, don't use an extension cord to build a tent. – Johannes_B May 24 '16 at 07:50
  • My thought process was, that at least some of the changes done by the begin macro would only apply to the group and would automatically disabled again when the group ends. I would never do such thing. I was just curious. – MaPePeR May 24 '16 at 08:45
  • "I'm the last one to say that being lazy is bad, but the above remarks should be enough to stop you trying being that lazy." - nice! – kcrisman May 24 '16 at 14:14
7

Fundamentally \newenviroment{<env>}[<args>]{<begin>}{<end>} defines two macros: one associated with the start of the environment and one with the end of the environment, simplistically turning

\begin{<env>}[<args>]
...
\end{<env>}

into

\<env>[<args>]
...
\end<env>

However, using the environment form additionally provides a limited scope to any changes made within the environment in the form of a group. For example, consider

enter image description here

\documentclass{article}

\begin{document}

Lorem ipsum.

\begin{itemize}
  \item An item
  \item Another item
\end{itemize}

Lorem ipsum.

\itemize
  \item An item
  \item Another item

Lorem ipsum.

\end{document}

Note how the environment usage of itemize reverses the list style indentation after \end{itemize} while the "lazy" method (without an \end...) doesn't restore the regular layout. Even if you were to use the command-form \<env>[<args>] ... \end<env>, it's best to stick to the environment use.

Essentially it may boil down to the environment you use. For example, theorems tend to set their "QED" during \end<theorem>. Other environments require and \end<env> (like tabularx or listings or even just the verbatim environment).

Werner
  • 603,163