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.

The first time I learnt LaTeX, I thought \newcommand properly fits for inline mode and \newenvironment for displayed mode.

Later I noticed it is not correct. So the question is

What is the rule of thumb when deciding whether we should use one over the other one?

share|improve this question
add comment

4 Answers

up vote 13 down vote accepted

Technical reasons for choosing an environment over a command form are that if the definition only involves setting up declarations then it never has to scan the content in advance, so list environments or longtable etc can start typesetting and shipping out pages before the end of the environment is seen. A command form necessarily has to scan to the end of the argument. If there is the possibility of verbatim-like material it is again easier to use an environment form.

Other reasons are mostly just to do with the aesthetics of the document structure you are defining. If you want to think of it as a scoped region define it as an environment. If you want to think of it as a block of text passed to a command, define it as a command.

share|improve this answer
add comment

For me the decision is about what the \macro or \begin{environment} should perform.

If I need to do something, I choose a macro.
If I need to provide something, only accessible in a certain region, I choose an environment.

So basically an environment provides a starting point for doing something generic with the content.
Whereas a macro performs a single action.

A not too strange thing would be to provide a command which resets the page counter.

\newcommand\resetpage{\setcounter{page}{1}}

However, one could argue that this could be the same as an environment with content that starts at page 1. However, the sheer size of the content of the environment does not seem so easily to comprehend in any editor. (it could easily extend for several pages). As well as the content formatting doesn't really change, and no macro's are provided to do something special, hence it would seem dubious to use an environment in this case. As @DavidCarlisle, states the length is not an issue if the environment provides things which are necessary to format the content (table, itemize, etc.).

share|improve this answer
1  
Ah interesting basically I said long stuff should be in an environment and you said long stuff should use a macro, but (to take your example) I was comparing \begin{reset}....\end{reset} with \resetpage{....} with the environment body changed to an argument. You are comparing to a no-argument form \resetpage in which the scope is implicitly the current environment or group. –  David Carlisle Mar 12 '13 at 16:49
    
@DavidCarlisle, my idea was that here it was clear that the \resetpage did something, whereas the environment would provide the same thing. In this case where the provided thing could be performed very explicitly by a do command, I would prefer this. Of course when changing the full layout of the residing text in the environment, (tables), an environment is obviously the best thing. –  zeroth Mar 12 '13 at 16:58
    
@DavidCarlisle, please see my update, whether that was a more fair statement? :) –  zeroth Mar 12 '13 at 17:02
    
sorry I did not meant to criticise it was just an observation that a reader may think we had come to opposite conclusions but actually we were talking about different aspects. (I didn't mention it but I'd given you a +1 :-) –  David Carlisle Mar 12 '13 at 17:10
    
@DavidCarlisle, I didn't take it as criticism :). Merely I could see your point, and that my explanation only had certain validation points, I tried to elaborate on those in the edit. It would be awful to have longtable as longtable#1. :) –  zeroth Mar 12 '13 at 17:13
add comment

nobody would use an environment for a defined abbreviation:

\newcommand\tim{That is me}

An environment foo is internally defined by two commands:

\begingroup
\foo
...
\endfoo
\endgroup

the reason why there is in general no big difference between a \newcommand and a \newenvironment. Using an environment is often the better choice for a markup language.

share|improve this answer
add comment

Just to add a bit to the other answers. Perhaps the most important consideration to ask yourself is for whom you are writing this command or environment? Both commands and environments hide the complexity of the TeX language from the user and should provide markup that should optimize for minimizing human1 memory and maximizing readability. So if there are no technical reasons is a matter of user interface.

Consider a simple example where the user is to provide a list of symbols and we will provide a command or environment to typeset them with the first one that comes in mind being a tabular.

enter image description here

For myself I would prefer this:

\printlist{\alpha,\beta,\gamma,\zeta,
           \eta,\theta,\epsilon}

If I would have expected the user to type fifty or more symbols an environment might lead to less human errors and improve readability.

\begin{symbols}
  \dosymbol{\alpha} 
  \dosymbol{\beta} 
  \dosymbol{\gamma} 
  \dosymbol{\zeta} 
\end{symbols}

Maybe? Take your pick using the MWE below:

\documentclass{book}
\usepackage{textcomp}
\makeatletter
%\def\symbols{\flushleft} %alternative way to define
%\def\endsymbols{\endflushleft} %the environment

\newenvironment{symbols}{\flushleft}{\endflushleft}
\def\dosymbol#1{%
   \leavevmode\hbox to .33\textwidth{%
    \hbox to 1.2em%
    {\hss$#1$\hfil}%
   \footnotesize\texttt{\string#1}\hss}%
   \penalty10}

\parindent0pt
\begin{document}

\begin{symbols}
  \dosymbol{\alpha} 
  \dosymbol{\beta} 
  \dosymbol{\gamma} 
  \dosymbol{\zeta} 
\end{symbols}

\def\listsymbols#1{%
\@for\next:=#1\do{%
    \expandafter\dosymbol\next%
}}
\long\def\printlist#1{%
  \def\alist{#1}
  \flushleft
  \listsymbols\alist
 \endflushleft
}

\printlist{\alpha,\beta,\gamma,\zeta,
           \eta,\theta,\epsilon}
\end{document}

1 Some TeX Programmers have also a human memory.

share|improve this answer
add comment

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.