32

Without any doubt there are fontsize switches in the form of control sequences (described here for instance.) But, from time to time I ran into code examples where environment versions of those were used, e.g. \begin{small}...\end{small}. After testing those successfully I began to incorporate them into my TeX vocabulary.

A few days ago @GonzaloMedina commented on one of my answers that those environments do not exists and constructions relying on them work by pure coincidence. Then I tried to find out more about those ominous environments. Ominous because they don't seem to be somehow documentated. (The only thing I found that tried to explain them in a somehow systematical manner was some Wikibooks resource.)

Are these environments real or not?

7
  • No, they aren't. The point is: You could use \begin{section}...\end{section}, but you shouldn't
    – user31729
    Aug 1, 2015 at 21:28
  • See my somewhat related question here: tex.stackexchange.com/questions/199692/…
    – user31729
    Aug 1, 2015 at 21:58
  • 4
    I'm glad you asked this question. As it turns out I was wrong :) Aug 1, 2015 at 22:10
  • 1
    What I personally don't like about these environments is not so much that they're not defined as ones but they're not semantic. Why would I mark up a part of my document as »large« when I actually mean »read this«? And the moment you define own environments for this you don't need those environments any (regardless if they exist or not)
    – cgnieder
    Aug 1, 2015 at 22:33
  • @GonzaloMedina, I think you are still right. Those environments did not get defined, neither expicitly nor implicitly. But, it's not harmfull to use them and recalling the fact that the end-definition of an environment uses \csname fully explains why they work without any problems. (So it's not just some miracle as you thought in the first place.^^) Still I wonder why it is so easy to use this syntax when it is considered bad practice.
    – Ruben
    Aug 1, 2015 at 22:36

3 Answers 3

21

You can use any LaTeX command defined by \newcommand and wrap a \begin{}...\end{} pair around it, however, it's not recommended, since this is not an environment.

The interesting thing is, however, that grouping works anyway, but this is consequence of \begin...\end.

There are no fontsize environments like \begin{small} etc, as there aren't \begin{chapter} etc.

See for example:

\documentclass{article}


\newcommand{\foo}{This does nothing}

\begin{document}
\begin{foo}

\end{foo}
\end{document}

Edit Some explanations

This code can be found in latex.ltx, see lines 4058f.

\def\begin#1{%
  \@ifundefined{#1}%
    {\def\reserved@a{\@latex@error{Environment #1 undefined}\@eha}}%
    {\def\reserved@a{\def\@currenvir{#1}%
     \edef\@currenvline{\on@line}%
     \csname #1\endcsname}}%
  \@ignorefalse
  \begingroup\@endpefalse\reserved@a}

It can be seen, that @ifundefined{foo} would be called, which is false, since it \foo is defined, so the environment name foo is set and \csname #1\endcsname (i.e. \foo) is called.

Now the \end{foo}: part

\def\end#1{%
  \csname end#1\endcsname\@checkend{#1}%
  \expandafter\endgroup\if@endpe\@doendpe\fi
  \if@ignore\@ignorefalse\ignorespaces\fi}

Environments look for the \end... code, here \endfoo, which isn't defined, but \csname endfoo\endcsname expands to a \relax and nothing bad happens.

It should be noted, that many of the well-known environments aren't defined with \newenvironment. It's sufficient to use a \foo command and \def\endfoo, see \endequation or \endenumerate etc. for example.

4
  • But doesn't \end{foo} issue \endfoo internally? (which isn't defined in your example.) How can this work?
    – Ruben
    Aug 1, 2015 at 21:35
  • 2
    @Ruben internally \csname endfoo\endcsname is called which gives \relax if the command is not defined. Aug 1, 2015 at 21:39
  • 5
    using commands as environments is really an intentional part of latex's design Aug 1, 2015 at 21:53
  • @DavidCarlisle: Yes, you answered that in my question tex.stackexchange.com/questions/199692/… (the one tick which brought you the 200k rep ;-))
    – user31729
    Aug 1, 2015 at 21:57
26

Any command may be used as an environment, and if surrounding large blocks of text it is often convenient, so

{\small  zzz\par}

and

\begin{small}%
zzzz

\end{small}

are more or less equivalent.

Note however that unlike \small, spaces after \begin{small} are not dropped due to normal TeX tokenization rules. Also you almost always need a \par or blank line before the \end which is also true in the command form, but easier to forget in the environment form as most custom environments such as center included an implicit \par in their end code, but that is not the case here.

See also the discussion in:

Are \end.... macro names reserved in LaTeX2e?

25

Actually, the “LaTeXbook” (properly “LaTeX. A Document Preparation System”, by Leslie Lamport) endorses the use of such environments: at the end of page 27 we find:

Every declaration has a corresponding environment of the same name (minus the \ character). Typing

\begin{em} ... \end{em}

is equivalent to typing {\em ... }.

In particular, the “environmental” form automatically supplies a group to keep the effects of the declaration local, as always happens with environments (\begin issues a \begingroup, and \end the corresponding \endgroup). Now, \small is a declaration, so the above applies.

This has been done purposedly, so I don’t deem it correct to say that these environments work “by chance”; indeed, as has already been observed, the lack of a definition of a command like \endsmall does not rise any problem, because it is called as \csname small\endcsname and is therefore equivalent to \relax if undefined.

So, in the end my answer is: “Yes, these environments are ‘real’” (whatever this means).


Addition

After seeing some of the comments, I thought that it might be useful to add some details about how environments are implemented in LaTeX2e (I’m not going to speak of LaTeX3 because I havan’t got enough expertize). Christian Hupfer has already presented the exact code excerpts from latex.ltx, but perhaps someone will find the following additional remarks useful as well.

When you define environment FOO, LaTeX simply defines two new commands, \FOO and \endFOO: the latter is always without arguments, while the former has the same argument(s), if any, that have been specified for the environment FOO. When a \begin{FOO} is encountered, the following things happen (among others):

  • a group is begun with \begingroup;

  • the argument of \begin (FOO, in this case) is saved (locally) in the macro \@currenvir, to be able to check, later on, that each \end is paired with the correct \begin;

  • the command \FOO is executed as the last thing; thus, it will absorb the arguments that follow \begin{fOO}, if they are present.

On the other hand, when LaTeX comes to \end{FOO}, the following happen (among other things):

  • \endFOO is executed, if it is defined (nothing happens if it is not, as already explained above);

  • LaTeX checks that the argument of \end is equal to \@currenvir and rises an error if not;

  • an \endgroup is issued to close the group.

Actually, when I define a new environment based on another, standard one, I prefer to avoid using explicit \begin and \end, so that the value of \@currenvir is not changed. For example, suppose I want to define a variant of the quote environment that italicizes its contents; I prefer, say,

\newenvironment{italquote}{%
  \quote
  \itshape
}{\endquote}

over

\newenvironment{italquote}{%
  \begin{quote}%
  \itshape
}{\end{quote}}

especially if I am not going to use the environment myself. In this way, if users misspell the name of the environment in the \end statement, they will get an error about an incorrectly terminated italquote environment, which is what they actually used, not about an incorrectly terminated quote environment, of which they could be unaware.

6
  • I would neither say that those 'environment' are by chance or are real. It's a design of the LaTeX format rather, in my point of view
    – user31729
    Aug 1, 2015 at 22:23
  • @ChristianHupfer: Well, of course I was talking flippantly. (:-)
    – GuM
    Aug 1, 2015 at 22:26
  • +1 for the Lamport reference. By "real" I meant "exisiting" and by "existing" I mean "defined" (in this case defined via \newenvironment as my question is about environments). Interesting that the programs design provides this two structures (commands and environments) and they are interchangable in that way. Without knowing the internal procedures of those structures one would not think -- at least I didn't before asking that question -- that they are somehow two sides of the same coin.
    – Ruben
    Aug 2, 2015 at 10:50
  • 2
    I beg to disagree with Leslie Lamport: if you do \begin{em}<newline>text<newline>\end{em} you get several unwanted spaces.
    – egreg
    Aug 2, 2015 at 21:27
  • 1
    @egreg: Oh, yes, of course: that has already been remarked by David Carlisle. But surely Lamport meant that the em “environment” should be used on a single input line (e.g., some \begin{em}truly important (no kidding!)\end{em} remark).
    – GuM
    Aug 2, 2015 at 23:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .