35

Why does LaTeX make a distinction between commands and environments?
Or also: why do we need environments?

I ask this because I am defining a number of custom commands (like \question{} and \answer{}) and I am not entirely sure whether to use the \newcommand-way or the \newenvironment-way (both work). The difference seems to be in supporting Tables and Figures and other 'complicated' stuff in my custom command.

(If I compare to HTML/CSS, all things are tags (or CSS classes or IDs), and they can be specified (or default to) to be inline or not. But apart from that, there is not a real difference among them. It seems to be that the underlying model of HTML/CSS formatting is simpler and therefor easier to understand and use.)

0

3 Answers 3

31

As Phillippe has already said, environments are usually best for this. To understand why, you need to follow how TeX works in reading arguments. For a command

\foo{<stuff>}

TeX reads all of <stuff> before expanding \foo. This can be an issue if <stuff> is very big. Also, once TeX has read an argument it can be difficult or impossible to change the category codes (these are important in TeX!). In particular, verbatim material is difficult to handle reliably in an arbitrary argument.

With environments, LaTeX does not read everything in one go. At \begin{foo} the set up is done, but the environment is then typeset normally without everything being read in one go. This makes it possible to include verbatim and avoids filling up TeX's memory with too much. I'd also point out that LaTeX makes each environment a group, which is why things like

\begin{center}
  \bfseries
  Text
\end{center}

keep the font changes local.

(The comparison to HTML/CSS is made a lot, which ignores the fact that the HTML model came long after the LaTeX one, and also the fact that the requirements for a typesetting model are different to those for on-screen display using HTML.)

2
  • {@Joseph Wright} Nice parenthetical addendum about the relative chronology of LaTeX and HTML/CSS. It should be more widely known.
    – Marcos
    Aug 4, 2016 at 20:12
  • @Joseph Wright actually the difference between HTML and LaTeX has nothing to do with with their chronology or the requirements of a typesetting model. The difference between HTML and LaTeX models is the way it works: HTML has a DOM tree, that is rendered from top to bottom. TeX on the other hand is a turing-complete language that does macro expansion e.g. that keeps replacing macros with ever smaller elements until it finally arrives at font letters. One is not better than the other. You can typeset with both tools, but they just work very very differently. It's not like HTML is more modern...
    – John Smith
    May 25, 2021 at 9:26
10

You should always prefer environments to commands for things like exercices, answers or theorems. What you can do with a command, you can always also do with an environment (using, for example, the environ package), but the converse is false. For example, you can use verbatim content inside most environments, but not inside a command.

6

You can think of an environment as made up of two commands a) the stuff that goes at the beginning and b) the stuff that goes at the end. This provides a convenient user interface. One such example is the \begin{enumerate}... \end{enumerate}. If you would have written a macro for this you would need to send all the items as parameters and it will not be very convenient for the user.

Your comparison with HTML/CSS is not applicable here. Using HTML every tag has a begin and end, you say <h1></h2>. The styling is then done using either inline css or an external sheet. In this case TeX/LaTeX does both in a unified language. LaTeX can also be programmed, whereas with HTML you will need JavaScript. It does take time to get used to TeX/LaTeX but the end result is very satisfying.

2
  • 2
    Is the typo <h1></h2> intentional? :-p Jan 6, 2011 at 12:21
  • @Bruno Yes! and you the first one to spot it:) Jan 6, 2011 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.