6

I run with an example today:

\documentclass{article}

\begin{document}
\def\EE{\section{EE}}

\begin{EE}
A
\end{EE}

\end{document}

this source can be compiled successfully using pdflatex, and generate the output:

enter image description here

According to the answer and my basic understanding of LaTeX, the fisrt section is generated by \EE, something like \begin{EE} there, but without starting group.

My question is: "why this source is compilable", for that the command \endEE hasn't been defined? Then i check the definitions of these 2 macros \begin and \end using latexdef, get the following:

 $ latexdef \begin

\begin:
macro:->\protect \begin


\begin :
macro:#1->\UseHook {env/#1/before}\@ifundefined {#1}{\def \reserved@a {\@latex@error {Environment #1 undefined}\@eha }}{\def \reserved@a {\def \@currenvir {#1}\edef \@currenvline {\on@line }\@execute@begin@hook {#1}\csname #1\endcsname }}\@ignorefalse \begingroup \@endpefalse \reserved@a

 $ latexdef \end

\end:
macro:->\romannumeral \ifx \protect \@typeset@protect \expandafter \expandafter \expandafter \expandafter \expandafter \expandafter \expandafter \z@ \else \expandafter \z@ \expandafter \protect \fi \end

I find \csname #1\endcsname in the definition of \begin, roughly consistent with my guess. There is nothing like \csname in the definition of \end, is this the reason that the above source compilable ? But from my previous experience, the environment \begin{Env}...\end{Env} can be write as \Env ... \endEnv, maybe within a group.

For the last \end macro in the definition of \end in LaTeX. I have check the meaning of \end in The TeXbook, found the following in "Chapter 6: RunningTEX":

TEX will now prompt you with ‘*’, because the file did not contain ‘\end’. Enter \end into the computer now ...

So there is nothing special about the macro \end in plain TeX. Then, what makes the above source compilable?

0

1 Answer 1

10

\end in latex is unrelated to the \end primitive described in the TeXBook.

It is essentially

\def\end#1{\csname end#1\endcsname\endgroup}

This means that \end{foo} will execute \endfoo if it is defined but no error is raised if it is not as \csname endfoo\endcsname acts like \relax if \endfoo is not defined.

Although with checking and hook code and other details the current definition is

\edef\end
  {\unexpanded{%
     \romannumeral
       \ifx\protect\@typeset@protect
       \expandafter       %1
         \expandafter        %2
       \expandafter       %1
           \expandafter         %3 expands the \csname inside \end<space>
       \expandafter       %1
         \expandafter        %2  expands \end<space>
       \expandafter       %1     expands the \else
           \z@
       \else
         \expandafter\z@\expandafter\protect
       \fi
   }%
   \expandafter\noexpand\csname end \endcsname
  }
\@namedef{end }#1{%
  \romannumeral
    \IfHookEmptyTF{env/#1/end}%
        {\expandafter\z@}%
        {\z@\UseHook{env/#1/end}}%
    %%%%%%%%%%%%%%%%%%%%%%%
%
    \csname end#1\endcsname
%
    %%%%%%%%%%%%%%%%%%%%%%%
    \@checkend{#1}%
    \expandafter\endgroup\if@endpe\@doendpe\fi
    \UseHook{env/#1/after}%
    \if@ignore\@ignorefalse\ignorespaces\fi
}
\def\@checkend#1{\def\reserved@a{#1}\ifx
      \reserved@a\@currenvir \else\@badend{#1}\fi}
2
  • 1
    @zpding latexdef is just giving the top level definition which is handling robust commands the actual code is in a lower level command. Commented Aug 15 at 14:15
  • 2
    More specifically, LaTeX's \begin corresponds more closely to the \beginthe that the TeX book suggests in exercise 5.7. Commented Aug 16 at 10:53

You must log in to answer this question.

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