# add prefix to toc numbering

I add a prefix to the page number of my appdix by

\documentclass[a4paper,oneside]{memoir}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\copypagestyle{apx}{plain}
\makeevenfoot{apx}{}{Apx. \thepage}{}
\makeoddfoot{apx}{}{Apx. \thepage}{}
\copypagestyle{chapter}{plain}
\makeevenfoot{chapter}{}{Apx. \thepage}{}
\makeoddfoot{chapter}{}{Apx. \thepage}{}
\pagestyle{apx}


Is there a way I can add the same prefix the numbering in toc ? or is there another method that dose both actions as one??

• Please add a fully working MWE. Also, I think some more explanation is necessary. I don't know what you are talking about here. Do you want to edit the way each section and such are printed int the table of contents? Have a look at titletoc in that case, or some simular package. – Runar Jun 22 '16 at 15:29
• @runartrollet: No, I don't think that titletoc is a good idea. memoir uses (tocloft, well somehow ;-)) and titletoc and tocloft do not go well together!) – user31729 Jun 22 '16 at 16:27

You have a couple of options here:

• Option 1

This is what you currently have: Updating the page styles for the Appendix to prepend Apx.  to \thepage as part of the headers/footers.

\addtodef{\appendix}{}{%
\copypagestyle{apx}{plain}%
\makeevenfoot{apx}{}{Apx. \thepage}{}%
\makeoddfoot{apx}{}{Apx. \thepage}{}%
\copypagestyle{chapter}{plain}%
\makeevenfoot{chapter}{}{Apx. \thepage}{}%
\makeoddfoot{chapter}{}{Apx. \thepage}{}%
\pagestyle{apx}%
}


This has the drawback of not being included in the ToC.

• Option 2

Updating \thepage entirely for the Appendix and adjusting the page width box for every ToC-related entry in Appendix.

\addtodef{\appendix}{}{%
\renewcommand{\thepage}{Apx.~\arabic{page}}%
}


This affects \pageref as well for entries in the ToC, which is essentially not a bad idea. However, you'll have different widths of the page number block in the ToC. Inconsistency is not always a good idea.

• Option 3

This combines Option 1 and part of 2 (while not updating \thepage).

\newcommand{\replacecontentsline}[3]{\oldcontentsline{#1}{#2}{Apx.~#3}}
\copypagestyle{apx}{plain}%
\makeevenfoot{apx}{}{Apx. \thepage}{}%
\makeoddfoot{apx}{}{Apx. \thepage}{}%
\copypagestyle{chapter}{plain}%
\makeevenfoot{chapter}{}{Apx. \thepage}{}%
\makeoddfoot{chapter}{}{Apx. \thepage}{}%
\pagestyle{apx}%
\let\protect\contentsline\protect\replacecontentsline}%
}


References using \pageref will still point to a page number that doesn't include Apx. , which is not what the end-user expects.

• Option 4

My preferred option which updates \thepage (and therefore also \pagerefs) as well as set some consistent page numbering block width for the ToC.

\setpnumwidth{4em}%
\copypagestyle{apx}{plain}%
\makeevenfoot{apx}{}{\thepage}{}%
\makeoddfoot{apx}{}{\thepage}{}%
\pagestyle{apx}%
\renewcommand{\thepage}{Apx.~\arabic{page}}%
}


Here is a minimal example using option 4 above:

\documentclass[oneside]{memoir}

\setpnumwidth{4em}%
\copypagestyle{apx}{plain}%
\makeevenfoot{apx}{}{\thepage}{}%
\makeoddfoot{apx}{}{\thepage}{}%
\pagestyle{apx}%
\renewcommand{\thepage}{Apx.~\arabic{page}}%
}

\usepackage{lipsum}

\begin{document}

\sloppy% Just for this example
\tableofcontents

\chapter{A chapter}\lipsum[1-50]
\section{A section}\lipsum[1-50]

\clearpage\appendix

\chapter{An appendix}\lipsum[1-50]
\section{A section}\lipsum[1-50]

\end{document}


If you want the page number itself to be recorded as Apx. #, then you shouldn't edit the page style. Instead you should just redefine \thepage.

\documentclass[a4paper,oneside]{memoir}

\begin{document}

\tableofcontents

\chapter{Test}

\clearpage %So that the new \thepage will not leak to the previous chapter.

\appendix
\renewcommand\thepage{Apx. \arabic{page}}
\chapter{Test2}

\end{document}


If you do this you probably also want to also want to use \setpnumwidth to make extra space for the page numbers (now including the prefix Apx) in your ToC.

Since you are going to change the format of the page number in the appendix, in my opinion you should restart the page numbering also. Therefore, I do it this way in a set of macros that I use for maintaining a labbook in LaTeX:

\makeatletter
\def\apxpagenumbering#1{%
\global\c@page \@ne%  page=1
\gdef\thepage{Apx. \csname @#1\endcsname \c@page}%
}
\makeatother


Now, when you start the appendix, you issue a \apxpagenumbering{<nuber format>} and you can select the usual number formats, arabic, roman, etc.

As explained in the other answers, you will have to adjust the widths of the table of contents, because the default is only 1.55em which is not enough and these longer page numbers will extent the right margin. However, then the leading dots in the previous sections with simple numbers, will be too far to the left.

To avoid this, what you can do is to change these widths just at the point where the appendix starts. This is done with:

\makeatletter
\makeatother


Now we get:

But this is not the whole story, since now you will loose the entries in the index. When the ind file is constructed from the idx file, everything that is not only a page number will be discarded. This has no simple solution. The only way that I have found is to use makeidx and compile the document four times. With this setup you can not use imakeidx because then the index is updated every run and this will not work. The \index entries must then be specified in the form of \index{<entry>|<cmd>} and the \cmd must be properly defined.

What we do is the following:

1. run LaTeX without prefix
2. run makeidx
3. run LaTeX with prefix two times (second for updating the toc)

Now we will get it right:

And to make it all easier, some macros are defined for this so that we only have to enter the command twice, empty for making the index and with the prefix for the rest. Now that we have reached so far, the best is to add an automatic measurement of the prefix so that we can change it for whatever we want.

The complete code is:

\documentclass{article}
\usepackage{pgfmath}
\usepackage{makeidx}\makeindex

\usepackage{lipsum}

\newlength{\apxpnumwidth}
\newlength{\apxtocrmarg}
\newlength{\apxpgpfxwidth}
\setlength{\apxpgpfxwidth}{0pt}

\makeatletter

\newcommand{\apxpageprefix}[1]{
\ifx\relax#1\relax
\def\apxpgpfx{}
\else
\def\apxpgpfx{\protect#1}
\fi
\settowidth{\apxpgpfxwidth}{\apxpgpfx}
\pgfmathsetlength{\apxpnumwidth}{\the\apxpgpfxwidth+1.55em}
\pgfmathsetlength{\apxtocrmarg}{\the\apxpnumwidth+1em}
}

\newcommand{\apx}[1]{\apxpgpfx#1}
\def\apxpagenumbering#1{%
\global\c@page \@ne%  page=1
\gdef\thepage{\apxpgpfx\csname @#1\endcsname \c@page}%
}

\newcommand\apxtocwidths{%
}

\makeatother

\newcommand\apxindex[1]{\index{#1|apx}}
\apxpageprefix{\textit{Apx.}\hspace{0.1em}--}
%\apxpageprefix{}

\begin{document}

\tableofcontents

\section{A section}\lipsum[1-5]
\index{in main}

\subsection{A subsection}\lipsum[1-5]

\clearpage\appendix

\apxpagenumbering{arabic}
\apxtocwidths

\section{An appendix}\lipsum[1-5]
\apxindex{in appendix}

\subsection{A subsection}\lipsum[1-5]

\printindex

\end{document}


You have to uncomment %\apxpageprefix{} and run LaTeX and makeidx. Then you comment it again and run LaTeX with the prefix two times.

Now you can have everything you like as prefix. Just for fun I made a picture (with \usepackage{tikz}) for my labbook section with:

\newcommand{\erlenmeyer}{%
\begin{tikzpicture}[scale=0.3]
\fill[blue!70!white] (1,0)--(0.76,0.50)--(0.24,0.50)[rounded corners=0.5mm]--
(0,0)--cycle;
\draw[rounded corners=0.5mm] (0,0)--(1,0)--(0.65,0.8)--(0.65,1.2)--(0.66,1.2)
--(0.34,1.2)--(0.35,1.2)--(0.35,0.8)--cycle;
\draw[very thin,fill=white] (0.22,0.2) rectangle (0.45,0.35);
\draw[very thin] (0.55,0.15)--(0.65,0.15) (0.55,0.35)--(0.65,0.35)
(0.55,0.65)--(0.65,0.65);
\end{tikzpicture}}


Obtaining: