4

Admit, this is a very special question, but I am quite sure, that it is realizable with LaTeX.

I would like not to count my chapters like

1 foo

1.1 Problems

1.2 Solutions

2 bar

2.1 Problems

2.2 Solutions

but

LAW foo

LAW.1 Problems

LAW.2 Solutions

VNR bar

VNR.1 Problems

VNR.2 Solutions

and so on.

Any ideas?

Remark: I do not need an index, a TOC would be nice (only on the highest level) but is not crucial.

\document{book}
\begin{document}
 \chapter{foo}
 \section{Problems}
 \section{Solutions}
 \chapter{bar}
 \section{Problems}
 \section{Solutions}
\end{document}
  • article does not have \chapter ;-) – user31729 Jun 21 '16 at 15:25
  • Oh, sorry, yes! I am going to change it. – Jürgen Jun 21 '16 at 17:22
4

It's certainly possible:

\documentclass{book}

\makeatletter
\newcommand{\chapterkey}[1]{%
  \renewcommand{\chapter@key}{#1}%
}
\newcommand{\chapter@key}{???} % initialize
\renewcommand{\thechapter}{\chapter@key}
\makeatother

\begin{document}

\chapterkey{LAW}
\chapter{foo}

\section{Problems}
\section{Solutions}

\chapterkey{VNR}
\chapter{bar}

\section{Problems}
\section{Solutions}

\end{document}

enter image description here

| improve this answer | |
  • @campa Separation of tasks. – egreg Jun 21 '16 at 15:24
  • Works like a charm, thanks a lot. And the main advantage is really that it is more flexible as I do not have to define the prefix in advance. I modified the command to \newcommand{\mychapter}[2]{\chapterkey{#1}\chapter{#2}}. – Jürgen Jun 22 '16 at 6:09
4

Using \ifcase...\fi conditional in advance (requires the relevant chapter prefixes to be defined in advance, of course too!)

\documentclass{book}
\usepackage{tocloft}

\addtolength{\cftchapnumwidth}{20pt}
\addtolength{\cftsecnumwidth}{20pt}

\renewcommand{\thechapter}{%
  \ifcase\value{chapter}
  \or
  LAW%
  \or
  VNR%
  \or
  FOO%
  \or
  FOOBAR%
  \or
  UPVOTEANSWERS%
  \fi
}

\begin{document}
\tableofcontents
\chapter{foo}

\section{Problems}
\section{Solutions}

\chapter{bar}

\section{Problems}
\section{Solutions}


\chapter{more foo}

\section{Problems}
\section{Solutions}

\chapter{Even more foo}

\section{Problems}
\section{Solutions}


\end{document}

enter image description here

A more flexible version with expl3 features: Storing all in a list and grab the n-th item:

\documentclass{book}
\usepackage{tocloft}

\usepackage{xparse}

\ExplSyntaxOn
\seq_new:N \l_juergen_chapternumbers_seq 

\newcommand{\StoreChapterNumbers}[1]{%
  \seq_set_from_clist:Nn \l_juergen_chapternumbers_seq {#1}%
}

\newcommand{\grabchapternumber}[1]{%
  \seq_item:Nn \l_juergen_chapternumbers_seq{#1}
}
\ExplSyntaxOff

\StoreChapterNumbers{LAW,A-TEAM,BAR,WHO,UN}

\addtolength{\cftchapnumwidth}{20pt}
\addtolength{\cftsecnumwidth}{20pt}
\makeatletter
\renewcommand{\thechapter}{%
  \grabchapternumber{\number\value{chapter}}%
}

\begin{document}
\tableofcontents
\chapter{foo}

\section{Problems}
\section{Solutions}

\chapter{bar}

\section{Problems}
\section{Solutions}


\chapter{more foo}

\section{Problems}
\section{Solutions}

\chapter{Even more foo}

\section{Problems}
\section{Solutions}


\end{document}

enter image description here

| improve this answer | |
  • Thanks a lot, works perfectly (not regarding the error, that my beamer class is too old). However I do not understand the code of the ExplSyntax, alas. So I prefer the simpler solution with \ifcase. – Jürgen Jun 22 '16 at 5:51
  • @Jürgen: beamer???? – user31729 Jun 22 '16 at 6:02
  • Yes, beamer. I got an error message saying something about a required beamer version. But when I try to replicate this dubious message does not show anymore. It must have been a relic, perhaps in an old aux-file or something like this. – Jürgen Jun 22 '16 at 7:46
  • @Jürgen: Hm, does beamer have chapter sectioning??? – user31729 Jun 22 '16 at 8:34
  • Don't know, probably not. I have a test bench to test small snippets of code (in the sense of a minimal working example) and there may be old aux-files in this directory with the same name. This is my only idea how this message might have happened. – Jürgen Jun 22 '16 at 8:38

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.