# My custom theorem/proof environments aren't playing well together

As can be shown in the following MWE, when I have a theorem environment followed by a proof environment, the proof isn't going into the next line; it's rendering right on top of the text I have for the theorem.

\documentclass{book}
\usepackage{color}

\newcounter{theorem}
\setcounter{theorem}{0}
\def\thetheorem{\thesection.\arabic{theorem}}

\newenvironment{theorem}[1][]{%
\refstepcounter{theorem}
\vspace{5mm}
\makebox[-15pt][r]{
\normalfont\large\color{cyan} Theorem\ \thetheorem$\;\;\;\;$}\setbox0\hbox{\ }
\hspace{-\the\wd0}
\ifx\relax#1\relax \else{\large\color{cyan} #1\\[1mm]}\fi
}
{\ignorespacesafterend\medskip}%

\newcounter{example}
\setcounter{example}{0}
\def\theexample{\thesection.\arabic{example}}

\newenvironment{example}[1][]{%
\refstepcounter{example}
\vspace{5mm}
\makebox[-15pt][r]{
\normalfont\large\color{cyan} Example\ \theexample$\;\;\;\;$}\setbox0\hbox{\ }
\hspace{-\the\wd0}
\ifx\relax#1\relax \else{\large\color{cyan} #1\\[1mm]}\fi
}
{\ignorespacesafterend\medskip}%

\newenvironment{proof}{%
\vspace{5mm}
\makebox[-15pt][r]{
\normalfont\large\color{cyan} Proof$\;\;\;\;$}\setbox0\hbox{\ }
\hspace{-\the\wd0}
}
{\ignorespacesafterend\medskip}%

\begin{document}
\begin{theorem}
asdf
\end{theorem}
\begin{proof}
fdsa
\end{proof}

\end{document}

-
\ignorespacesafterend should always be last; and it doesn't matter if you end the paragraph at the environment end. Say \newcounter{theorem}[section] if you want that the counter is reset to zero in new sections. –  egreg Feb 16 '13 at 10:03

\medskip and friends don't work in horizontal mode. Put a \par (or a \\) in the environment closure. (And then play with horizontal spacing ;-)

-
\par did it.. Thank you very much! –  agent154 Feb 16 '13 at 2:20
\medskip does indeed force a \par; it's \vspace that doesn't. –  egreg Feb 16 '13 at 10:18
@egreg It does in plain TeX; in LaTeX, not. ?!? Checked bla\medskip blaon three computers ;-) –  Sašo Živanović Feb 16 '13 at 10:52
@SašoŽivanović You're right; I was thinking to a different thing. –  egreg Feb 16 '13 at 11:04

You're doing many things in a complicated way and so have very bad control of spaces. Here's a proposal for a better realization.

\documentclass{book}
\usepackage{color}
\usepackage{lipsum} % for mock text

\newcounter{theorem}[section]
\def\thetheorem{\thesection.\arabic{theorem}}

\newenvironment{theorem}[1][]{%
\refstepcounter{theorem}%
\noindent\makebox[0pt][r]{%
\ifx\relax#1\relax \else{\large\color{cyan}#1\\[1mm]}\fi
\ignorespaces}

\newcounter{example}[section]
\def\theexample{\thesection.\arabic{example}}

\newenvironment{example}[1][]{%
\refstepcounter{example}%
\noindent\makebox[0pt][r]{%
\ifx\relax#1\relax\else{\large\color{cyan}#1\\[1mm]}\fi
\ignorespaces}

\newenvironment{proof}{%
\noindent\makebox[0pt][r]{%
\ignorespaces}

\begin{document}
\lipsum[1]

\begin{theorem}
\lipsum*[2]
\end{theorem}

\begin{proof}
\lipsum*[3]
\end{proof}

\end{document}

1. Use \newcounter{theorem}[section] in order to reset the counter at each section.

2. Use \par\addvspace{5mm} so that this won't add space in excess when following other environments that use the same command (such as lists).

3. With \noindent you can control precisely where your label will go. Don't forget \ignorespaces at the end of the "begin part"

4. \ignorespacesafterend is useless in the "end part" since you force a paragraph end; in any case this must be the last token.

5. Be careful with spurious spaces in your definition. I've protected with % more end-of-lines than necessary, but better be safe than sorry.

-
+1 for the elegant documented clean rewrite. –  Ethan Bolker Feb 16 '13 at 13:49