8

I have a document which is required to have an example for (most) proofs. Is there an easy way to have LaTeX check that I haven't forgotten an example? I'm imagining something like this:

\documentclass{article}
\begin{document}
\section{Problems 2.4}
\begin{examples}
  Examples here
\end{examples}
\begin{proof}
  Prove things
\end{proof}
\end{document}

where if I leave out the examples environment I get some kind of warning.

(I'm actually writing the problems in \items in an enumerate, but I'm willing to switch to using sections or subsections since that probably makes more sense)

Answers to questions from comment:

Do the examples and proofs have to be in a particular order or can examples sometimes come before proofs and proofs sometimes come before examples?

It doesn't matter particularly; it would probably be slightly better to have examples before proofs, but either order is fine. I usually put the examples before the proofs—it doesn't need to have the option to vary.

Can a given section have more than one proof or example?

Nope

Next, do you prefer using \subsection to \item?

Not particularly; I'm not sure what's the usual way to do problem sets in a textbook, but it seems \item usually matches the textbook numbering.

Finally, should the warning appear in the log file or should it be printed in the text?

In the log file would be preferable; it would also be nice if it was printed in the output to the terminal.

3
  • This is certainly possible. Do the examples and proofs have to be in a particular order or can examples sometimes come before proofs and proofs sometimes come before examples? Can a given section have more than one proof or example? Next, do you prefer using \subsection to \item? Finally, should the warning appear in the log file or should it be printed in the text?
    – user30471
    Commented May 6, 2015 at 7:01
  • @Andrew See edit to question
    – andyg0808
    Commented May 6, 2015 at 8:07
  • @andy0808: If you can change to book it might be possible to use cntperchap, but I think, I can adapt the code of the package.
    – user31729
    Commented May 6, 2015 at 12:43

1 Answer 1

10

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 may need to add \newif\ifdraft to your tex file after which 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}
2
  • 1
    Will this logic check the final section, since the check seems to hinge on the invocation of the following section? Or am I missing something? Commented May 6, 2015 at 14:00
  • 1
    @StevenB.Segletes I decided to also check for sections that contained too many proofs and examples. When I was doing this I noticed and fixed the issue you found by adding a check at the end of the document with \AtEndDocument from the etoolbox package.
    – user30471
    Commented May 6, 2015 at 14:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .