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.

I'd like to try to make a LaTeX class which enforces a number of (non-empty) sections to exist-- if the user doesn't define them manually, then some default section content will be created.

\begin{document}

\section{Section which requires subsections ``African swallow'' and ``European swallow''}

\subsection{African swallow}

The unladen airspeed is \ldots  % The section is defined and has content, so it is okay

% subsection ``European swallow'' doesn't exist (and therefore is also missing content, of course), so here the compiler adds:
% \subsection{European swallow}
%
% The unladen airspeed is \ldots, although when carrying a coconut it is much less.

\end{document}

It would be even cooler if I could also specify that certain sections must be defined (and are not empty), otherwise the compiler exits with an error.

I hope my question is understandable... I'd be grateful for any tips.

share|improve this question

1 Answer 1

up vote 5 down vote accepted

I assume that this feature is used for some kind of "to do" markers. If a new section is started, then required parts are specified (comma separated list):

\require{African swallow, European swallow}

And if a part is finished, it is closed with

\done{African swallow}

At the end of the document the missing \dones are reported.

\documentclass{article}

\usepackage{kvsetkeys}
\makeatletter
\newcommand*{\req@list}{}
\newcommand*{\@require}[1]{%
  \xdef\req@list{%
    \req@list{\detokenize{#1}}%
  }%
}
\newcommand*{\require}[1]{%
  \comma@parse{#1}\@require
}
\newcommand*{\done}[1]{%
  \global\expandafter
  \let\csname done@\detokenize{#1}\endcsname\@empty
}
\newcommand*{\check@requirements}{%
  \begingroup
    \typeout{}%
    \typeout{ToDo list}%
    \typeout{=========}%
    \expandafter
    \@tfor\expandafter\x\expandafter:\expandafter=\req@list\do{%
      \@ifundefined{done@\x}{%
        \typeout{! ToDo: \x}%
      }{}%
    }%
    \typeout{}%
  \endgroup
}
\AtEndDocument{\check@requirements}
\makeatother

\begin{document}

\section{Section which requires subsections ``African swallow'' and ``European swallow''}
\require{African swallow, European swallow}

\subsection{African swallow}

The unladen airspeed is \ldots  % The section is defined and has content, so it is okay
\done{African swallow}

% subsection ``European swallow'' doesn't exist (and therefore is also missing content, of course), so here the compiler adds:
% \subsection{European swallow}
%
% The unladen airspeed is \ldots, although when carrying a coconut it is much less.

\end{document}

And the report at the end of the LaTeX run:

ToDo list
=========
! ToDo: European swallow
share|improve this answer

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.