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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?


share|improve this question

migrated from stackoverflow.com Dec 3 '12 at 3:58

This question came from our site for professional and enthusiast programmers.

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. – Peter Grill Nov 30 '12 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 '12 at 4:58

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:


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


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



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


    \begingroup% Keep change of \fboxrule local
        \ReportError[cyan!50]{Intro section missing.}%
        \ReportError[magenta!50]{Conclusion section missing.}%
        \ReportError{Errors detected.}%
        %\PackageError{\jobname}{Error(s) detected.}{}%

            \ReportError[cyan!50]{Multiple Introduction sections.}% 
            \ReportError[cyan!50]{Introduction after  Conclusion.}%
            \ReportError[magenta!50]{Multiple Conclusion sections.}%
            \ReportError[magenta!50]{Conclusion before Introduction.}%


    \section{Conclusions [Code 345]}
    \section{Introduction [Code 123]}
    \section{Introduction [Code 123]}
    \section{Conclusions [Code 345]}
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.