7

Is there a way to design a LaTeX template that would either enforce given sections, subsections, etc or generate an error if a given section label were missing upon compilation?

For example, suppose that I always needed this for all documents that use my template:

\section{Introduction [Code 123]}

\section{Conclusions [Code 345]}

Assume that other sections are allowed without restrictions, and that required sections need to appear in the given sequence.

How could I enforce this programmatically within the template?

Thanks!

2
  • Thanks for the clarification. However, you should still compose all the test cases in the way of fully compilable examples and define what should be done in those cases. Should the compile terminate, or do you just want an error message in the log file. I realize that composing the test cases is not that difficult, but should really try to minimize the time those trying to help you have to spend on fully understanding your requirements and testing that that they have been met. Nov 30, 2012 at 23:06
  • Welcome to TeX.sx! Your post was migrated here from Stack Overflow. Please register on this site, too, and make sure that both accounts are associated with each other (by using the same OpenID), otherwise you won't be able to comment on or accept answers or edit your question.
    – Werner
    Dec 3, 2012 at 4:58

1 Answer 1

8

You can redefine the \section command to set various flags so that we can do the appropriate checks.

A document without either of those two sections:

\begin{document}
    \section{Summary}
    \section{Discussion}
\end{document}

will be typeset as follows:

enter image description here

Note that messages specific to the Introduction section are in cyan, and those related to the Conclusion are in magenta.

A document containing the Introduction and/or Conclusion sections in the wrong order, or multiple occurrences of the given sections:

enter image description here

Notes:

  • Since no test case was provided only minimal testing has been done. Needs further testing to ensure that all cases have been covered.
  • I have used \newtoggle from the etoolbox package as I prefer that syntax versus the \newif syntax. But if you don't want to include an additional package it should be pretty straightforward to adapt this to use \newif or some other conditional methods.
  • The xstring package for string comparison.
  • As you can see I am not very good at selecting goes with colors. However, in this case, that turns out to be a good thing as you don't really want to be seeing those messages in your document.
  • You could also use \PackageError (see commented code) to quit out with a console message if so desired.

Code:

\documentclass{article}
\usepackage{xcolor}
\usepackage{etoolbox}
\usepackage{xstring}
\usepackage{calc}

\newcommand*{\IntroSectionName}{Introduction [Code 123]}%
\newcommand*{\ConclusionSectionName}{Conclusions [Code 345]}%

\newtoggle{ErrorDetected}
\newtoggle{IntroSectionEncountered}
\newtoggle{ConclusionSectionEncountered}

\newcommand*{\ReportError}[2][yellow]{%
    \global\toggletrue{ErrorDetected}%
    \begingroup% Keep change of \fboxrule local
    \setlength{\fboxrule}{6pt}%
    \par\noindent\ignorespaces%
    \fcolorbox{red}{#1}{\parbox{\linewidth-2\fboxrule-2\fboxsep}{\raggedright#2}}%
    \endgroup%
}%
\newcommand*{\CheckErrors}{%
    \iftoggle{IntroSectionEncountered}{}{%
        \ReportError[cyan!50]{Intro section missing.}%
    }%
    \iftoggle{ConclusionSectionEncountered}{}{%
        \ReportError[magenta!50]{Conclusion section missing.}%
    }%
    \iftoggle{ErrorDetected}{%
        \ReportError{Errors detected.}%
        %\PackageError{\jobname}{Error(s) detected.}{}%
    }{}%
}%

\let\OldSection\section%
\renewcommand*{\section}[1]{%
    \IfStrEq{#1}{\IntroSectionName}{%
        \iftoggle{IntroSectionEncountered}{%
            \ReportError[cyan!50]{Multiple Introduction sections.}% 
        }{}%
        \iftoggle{ConclusionSectionEncountered}{%
            \ReportError[cyan!50]{Introduction after  Conclusion.}%
        }{}%
        \global\toggletrue{IntroSectionEncountered}%
    }{}%
    \IfStrEq{#1}{\ConclusionSectionName}{%
        \iftoggle{ConclusionSectionEncountered}{%
            \ReportError[magenta!50]{Multiple Conclusion sections.}%
        }{}%
        \iftoggle{IntroSectionEncountered}{}{%
            \ReportError[magenta!50]{Conclusion before Introduction.}%
        }%
        \global\toggletrue{ConclusionSectionEncountered}%
    }{}%
    \OldSection{#1}%
}%


\AtEndDocument{\CheckErrors}

\begin{document}
    \section{Conclusions [Code 345]}
    \section{Introduction [Code 123]}
    \section{Introduction [Code 123]}
    \section{Conclusions [Code 345]}
\end{document}

You must log in to answer this question.