Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.)

share|improve this question

3 Answers 3

up vote 20 down vote accepted

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.)

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer
2  
Is the typo <h1></h2> intentional? :-p –  Bruno Le Floch Jan 6 '11 at 12:21
    
@Bruno Yes! and you the first one to spot it:) –  Yiannis Lazarides Jan 6 '11 at 12:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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