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.

What reason (other than perhaps being easier for my syntax highlighter to parse) is there to write

\begin{center}
Here is centered text
\end{center}

rather than:

\center
Here is centered text
\endcenter

And likewise for other environments. Does the \begin do any extra funky checks or are they effectively synonymous?

share|improve this question
2  
See the very related question Defining environments based on other ones: What's the right way? –  Martin Scharrer Aug 21 '11 at 21:39
add comment

4 Answers 4

up vote 17 down vote accepted

\begin and \end build a group, which can keep settings local. Simply writing \command ... \endcommand doesn't. Such a scope is very important for me, that's why I prefer the \begin ... \end syntax.

Example:

\documentclass[a4paper]{article}
\begin{document}
\begin{center}
\bfseries Text
\end{center}
text % this is normal font, left aligned as default
\center
\bfseries Text
\endcenter

text % this is still bold! and it's even centered
\end{document}
share|improve this answer
1  
If \endcenter doesn't end centering, what does it do? –  doncherry Aug 21 '11 at 21:43
    
@doncherry: It ends the triv-list used for centering \endcenter: macro:->\endtrivlist. The \trivlist apparently depends on the group added by \begin .. \end to revert the changes made. –  Martin Scharrer Aug 21 '11 at 21:48
    
Some environments actively support the "plain form", e.g. \pgfpicture .. \endpgfpicture, because they add a group by themselves. –  Martin Scharrer Aug 22 '11 at 8:33
add comment

From latex.ltx:

\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}
\def\end#1{%
  \csname end#1\endcsname\@checkend{#1}%
  \expandafter\endgroup\if@endpe\@doendpe\fi
  \if@ignore\@ignorefalse\ignorespaces\fi}

\begin defines (locally) \@currenvir and \@currenvline, starts a group and sets \@endpefalse; \end checks \@currenvir against its argument (throwing an error if they don't agree) and does other management tasks. It's not only the grouping. On the other hand, \environment and \endenvironment can be (almost always) safely used in the definition of another environment. There are a few topics on TeX.sx about this.

share|improve this answer
    
Regarding using \environment and \endenvironment in the definition of another environment: that's mostly true, though scoping could also be a issue here, producing undesired results in the end code. –  Stefan Kottwitz Aug 21 '11 at 21:37
4  
The \@currenvir macro \begin{env}...\end{env} will give an error when environments are improperly nested. That's worth making more explicit. –  Matthew Leingang Aug 21 '11 at 23:52
add comment

In addition to everything else: \endfoo need not be defined in order for the \end{foo} to work, see everybody who uses size commands as environments and gets away with it (to some extent).

share|improve this answer
add comment

Further to Stefan's answer, here is the definition of \begin and \end from latex.ltx, clearly showing the grouping (\begingroup and \endgroup):

\def\begin#1{%
  \@ifundefined{#1}% Environment begin
    {\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}% <- grouping starts
\def\end#1{% Environment end
  \csname end#1\endcsname\@checkend{#1}%
  \expandafter\endgroup\if@endpe\@doendpe\fi% <- grouping ends
  \if@ignore\@ignorefalse\ignorespaces\fi}

In contrast to that, defining a new environment merely executes (in some order, depending on optional parameter specification):

\def\newenvironment{\@star@or@long\new@environment}
\def\new@environment#1{%
  \@testopt{\@newenva#1}0}
\def\@newenva#1[#2]{%
   \kernel@ifnextchar [{\@newenvb#1[#2]}{\@newenv{#1}{[#2]}}}
\def\@newenvb#1[#2][#3]{\@newenv{#1}{[#2][{#3}]}}
\def\renewenvironment{\@star@or@long\renew@environment}
\def\renew@environment#1{%
  \@ifundefined{#1}%
     {\@latex@error{Environment #1 undefined}\@ehc
     }\relax
  \expandafter\let\csname#1\endcsname\relax
  \expandafter\let\csname end#1\endcsname\relax
  \new@environment{#1}}
\long\def\@newenv#1#2#3#4{%
  \@ifundefined{#1}%
    {\expandafter\let\csname#1\expandafter\endcsname
                         \csname end#1\endcsname}%
    \relax
  \expandafter\new@command
     \csname #1\endcsname#2{#3}% <- \<env>
     \l@ngrel@x\expandafter\def\csname end#1\endcsname{#4}}% <- \end<env>

From the definition of \@newenv (I've added some comments), \newenvironment{<env>}{<begin-env>}{<end-env>} assigns <begin-env> to \<env> and <end-env> to \end<env> without any sign of grouping.

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.