# Detect \begin{...} ... \end{...} mismatch, or ending what you did not begin

While testing some of my new environments I made a mistake like this:

...
\begin{document}
...
\begin{A}
...
\end{A}
...
\begin{B}
...
\end{A}
...
\end{document}


LaTeX complained that B did not end when document ended. And it talks about the line where \end{document} is, and the line where \begin{B} was, but it did not talk about the mismatching \end{A} (the location of the actual error).

Why can't LaTeX detect such? I know that \newenvironment actually defines two commands, one for beginning and one for ending, but isn't it possible to detect the case where something is to be ended that never had begun?

Update 1:

The example I had given was (to my best knowledge) the essence of the problem, and I contructed it in the head as the original file has thousands of lines and many figures, etc. (too much to present here, and also written by a TeX analphabet, so you will tear your hair out to see my struggles...). But people do not believe what I wrote. So I made a small test file from my big file, showing the problem:

Here's the essential part of the input:

\begin{gfxTableCap}[p]{lr}{Caption}{label2}%
\gfxCap{0.4}{Platzhalter.pdf}{Caption 2A}
&
\gfxCap{0.4}{Platzhalter.pdf}{Caption 2B}
\end{gfxTableCapLab}


Causing this output (truncated to the essence):

This is pdfTeX, Version 3.14159265-2.6-1.40.18 (TeX Live 2017/TeX Live for SUSE Linux) (preloaded format=pdflatex)
restricted \write18 enabled.
entering extended mode
(./xxx.tex
LaTeX2e <2017-04-15>


[...]

<Platzhalter.pdf, id=1, 284.52756pt x 227.62204pt> <use Platzhalter.pdf>
<use Platzhalter.pdf> <use Platzhalter.pdf> <use Platzhalter.pdf>

! LaTeX Error: \begin{gfxTableCap} on input line 88 ended by \end{document}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.


So it does not say "unexpected \end{gfxTableCapLab}.

In case it's special for the environments I had defined, here's the mess:

\documentclass[a4paper,twoside]{report}
\newcommand{\gtLab}{}
\newcommand{\gtCap}{}
\newenvironment{gfxTableCapLab}[4][htbp]{%
\begin{figure}[#1]%
\begin{tabular}{#2}}%
{\end{tabular}%
\gtCap%
\end{figure}%
}
\newenvironment{gfxTableCap}[3][htbp]{%
\begin{gfxTableCapLab}{#2}{#3}{}%
}%
{\end{gfxTableCapLab}}
\newenvironment{gfxTableLab}[3][htbp]{%
\begin{gfxTableCapLab}{#2}{}{#3}%
}%
{\end{gfxTableCapLab}}
\newenvironment{gfxTable}[2][htbp]{%
\begin{gfxTableCapLab}{#2}{}{}%
}%
{\end{gfxTableCapLab}}
\newcommand{\gLab}{}
\newcommand{\gCap}{}
\newcommand{\gfxCapLab}[4]{%
\begin{minipage}[t]{#1\textwidth}%
{graphic}%
\gCap%
\end{minipage}
}
\newcommand{\gfxCap}[3]{\gfxCapLab{#1}{#2}{#3}{}}
\begin{document}
Some text...
\begin{gfxTableCapLab}[p]{lr}{Caption}{label1}%
\end{gfxTableCapLab}
%
Text
\begin{gfxTableCap}[p]{lr}{Caption}{label2}%
\end{gfxTableCapLab}

\end{document}


For all who do not have a nice placeholder PDF, here's the uuencoded version (I can't attach a PDF as image):

begin 644 Platzhalter.pdf
M)5!$1BTQ+C4*);7MKOL*-"P(&]B:@H\/"O3&5N9W1H(#4@,"!2"B@("]& M:6QT97(@+T9L871E1&5C;V1E"CX^"G-T<F5A;0IXG)5036L"00R]YU?DJ(>- MD\Q7]EHH!4&H[=S$PU*K4G8/6J'07V]VEQ9I*UC"9,)[Y.4EC,ZB8DLBB7+F
M%5?.CB&ZBG!YPU#G?OP!0X>_'X8=3<WANL4$A]2"%]%VOKV4#!1Z0!X4Q MFZ0H9<G8?UQCAQ(#:8Y?0&M).1:Z4@J4>4?-+8/]+Y1F7UWW<X.'GR'_K M71K\8ZE>;SSE<8=W!;PGHX>KCV5RI-'5/B-+I!!4.6'I8+:M7-6/*EM831[; MYO2Y;]IIY2>GU^-T7>9P7V)9VT07*(*96YD<W1R96%M"F5N9&]B:@HU(#@ M;V)J"B@(#(Q.0IE;F1O8FH*,RP(&]B:@H\/H@("O17AT1U-T871E(#P\ M"B@("@("]A,"\/"O0T$@,2O8V$@,2^/@H@("^/@H@("O1F]N="\ M/H@("@("O9BTP+3@-BP(%(*("@/CX*/CX*96YD;V)J"C(@,"!O8FH* M/#P@+U1Y<&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-A=&%L;V<*("@+U!A9V5S(#$@,"!2 M"CX^"F5N9&]B:@IX<F5F"C@,30*,#P,#P,#P,"V-34S-2!F(HP,#P M,#U.#V(#P,#P(&X@"CP,#P,#T-#(@,#P,#@;B*,#P,#P,#,S M,RP,#P,"!N(HP,#P,#P,#$U(#P,#P(&X@"CP,#P,#S,3$@,#P M,#@;B*,#P,#P-3,Y,2P,#P,"!N(HP,#P,#P-C<T(#P,#P(&X@ M"CP,#P,#0W,S$@,#P,#@;B*,#P,#P-#<U-"P,#P,"!N(HP,#P
M,#U,#DR(#P,#P(&X@"CP,#P,#4Q,34@,#P,#@;B*,#P,#P-3@W
M,2P,#P,"!N(HP,#P,#U.3@X(#P,#P(&X@"G1R86EL97(*/#P@+U-I
M>F4@,30*("@+U)O;W0@,3,@,"!2"B@("]);F9O(#\$R(#@4@H^/@IS=&%R
1='AR968*-CT,0HE)45/1@H

end

• LaTeX detects it. Exactly when the document ended and B was never finished. How is it supposed to detect it earlier? It does not have a human brain. Mar 28 '19 at 7:30
• It is not that people don't (want to) believe what you write. It's the fact that in some (many?) situations the issue you describe does not occur (as demonstrated in the answer), so your situation must be special and people need to see code that actually reproduces the problem to be able to help. Mar 29 '19 at 4:53
• Note for example that if I complete the code at the beginning of the question to a working example with \documentclass{article} \newenvironment{A}{A}{A} \newenvironment{B}{B}{B} \begin{document} \begin{A} \end{A} \begin{B} \end{A} \end{document} TeX says ! LaTeX Error: \begin{B} on input line 11 ended by \end{A}. on l.13 and thus gives a good error message. But if I say \newenvironment{B}{\begin{A}}{\end{A}} instead, I get ! LaTeX Error: \begin{B} on input line 11 ended by \end{document}.. This is what you have in your second example code. Mar 29 '19 at 4:56
• the new code produces a different error ! Package pdftex.def Error: File """Platzhalter".pdf' not found: using draft setting really the idea is you make a minimal example that produces the error that you are asking about, but I'll fix that this time. Mar 29 '19 at 7:41
• @U.Windl I already fixed your example not to require a figure which was not necessary to demonstrate the error that you were asking about. If you have a question that does need a figure, you can use example-image which is in tex distributions for such tests but here I simply deleted the \includegraphics Mar 30 '19 at 23:02

Please always post actual test documents not untestable fragments. In general latex would detect that error at the

\end{A}


but there are cases when it can not, for example if B was verbatim then by definition all processing is disabled until \end{B} is seen, in which case that error would only be detected at end of file.

so

\documentclass{article}

\begin{document}

\begin{empty}

\end{empty}

\end{empty}

\end{document}


produces an error message on line 11

! LaTeX Error: \begin{quad} on input line 9 ended by \end{empty}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
...

l.11 \end{empty}


but

\documentclass{article}

\begin{document}

\begin{empty}

\end{empty}

\begin{verbatim}

\end{empty}

\end{document}


Produces an error at end of file

Runaway argument?
^^M^^M\end{empty}^^M^^M^^M\end{document}^^M
! File ended while scanning use of \@xverbatim.
<inserted text>
\par
<*> bb469

?


looking at the specific example provided, note that tex is a macro expansion system not a compiler it never parses the full document in advance.

so starting from

\begin{gfxTableCap}[p]{lr}{Caption}{label2}%
\end{gfxTableCapLab}
\end{document}


after the first few expansions of \gfxTableCap you have

\begin{gfxTableCapLab}{p}{lr}{Caption}%
\end{gfxTableCapLab}
\end{document}


so the explicit \end{gfxTableCapLab} in your document closed the inner environment, but the outer gfxTableCap is never closed and that can not be detected until the closing of a containing group which is \end{document}` here.

• Probably more a comment than an answer. As for the test document: I typed it from memory as I don't have the document here, and the document actually is way to large to present here. I'm trying to extract a test case (hopefully) to demonstrate the problem. What I can say for now is that my environment messes with floats. Mar 28 '19 at 7:58
• @U.Windl I think this is the full answer in the generality that you asked the question. If you ask about the end parsing for a specific environment that would be a different question and of course need a slightly different answer. Mar 28 '19 at 8:05
• OK, I will add my environments, but I wanted to avoid showing you this ugly code. Mar 29 '19 at 1:54
• @U.Windl I extended the answer to cover the code you posted. Mar 29 '19 at 7:56