# Variable memoir TOC page number box size

My book long front matter page numbers which were sticking out into the margin. Following the memoir documentation, it was easy to remedy this, as in the MWE which follows.

The documentation (§9.2.2) says that:

"the length used in \setrmarg should be greater than the length set in \setpnumwidth. These values should remain constant in any given document".

It makes sense not to have all kinds of variation, but this means that in my case at least, many pages of TOC have a huge pnumwidth (and thus sacrifice width for the entries) due to a few lines of long Roman numerals.

I tried a smaller setrmarg, and this does allow for longer entries, but it doesn't seem to affect the dots, which still stop before the page number box.

So what I would like to do is to restrict the increased pnumwidth to the front matter (which, coincidentally, does not have dots).

MWE:

 \documentclass[%
a4paper,
11pt,
twoside,
one column,
openright
]{memoir}

\setpnumwidth{3.55em}
\setrmarg{5.55em}

\begin{document}
\frontmatter
\setcounter{page}{33}
\tableofcontents
\cleardoublepage

\listoffigures
\cleardoublepage

\listoftables
\cleardoublepage

\mainmatter
\renewcommand{\thefigure}{\arabic{chapter}--\arabic{figure}}
\renewcommand{\thetable}{\arabic{chapter}--\arabic{table}}
\section{Section}
The section.
\begin{table}
\caption{Table}
Table contents
\end{table}

\end{document}


EDIT: I tried the first approach, putting the \resetpnum right after \mainmatter, and the result was peculiar: the box did get reset, but not until a chapter later (in my main file, with multiple chapters and appendices.

Second EDIT: using the longer solution involving \etoolbox, with the same result. Lines from the .toc follow:

\contentsline {subsection}{\numberline {1.10.4}Remarks regarding presentation}{20}{subsection.1.10.4}
\defcounter {refsection}{0}\relax
\def \@pnumwidth {0.55em}\def \@tocrmarg {1.55em}
\defcounter {refsection}{0}\relax
\contentsline {chapter}{\chapternumberline {2}Larsa/Senkereh}{21}{chapter.2}


EDIT3 (apparent solution): Having noted some remarks in the memoir documentation, section 9.2, page 156 (version v3.7f, 2016/05/16), I tried the code below, which seemed work:

...
\frontmatter
\setcounter{page}{5}% as per Olms format requirements
\cftlocalchange{toc}{3.55em}{5.55em}
\tableofcontents
\cleardoublepage

\listoffigures
\cleardoublepage

\listoftables
\cftlocalchange{toc}{1.55em}{2.55em}
\cleardoublepage
...

• As far as I remember, there is no per type interface for this. But we can probably hack something a bit more manually. Where exactly do you want two different rmargs? Just one in the frontmatter and another in the rest of the doc (where the page numbers are more similar?) Jan 28 '18 at 16:39
• @daleif Yes, exactly so. The front matter has the long roman numerals, everything else can be one measure (so far, the default is fine). The current max is around 500 and it's not going to reach 1000.... Jan 28 '18 at 17:43
• Not at pc, I'll add a suggestion tomorrow Jan 28 '18 at 17:43
• @daleif Great. I can't work on this today either, anyway. When I get back to it it occurred to me I could try the 'brute force' approach and prepend to the figure list, table list, etc., but even if it works that's a bit crude.... Jan 28 '18 at 18:57
• We can add hooks to the toc, just have to do it in the right way Jan 28 '18 at 19:15

Here is probably how I would do it. Using \cftlocalchange, is one way, but this leaves the values used in the body of the text, I like to keep layout stuff in the preamble (or in a separate package).

Instead I use memoirs build in toc hook system to insert hooks (macros) to be executed from within the toc. Note that this means that we cannot use \cftlocalchange as it writes to the toc, and the hook runs from within the toc. Thus we simply do by hand, what \cftlocalchange would add to the toc.

Note that these changes are global in this case. I you do not want this, place \tableofcontens inside a group.

\documentclass[%
a4paper,
11pt,
twoside,
one column,
openright
]{memoir}

% define what the hooks do
% we cannot use \cftlocalchange as this hook is added inside the toc
% and \cftlocalchange writes to the toc
\cftinsertcode{FRONTMATTER}{%
\protect\setpnumwidth{3.55em}
\protect\setrmarg{5.55em}
}

\cftinsertcode{MAINMATTER}{%
\protect\setpnumwidth{2.55em}
\protect\setrmarg{3.55em}
}

\begin{document}

\cftinserthook{toc}{FRONTMATTER}
\frontmatter
\setcounter{page}{33}
\tableofcontents

\chapter{Test in frontmatter}

\section{Test}

\cleardoublepage

\listoffigures
\cleardoublepage

\listoftables
\cleardoublepage

\cftinserthook{toc}{MAINMATTER}
\mainmatter
\renewcommand{\thefigure}{\arabic{chapter}--\arabic{figure}}
\renewcommand{\thetable}{\arabic{chapter}--\arabic{table}}
\section{Section}
The section.
\begin{table}
\caption{Table}
Table contents
\end{table}

\end{document}

• I have just road-tested this and it works fine, much appreciated. I still don't know why Jon's original solutions only half-worked, but I suspect that it would take a lot of work to generate a somewhat minimal example to reproduce the secondary problem and a lot of time from you folk. So I'm content to accept this for now and perhaps the other will become clearer as I learn. Jan 29 '18 at 15:45

The key is to write to the .toc at the point you want to change \@pnumwidth and \@tocrmarg. You could do it like this:

Create the command \resetpnum:

\newcommand\resetpnum{%
% I think these are the default values
\string\def\string\@pnumwidth{1.55em}%
\string\def\string\@tocrmarg{2.55em}%
}}


Then you either issue \resetpnum where you want to, or use etoolbox to append the command to something like \mainmatter:

\usepackage{etoolbox}
\appto\mainmatter{\resetpnum}


Full example:

\documentclass[%
a4paper,
11pt,
twoside,
one column,
openright
]{memoir}

\setpnumwidth{3.55em}
\setrmarg{5.55em}

\newcommand\resetpnum{%
\makeatletter
\string\def\string\@pnumwidth{0.55em}%
\string\def\string\@tocrmarg{1.55em}%
}
\makeatother
}

\usepackage{etoolbox}
\appto\mainmatter{\resetpnum}

\begin{document}
\frontmatter
\setcounter{page}{33}
\tableofcontents
\cleardoublepage

\listoffigures
\cleardoublepage

\listoftables
\cleardoublepage

\mainmatter

\renewcommand{\thefigure}{\arabic{chapter}--\arabic{figure}}
\renewcommand{\thetable}{\arabic{chapter}--\arabic{table}}
\section{Section}
The section.
\begin{table}
\caption{Table}
Table contents
\end{table}

\end{document}


Produces a .toc file that looks like this:

\contentsline {chapter}{Contents}{xxxiii}
\contentsline {chapter}{List of Figures}{xxxv}
\contentsline {chapter}{List of Tables}{xxxvii}
\def \@pnumwidth {0.55em}\def \@tocrmarg {1.55em}
\contentsline {section}{\numberline {0.1}Section}{1}

• I'll give this a try! Jan 28 '18 at 14:13
• The first solution did not quite work, see edit above. Will have to try the other one. Jan 28 '18 at 15:34
• The second, fuller solution, didn't work either, same result as shown in the edit. Of course, I'm trying this on my actual (very complicated) file, not the MWE. It works fine on the MWE. I'm adding another edit with lines from the .toc file which match the visual result. I can't imagine what is causing the 'delayed reaction'. Jan 28 '18 at 15:53
• Next tried a \preto\chapter instead of the \appto and had the reverse problem: the chapters were ok but the shorthand list (last front matter item) was hanging into the margin again. Clearly there's something more to the sequence of events here, but I don't know how to hook into the correct chronological sequence.... Jan 28 '18 at 16:22
• Hmm, well, as you note, it works as expected on the example you've provided. And, according to the snippet of the .toc you've provided, it is getting written to the .toc as expected. I think your example is, in this case, too minimal. Can you expand on it some? The command \addtocontents{toc}{<text>} writes to the .toc at exactly the place it is called (as you can probably tell)....
– jon
Jan 28 '18 at 19:05