3

I want some files to compile with two different templates, one that has the hierarchy like chapter > section > subsection and the other as section > subsection > subsubsection.

For that, I tried to define newcommands to act like an alias to the level like:

\newcommand{\level1}[1]{\chapter{#1}}

\newcommand{\level2}[1]{\section{#1}}

\newcommand{\level3}[1]{\subsection{#1}}

and the in the body I would call \level1{title}. This way I could change from templates only by changing this 3 lines.

I use lines like: \newcommand{\secref}[1]{Section \ref{#1}}, but the ones with \chapter and so on don't work.

Any idea on how can I solve this?

4

1 Answer 1

5

This might be what you're looking for:

\documentclass{report}

\newcommand\level[1]{
\begingroup\ifcase#1\relax
    \GenericError{}{\string\level\space given an invalid argument}{}{}
    \or\aftergroup\chapter
    \or\aftergroup\section
    \or\aftergroup\subsection
    \or\aftergroup\subsubsection
    \or\aftergroup\paragraph
    \or\aftergroup\subparagraph
    \else\GenericError{}{\string\level\space given an invalid argument}{}{}
    \fi\endgroup
}

\begin{document}

\level1{CHCHCH}

\level2{SESESE}

\level2{SESESESE}

\level3{SUBSUBSUB}

\level4{subsubsub}

\end{document}

We use the TeX primitive \ifcase on the argument. The \aftergroup construction is necessary so that the sectioning command is the last thing to be issued by the macro. We use LaTeX's \GenericError to generate an error if incorrect input is given.

In book class, you might add \or\part before \or\chapter. In article class, on the other hand, you should comment out \or\chapter.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .