6

Some types of publications like for instance module documentation have a clear structure (for instance first one has an introduction section, at the end source code is provided,...)

I was wondering if it LaTeX or some package can force a user to obey a given structure. If the user fails to write a document according to the specified structure, at least an warning should be printed.

1
  • 2
    Put the information into a database and then generate LaTeX from that. Otherwise, not in any way that feels natural, IMHO. Oct 19 '13 at 3:39
6

As far as I know you can’t enforce the structure, in a way that \section only appear in a distinct order. But you can extend (imitate) the \maketitle mechanism.

Define some macros like

\newcommand{\mc@introduction}{}
\newcommand{\introduction}[1]{%
    \renewcommand{\mc@introduction}{#1}%
}

(The mc@ prefix can be chosen freely. I used mc for MyClass …) And use them inside a macro like \makedocument

\newcommand{\makedocument}{%
    Some text 

    \addsec{Introduction}
    \mc@introduction

    \let\makedocument\relax
}

The last line ensures that \makedocument can’t be used twice. And with etoolbox you can call \makedocument automatically at the end of the document.

\AtEndDocument{\makedocument}

If some of the information must be provided you may initialize the \mc@introduction macro like this

\newcommand{\mc@introduction}{
    \ClassError{myclass}{\string\introduction\space not used.}{%
        You must provide the introduction.
    }
}

When you redefine the \section you can permit to use it in the document

\renewcommand{\section}[1][]{%
    \ClassError{myclass}{Don't use \string\section}{%
        You are not allowed to use \string\section
        in your document!
    }
}

but you must save the definition before redefining if you want to use it in \makedocument with

\let\addsec\section

This is a full but very rudimentary example:

% myclass-test.tex
\documentclass{myclass}

\begin{document}
\faqs{The order in the document won't change anything.}

\introduction{Text}

%\section{Test}% caues an error

%\makedocument% optional
\end{document}

result

% myclass.cls
\ProvidesClass{myclass}

\LoadClassWithOptions{article}

\RequirePackage{etoolbox}

\newcommand{\mc@introduction}{
    \ClassError{myclass}{\string\introduction\space not used.}{%
        You must provide the introduction.
    }
}
\newcommand{\introduction}[1]{%
    \renewcommand{\mc@introduction}{#1}%
}
\newcommand{\mc@information}{}
\newcommand{\information}[1]{%
    \renewcommand{\mc@information}{#1}%
}
\newcommand{\mc@faqs}{}
\newcommand{\faqs}[1]{%
    \renewcommand{\mc@faqs}{#1}%
}

\let\addsec\section

\renewcommand{\section}[1][2]{%
    \ClassError{myclass}{Don't use \string\section}{%
        You are not allowed to use \string\section
        in your document!
    }
}

\newcommand{\makedocument}{%
    Some text that is always the same in
    every single document.

    \addsec{Introduction}
    \mc@introduction

    \addsec{Information}
    \mc@information

    \addsec{FAQs}
    \mc@faqs

    \let\makedocument\relax
}

\AtEndDocument{\makedocument}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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