Skip to main content
7 of 8
added 209 characters in body
user avatar
user avatar

Here is one quick way of doing this. You have not said what you want the example and proof environments to look like so I have just created dummy ones.

The trick is to set some flag at the start of these environments: I have used two counters \myexample and \myproof. I have then hijacked the \section command and simply checked to see whether or not \myexample and \myproof are equal: if they are not then there is either an example with no proof, or a proof with no example.

The MWE writes the following messages in the log:

LaTeX Warning: Section 2 is missing an example or proof on input line 37.
LaTeX Warning: Section 3 is missing an example or proof on input line 41.
LaTeX Warning: Section 5 contains too many examples and proofs on input line 57.

(And hence also on the command-line.) Personally, I would have these messages printed in the PDF/dvi file as well, inside something like \ifdraft ... \fi so that you can automatically disable the messages in the output when not in draft mode. (Depending on which class file you are using, to do this you need to add \newif\ifdraft to your tex file and then you can turn "draft mode" on and off with \drafttrue and \draftfalse).

You said that there would never be more than one example and one proof but it probably makes sense to give a warning if some section contains 2 or more proofs/examples, so I have made the code do this.

Here is the code:

\documentclass{article}
\usepackage{etoolbox}
\let\realSection=\section% saving for use later
\newcount\myexample      % number of examples in a section
\newcount\myproof        % number of proofs in a section
% dummy proof/example environments
\newenvironment{example}{\noindent\textbf{Example}\global\advance\myexample by 1}{\par}
\newenvironment{proof}{\noindent\textbf{Proof}\global\advance\myproof by 1}{\par}
\makeatletter
% check if \myproof and \myexample are both 0 or both 1 or WARN
\newcommand\CheckExamplesProofs{%
   \ifnum\myexample=\myproof%
      \ifnum\myexample>1% too many examples/proofs is bad!
        \@latex@warning{Section \arabic{section}\space contains too many examples and proofs}%
      \fi%
   \else% different number of examples/proofs is bad!
      \@latex@warning{Section \arabic{section}\space is missing an example or proof}%
   \fi
   \myproof=0\myexample=0% new section so reset the counters
}
\makeatother
\def\section{\CheckExamplesProofs\realSection}
\AtEndDocument{\CheckExamplesProofs}% to check that the last section behaves

\begin{document}
\section{Problems 2.4}
\begin{example}
  Examples here
\end{example}
\begin{proof}
  Prove things
\end{proof}

\section{Problems 2.3}
  \begin{proof}A proof
  \end{proof}

\section{Problems 2.2}
  \begin{example}A example
  \end{example}

\section{Problem 2.1}

\section{Problem 2.1}
\begin{example}
  Examples here
\end{example}
\begin{proof}
  Prove things
\end{proof}
\begin{example}
  Examples here
\end{example}
\begin{proof}
  Prove things
\end{proof}

\end{document}

The command \AtEndDocument{\CheckExamplesProofs} checks the number of proofs and examples in the last section. Depending how your document is structured, you will probably need to have a similar check at the start of each chapter to make sure that the last section in the previous chapter has the correct number of proofs and examples. For this you could use:

\let\realChapter=\chapter
\def\chapter{\CheckExamplesProofs\realChapter}
user30471