[Tex/LaTex] Separate column in table of contents (in Memoir)

memoirsectioningtable of contents

I'm curious if there is a way to have another "column" of numbers in the Table of Contents. The book I'm trying to typeset has a column of chapter and section titles, a corresponding column of subsection number-ranges and a corresponding column of page numbers. So, for example, for chapter one section one, which has subsections one through five on it, and is on page 2, the line would read:

1.1 Section Title ….. 1-5 … 2

I've looked at the memoir and titletoc documentation, but to no avail. The output should look something like this:

an example of the TOC I'm trying to create: columns for 1) chapter number and name, 2) Sections, 3) Pages

Best Answer

One can do this with etoc, although it is not entirely straightforward.

A revised version is added at the bottom of the answer: it simplifies the coding a bit, and tries hard to get even closer to the original. Note that in the code here the \bfseries inside the \textsc does not work:

Font shape `OT1/cmr/bx/sc' undefined 
using `OT1/cmr/bx/n' instead

But with T1 encoding it works, so I use it in the second proposal.

\documentclass{memoir}

\renewcommand{\thechapter}{\Roman{chapter}}
\counterwithout{section}{chapter}

\usepackage{etoc}
\usepackage{color}

% \usepackage[linktoc=all]{hyperref} % try it, it works!
% \etoctoclines  % removed as it is unnecessary: styles for part, 
                 % chapter, and section are entirely re-defined below

\renewcommand\etoctoclineleaders{\hbox to .12\linewidth{\hss...\hss}}
\newif\iffirstextradone
\newif\ifsecondextradone

\etocsetstyle{part}
{}
{}
{\begin{center}\Large\bfseries PART \etocnumber
\ifsecondextradone\\\etocname.\fi\end{center}
\iffirstextradone\else
\mbox{}\hskip.72\linewidth\hbox to .23\linewidth
    {\textsc{\bfseries Sections.\hss Page.}}\par\fi
\firstextradonetrue}
{}

\etocsetstyle{chapter}
{}
{\par\noindent\etocifnumbered{\makebox[.1\linewidth][r]
    {\bfseries\etocnumber.}}{\hspace*{.1\linewidth}}}
{\hbox to .7\linewidth
    {\hspace{1em}\etocname\unskip\leaders\etoctoclineleaders\hss}%
\hbox to .15\linewidth{\hss\ifsecondextradone\etocifnumbered\etocpage%
{}\else\etocpage\fi}%
\hskip-.3\linewidth\leaders\etoctoclineleaders\hskip.2\linewidth
\hskip-.1\linewidth}
{}

\newbox\forsectionnumbers
\makeatletter
\etocsetstyle{section}
{\setbox\forsectionnumbers=\hbox to .1\linewidth
    \bgroup\hss\etocskipfirstprefix}
{\@gobble}
{\etocnumber---}
{\etocnumber\hss\egroup\colorbox{white}{\box\forsectionnumbers}\par
\ifsecondextradone\else
\begin{center}\bfseries Concord.\end{center}
\noindent \textsc{\bfseries Lesson.}\par\fi\secondextradonetrue
}
\makeatother

\begin{document}

\etocruledstyle[1]{\parbox{\linewidth}{\centering
\textsc{\Huge\MakeUppercase{Table of Contents}}\\
\rule{.2\linewidth}{2pt}}}

\tableofcontents*

\part{}

\chapter*{Introductory}
\addcontentsline{toc}{chapter}{Introductory}

\newcount\tempcount

\loop\advance\tempcount by 1
\section{This is}
section \the\tempcount
\ifnum\tempcount<8
\repeat

\chapter{Concord of Subject and Verb}

\loop\advance\tempcount by 1
\section{This is}
section \the\tempcount
\ifnum\tempcount<17
\repeat

\chapter{Concord of Substantive and Adjective}

\loop\advance\tempcount by 1
\section{This is}
section \the\tempcount
\ifnum\tempcount<22
\repeat

\chapter*{Concord of Relative and its Antecedent}
\addcontentsline{toc}{chapter}{Concord of Relative and its Antecedent}

\loop\advance\tempcount by 1
\section{This is}
section \the\tempcount
\ifnum\tempcount<25
\repeat

\part{Government}

\chapter{The Accusative Case}

\chapter*{General uses}
\addcontentsline{toc}{chapter}{General Uses}

\loop\advance\tempcount by 1
\section{This is}
section \the\tempcount
\ifnum\tempcount<30
\repeat

\chapter*{Particular uses}
\addcontentsline{toc}{chapter}{Particular Uses}

\loop\advance\tempcount by 1
\section{This is}
section \the\tempcount
\ifnum\tempcount<37
\repeat

\chapter{Verbs governing two Accusatives}

\loop\advance\tempcount by 1
\section{This is}
section \the\tempcount
\ifnum\tempcount<41
\repeat

\chapter{The Causal}

\loop\advance\tempcount by 1
\section{This is}
section \the\tempcount
\ifnum\tempcount<49
\repeat

\chapter{The Instrumental Case}

\chapter*{General uses}
\addcontentsline{toc}{chapter}{General Uses}

\loop\advance\tempcount by 1
\section{This is}
section \the\tempcount
\ifnum\tempcount<54
\repeat

\chapter*{Particular uses}
\addcontentsline{toc}{chapter}{Particular Uses}

\loop\advance\tempcount by 1
\section{This is}
section \the\tempcount
\ifnum\tempcount<59
\repeat

\chapter{The Dative Case}

\chapter*{General uses}
\addcontentsline{toc}{chapter}{General Uses}

\loop\advance\tempcount by 1
\section{This is}
section \the\tempcount
\ifnum\tempcount<65
\repeat

\chapter*{Particular uses}
\addcontentsline{toc}{chapter}{Particular Uses}

\loop\advance\tempcount by 1
\section{This is}
section \the\tempcount
\ifnum\tempcount<71
\repeat

\chapter{The Ablative Case}

\chapter*{General uses}
\addcontentsline{toc}{chapter}{General Uses}

\loop\advance\tempcount by 1
\section{This is}
section \the\tempcount
\ifnum\tempcount<75
\repeat

\chapter*{Particular uses}
\addcontentsline{toc}{chapter}{Particular Uses}

\loop\advance\tempcount by 1
\section{This is}
section \the\tempcount
\ifnum\tempcount<86
\repeat

\chapter{The Locative Case}

\chapter*{General uses}
\addcontentsline{toc}{chapter}{General Uses}

\loop\advance\tempcount by 1
\section{This is}
section \the\tempcount
\ifnum\tempcount<92
\repeat

\chapter*{Particular uses}
\addcontentsline{toc}{chapter}{Particular Uses}

\loop\advance\tempcount by 1
\section{This is}
section \the\tempcount
\ifnum\tempcount<100
\repeat

\end{document}

Unfortunately the \etocnumber macro is not something to which one can apply \arabic or \Roman. So one has to make sure that the number is registered in the .toc file in the desired format. Renewing \thechapter does it.

I also took advantage of the memoir command \counterwithout which was very handy here to get the sections numbered as desired (I had to go into the memoir manual as I am not familiar with that class).

Finally, I had to manually put in the toc file the unnumbered chapters with some \addcontentsline commands.

Setting up the etoc styles for part, chapter and section was a bit subtle and I used one or two tricks to get the desired look.


Update: some explanations on the code.

With etoc one specifies a style for each sectioning level actually to be displayed in the TOC, so here part, chapter, section. The command \etoctoclines will use the package pre-defined styles, but here we need to redefine all of them (part, chapter, and section). Even when it is not clearly a requirement as here to redefine all styles, it is often a necessity as the pre-defined styles for example expect to start in 'vertical mode', and this puts a constraint on what one redefines. So typically one has to redefine all styles used (the pre-defined styles can be customized as explained in the manual, rather than redefined).

So one does \etocsetstyle{part or chapter or section}{A}{B}{C}{D} where BC is executed for each part, chapter, or section, and A is executed once at the beginning and D once at the end. This means that the first section in a chapter (or in a part if there was no chapter in-between) excutes A and B and C, the last in the chapter executes B and C and D, and the others do B and C.

The B and C parts are supposed to be built up with \etocname, \etocnumber and \etocpage. When hyperref is present they all are hyperlinks (for the page number, if linktoc=all was an hyperref option). The reason for a separate B and C is that sometimes one wishes to skip the B of the first section. For this one ends the A with the command \etocskipfirstprefix.

Here the B for the section line style is used to do a trick: it is set up to gobble the C, but as we used \etocskipfirstprefix the result is that all section numbers are indeed gobbled except the first one. And the D of the last section then puts in the missing (gobbled) final section number. The A opened a box which is then closed by D, and printed by D. This is to use a colorbox to erase the dots which were printed before by the style for chapter...

The codes for part and chapter do some tricks to add 'extra decorations' to the table of contents, interrupting its normal flow to set up the Lessons, Sections and Page headings.

The reason for the \unskip after \etocname in the chapter line style is that etoc uses the package xspace and that each of \etocname, \etocnumber and \etocpage ends with an \xspace. Sometimes one has to cancel its effect, and it seems to be the case before leaders.


Above I discussed the 'line styles', now there is the matter of the title of the TOC and its global style: is it printed in one or two columns? does it add its name to the .toc file? etc...

Here I used (I didn't give much thought to it, some other package commands which could have been suitable) the \etocruledstyle command, with argument [1] to say we are on one column (the default is two). The reason to use the command is that it centers its argument, not for the 'rule' in its name: normally the command puts two horizontal rules on each side, but here using a parbox of maximal width we canceled these rules. And then a rule is explicitely added to the definition of the parbox.

Overall, the code uses a lot of TeX looking things, the reason is simple, I never remember the LaTeX commands for boxes and rules, and I have often problems when I try to use them, so I always end up going back to \hbox and \rule...

nice table of contents

\documentclass[letterpaper]{memoir}
\usepackage[T1]{fontenc}
%\usepackage{layout}
% I am just ignorant of the memoir commands to set up
% the page parameters. I just want my text centered on the page.
\setlength{\textwidth}{5.5in}
\setlength{\oddsidemargin}{.5in}
\setlength{\evensidemargin}{.5in}

\renewcommand{\thechapter}{\Roman{chapter}}
\counterwithout{section}{chapter}

\usepackage{etoc}
\usepackage{color}
% \usepackage[linktoc=all]{hyperref}

\newif\ifextraidone
\newif\ifextraiidone

\etocsetstyle{part}
{}
{}
{\begin{center}\Large\bfseries PART \etocnumber
\ifextraiidone\\\etocname.\fi\end{center}
\ifextraidone\else
\noindent\hskip.7\linewidth
    \hbox to .2\linewidth
    {\hss\small\textsc{\bfseries Sections.\ Page.}}\par\fi
\extraidonetrue
}
{}

\newcommand\mytocleaders{\hbox to .125\linewidth{...\hss}}
\etocsetstyle{chapter}
{}
{\par\noindent\etocifnumbered
    {\makebox[.15\linewidth][r]{\bfseries\etocnumber.}}
    {\hspace*{.15\linewidth}}}
{\hbox to .65\linewidth
    {\hspace{1em}\etocname\unskip\leaders\mytocleaders\hss}%
 \hskip-.1\linewidth
 \hbox to .2\linewidth{\hspace{1.5em}...\hss
                \etocifnumbered
                    {\etocpage}
                    {\ifextraiidone\else\etocpage\fi}}%
 \hskip-.2\linewidth
}
{}

\newbox\forsectionnumbers
\makeatletter
\etocsetstyle{section}
{\setbox\forsectionnumbers=\hbox to .1\linewidth
    \bgroup\hss\etocskipfirstprefix}
{\@gobble}
{\etocnumber---}
{\etocnumber\egroup
% for reasons I do not quite understand, in some pdf viewers the dots
% do not completely disappear if here \fboxsep0pt is used.
    \fboxsep1pt\colorbox{white}{\box\forsectionnumbers}\par
    \ifextraiidone\else
        \begin{center}\bfseries Concord.\end{center}
        \noindent\makebox[.15\linewidth][r]{\textsc{\bfseries Lesson.}}\par
    \fi\extraiidonetrue
}
\makeatother

\begin{document}%\layout

\renewcommand{\etocinnertopsep}{0pt}

\etocruledstyle[1]{%
    \parbox{\linewidth}{%
        \centering
        \textsc{\bfseries\LARGE\MakeUppercase{Table of Contents}}\\
        \rule{.2\linewidth}{2pt}}%
}

\tableofcontents*

\part{}

\chapter*{Introductory}
\addcontentsline{toc}{chapter}{Introductory}

\newcount\tempcount

\loop\advance\tempcount by 1
\section{This is}
section \the\tempcount
\ifnum\tempcount<8
\repeat

\chapter{Concord of Subject and Verb}

\loop\advance\tempcount by 1
\section{This is}
section \the\tempcount
\ifnum\tempcount<17
\repeat

\chapter{Concord of Substantive and Adjective}

\loop\advance\tempcount by 1
\section{This is}
section \the\tempcount
\ifnum\tempcount<22
\repeat

\chapter*{Concord of Relative and its Antecedent}
\addcontentsline{toc}{chapter}{Concord of Relative and its Antecedent}

\loop\advance\tempcount by 1
\section{This is}
section \the\tempcount
\ifnum\tempcount<25
\repeat

\part{Government}

\chapter{The Accusative Case}

\chapter*{General uses}
\addcontentsline{toc}{chapter}{General Uses}

\loop\advance\tempcount by 1
\section{This is}
section \the\tempcount
\ifnum\tempcount<30
\repeat

\chapter*{Particular uses}
\addcontentsline{toc}{chapter}{Particular Uses}

\loop\advance\tempcount by 1
\section{This is}
section \the\tempcount
\ifnum\tempcount<37
\repeat

\chapter{Verbs governing two Accusatives}

\loop\advance\tempcount by 1
\section{This is}
section \the\tempcount
\ifnum\tempcount<41
\repeat

\chapter{The Causal}

\loop\advance\tempcount by 1
\section{This is}
section \the\tempcount
\ifnum\tempcount<49
\repeat

\chapter{The Instrumental Case}

\chapter*{General uses}
\addcontentsline{toc}{chapter}{General Uses}

\loop\advance\tempcount by 1
\section{This is}
section \the\tempcount
\ifnum\tempcount<54
\repeat

\chapter*{Particular uses}
\addcontentsline{toc}{chapter}{Particular Uses}

\loop\advance\tempcount by 1
\section{This is}
section \the\tempcount
\ifnum\tempcount<59
\repeat

\chapter{The Dative Case}

\chapter*{General uses}
\addcontentsline{toc}{chapter}{General Uses}

\loop\advance\tempcount by 1
\section{This is}
section \the\tempcount
\ifnum\tempcount<65
\repeat

\chapter*{Particular uses}
\addcontentsline{toc}{chapter}{Particular Uses}

\loop\advance\tempcount by 1
\section{This is}
section \the\tempcount
\ifnum\tempcount<71
\repeat

\chapter{The Ablative Case}

\chapter*{General uses}
\addcontentsline{toc}{chapter}{General Uses}

\loop\advance\tempcount by 1
\section{This is}
section \the\tempcount
\ifnum\tempcount<75
\repeat

\chapter*{Particular uses}
\addcontentsline{toc}{chapter}{Particular Uses}

\loop\advance\tempcount by 1
\section{This is}
section \the\tempcount
\ifnum\tempcount<86
\repeat

\chapter{The Locative Case}

\chapter*{General uses}
\addcontentsline{toc}{chapter}{General Uses}

\loop\advance\tempcount by 1
\section{This is}
section \the\tempcount
\ifnum\tempcount<92
\repeat

\chapter*{Particular uses}
\addcontentsline{toc}{chapter}{Particular Uses}

\loop\advance\tempcount by 1
\section{This is}
section \the\tempcount
\ifnum\tempcount<100
\repeat

\end{document}

Note: the trick to erase previous things by putting a colored box on it is not perfect; it seems to depend then on the PDF viewer used. In Acrobat, ok, but on mac os, with Skim the dots survive, although faintly. Probably some transparency problem, I think I have seen it discussed elsewhere.

Actually using \fboxsep1pt and not \fboxsep0pt before \colorbox solves this. I am not sure I understand why it did not work already with \fboxsep0pt.

Anyway the outcome, un-trimmed to compare to the OP's original:

fine tuned TOC