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.

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.

enter image description here

\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}
share|improve this question
    
\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
add comment

2 Answers

up vote 5 down vote accepted

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

share|improve this answer
    
\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
add comment

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][]{%
  \par\addvspace{5mm}%
  \refstepcounter{theorem}%
  \noindent\makebox[0pt][r]{%
    \normalfont{\large\color{cyan}Theorem\ \thetheorem}\quad}%
    \ifx\relax#1\relax \else{\large\color{cyan}#1\\[1mm]}\fi
  \ignorespaces}
  {\par\addvspace{\medskipamount}}

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

\newenvironment{example}[1][]{%
  \par\addvspace{5mm}%
  \refstepcounter{example}%
  \noindent\makebox[0pt][r]{%
    \normalfont{\large\color{cyan}Example\ \theexample}\quad}%
    \ifx\relax#1\relax\else{\large\color{cyan}#1\\[1mm]}\fi
  \ignorespaces}
  {\par\addvspace{\medskipamount}}

\newenvironment{proof}{%
  \par\addvspace{5mm}
  \noindent\makebox[0pt][r]{%
    \normalfont{\large\color{cyan}Proof}\quad}%
  \ignorespaces}
  {\par\addvspace{\medskipamount}}

\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.

enter image description here

share|improve this answer
    
+1 for the elegant documented clean rewrite. –  Ethan Bolker Feb 16 '13 at 13:49
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.