# Horizontal space in a new environment after \label{}

Why is the horizontal space inserted in the following mwe?

\documentclass{scrartcl}

\usepackage[american]{babel}
\usepackage{environ}
\usepackage{amsthm}

% proof environment
\makeatletter
\let\proof\relax
\let\endproof\relax
\NewEnviron{proof}[1][true]{%
\pushQED{\qed}%
\csname if#1\endcsname
\trivlist
\item\relax{\sffamily\bfseries Proof}\par\noindent
\BODY
\popQED\endtrivlist
\fi
}
\makeatother

\begin{document}
\begin{proof}
Just some text
\end{proof}

\begin{proof}\label{foo}
Just some text (after a label)
\end{proof}

\begin{proof}[true]
Just some text after optional argument true
\end{proof}
\end{document}

• Try ending the line as \label{foo}%% to avoid the unwanted additional whitespace – A.Ellett Apr 26 '14 at 20:19

If you use a construct such as \par\noindent getting spurious space in the output is inevitable, \label just happens to be the trigger here but the real issue is that you have started horizontal mode too early, and in horizontal mode white space makes a word space. You can see that \label is not really involved if you change your original document to have \begin{proof}{} just the {} is enough to get the bad space you don't need \label{foo}.

\documentclass{scrartcl}

\usepackage[american]{babel}
\usepackage{environ}
\usepackage{amsthm}

% proof environment
\makeatletter
\let\proof\relax
\let\endproof\relax
\NewEnviron{proof}[1][true]{%
\pushQED{\qed}%
\csname if#1\endcsname
\trivlist
\labelsep\z@
\def\makelabel##1{\makebox[\linewidth][l]{##1}}%
\item[\sffamily\bfseries Proof]%
\BODY
\popQED\endtrivlist
\fi
}
\makeatother

\begin{document}

\noindent X\dotfill X

\begin{proof}
Just some text\\hhh
\end{proof}

\begin{proof}\label{foo}
Just some text (after a label)
\end{proof}

\begin{proof}[true]
Just some text after optional argument true
\end{proof}
\end{document}

• OK now it's my turn to be baffled. I would have thought, even with your approach, that \label{...} ending a line or any atom such as {} ending the line without a terminal % would still result in unwanted space popping up. What's happening? – A.Ellett Apr 27 '14 at 1:52
• @A.Ellett tex is in vertical mode after an item label, as it is after section headings and anywhere else Latex expects you to use \label. – David Carlisle Apr 27 '14 at 1:53
• But even when I place {} as the first content after the beginning of the proof environment, I still don't get extra space. Shouldn't {} be starting horizontal mode? – A.Ellett Apr 27 '14 at 1:55
• @A.Ellett well it doesn't: (it just opens and closes a group) "should" is a judgement on Knuth's design and I wouldn't do that:-) – David Carlisle Apr 27 '14 at 1:57
• Wow. I'm apparently transgressing rather badly today. ;P – A.Ellett Apr 27 '14 at 1:58

LaTeX is notoriously finicky about whitespace at the end of lines.

So to avoid extra white space from creeping in it can help to add a comment at the end of the offending line.

\label{foo}%%


Generally it is safe to end the line with a control sequence such as

\bfseries


But anything other than a control sequence will result in additional whitespace added to your page. The only place where LaTeX usually ignores white space is at the beginning of a line. (I say usually because in certain contexts such as verbatim environments and their ilk, this is not the case.)

• hey don't blame latex: if the user asks for space, that's what the user gets:-) – David Carlisle Apr 27 '14 at 1:29
• @DavidCarlisle Perhaps by finicky I mean, LaTeX does exactly as you tell it to do. So I guess I'm just stating the obvious: LaTeX is as LaTeX does. ;) – A.Ellett Apr 27 '14 at 1:31
• OK you're forgiven then – David Carlisle Apr 27 '14 at 1:33

If you really, really want a line break after Proof, then use a different approach, with the help of enumitem:

\documentclass{scrartcl}

\usepackage[american]{babel}
\usepackage{environ,letltxmacro,enumitem}
\usepackage{amsthm}
\usepackage{amsmath}

\newif\ifshowproof
\showprooftrue

% proof environment
\let\proof\relax
\let\endproof\relax
\NewEnviron{proof}[1][showproof]{%
\csname if#1\endcsname
\pushQED{\qed}%
\description[topsep=6pt plus 6pt,style=nextline,leftmargin=0pt,font=\bfseries]
\item[Proof]
\BODY
\popQED\enddescription
\fi
}

\begin{document}
Proof with hide feature:
\begin{proof}[false]\label{x}
This should not appear
\end{proof}

Proof without hide feature:
\begin{proof}\label{y}
This should appear
\end{proof}
\end{document}


But, please, have mercy of your readers. ;-)

• Hi, thanks. This does not introduce a line break if the proof starts with an enumerate environment :-( – Marius Hofert Apr 26 '14 at 20:41
• @MariusHofert This seems a bug of enumitem. I'd like to help you, but I won't pursue this any more: I firmly believe that (1) the label “Proof” should not be boldface; (2) the label should not be in a line by itself; (3) no proof should be a big enumerate environment. – egreg Apr 26 '14 at 20:44