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.

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!

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
add comment

1 Answer 1

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

Your Answer

 
discard

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