# Elements of TOC styling using memoir class

I have yet another (baffling) question. Suppose I have the figure attached

The question is how can I move the chapter numbers to align with the first line of the chapter title ? i.e using the figure, I'd like to move 11 to align with chapter 3 and 9 to align with chapter 2.

I appreciate all the help I get from here. Makes me understand faster this business of typesetting with LaTeX. By the way, I use Memoir class.

-
Interesting question! I guess the desired style is very unusual. I suggest: have a quick look into the TOCs of 10 good books to check if anyone ever typesets the page number above and right of the last word of the title instead of following on the same line. If you are still convinced, we could work it out. Page 3 resp. figure 1 of the tocloft documentation shows a typical TOC entry layout. –  Stefan Kottwitz Mar 2 '11 at 17:39
One "way" to do this is to make sure your chapter titles are short and don't wrap. :-) You can do this with the optional argument of the \chapter command. –  Alan Munn Mar 2 '11 at 19:29
You can have a look at this answer of Herbert; it should be possible to solve your problem in a similar way (as suggested by daleif). –  Hendrik Vogt Mar 3 '11 at 13:08

## Solution using titletoc

I find that the titletoc package provides the best options for customization — as long as you really know what you want! Here is an implementation for the chapter entry:

\documentclass{memoir}
\usepackage{titletoc}

% This command "floats" the page number to the far right of the current line.
\newcommand\floatcontentspage{%
\hbox to 0pt{%
\hspace{-\leftskip}\hspace{\textwidth}\hspace{-\rightskip}%
\textbf{\contentspage}}%
}
\titlecontents
{chapter}                % 1. <section>
[0pt]                    % 2. <left> indent (none here)
{}                       % 3. <above code>: i.e. vertical space
{%                       % 4. <numbered-entry-format> for numbered chapters
\contentspush{\floatcontentspage%
\textbf{\chaptername~\thecontentslabel}%
\hspace{1.5em}}}
{}                       % 5. <numberledd-entry-format> for unnumbered chapters
{}                       % 6. <filler-page-format> no filler needed
[\vspace{\baselineskip}] % 7. <after code>: i.e. vertical space
\begin{document}

% I am not exactly sure what I should do here, but \chapternumberline has
% \@chapapp@head which is undefined when I use titletoc... This is an
% unexplained hack to get it working
\renewcommand\chapternumberline[1]{\numberline{#1}}

\tableofcontents*
\chapter{First chapter}
\chapter{Second chapter, in which many wonderful things happen, and then other things.}
\end{document}


## Explanation:

I first define the command \floatcontentspage:

\newcommand\floatcontentspage{%
\hbox to 0pt{%
\hspace{-\leftskip}\hspace{\textwidth}\hspace{-\rightskip}%
\textbf{\contentspage}}%
}


This is intended to be inserted at the start of a contents line after the left indent (of \leftskip). It typesets its contents in a box of width 0pt so that it does not affect the rest of the line. I first insert some space to total \textwidth - \leftskip - \rightskip to obey the current margins, then set the page number using the \contentspage command which I have wrapped in \textbf{} to match the style of the original poster. This will push the page number out to the right after the inserted space.

I then use the \titlecontents command to define the formatting of the contents line. It takes arguments in the following order:

1. {chapter} specifies the section type.
2. [0pt] is the optional left indent (none here: you would probably want this for section etc.).
3. {} There is no above code (vertical space).
4. This is the main entry for numbered chapters discussed in a moment.
5. {} I have not defined anything for unnumbered chapters.
6. {} There is no filler-page-format since we have already typeset the pagenumber.
7. [{\vspace{\baselineskip}] I have added some space after the chapter entry. (You might like to do this before instead.)

The work is done by argument 4.:

  {%                       % 4. <numbered-entry-format> for numbered chapters
\contentspush{\floatcontentspage%
\textbf{\chaptername~\thecontentslabel}%
\hspace{1.5em}}}


This calls \contentspush which inserts some text and then indents the remaining lines accordingly. We insert our \floatcontentspage command first which will float the page number to the right but take no space, then we insert the \chaptername and \thecontentlabel (in \textbf{}) which gives "Chapter 1" etc. Finally we insert some space to separate the title from the number.

Finally, I had to redefine \chapternumberline to call \numberline: I do not understand exactly why, but \chapternumberline uses \@chapapp@head which is undefined...

As you can see, the titletoc package allows for great flexibility, but requires that you know exactly what you want, and you generally must define all of your entries (I have only provided the chapter definition here. How do you want the sections defined?) Provided that you have a clear idea, though, it is the best tool in my opinion.

-

As the desired style is rather unusual, there is no simple solution. You'll have to recode the \l@chapter command placing the title inside a \parbox that is aligned at the top line. Then it should be doable.

-
Nice to have here at tex.sx! I took the freedom to edit your answer to make it a bit clearer, OK? –  Hendrik Vogt Mar 3 '11 at 13:04

You still have to rewrite \l@chapapp, using an overlapping box or \rlap. I would not us this, because it is realy ugly :-)

\documentclass{memoir}
\usepackage{calc}
\renewcommand*{\cftchaptername}{\chaptername~}
\renewcommand*{\cftappendixname}{\appendixname~}
\renewcommand*{\cftchapteraftersnum}{.}
\setlength{\cftchapternumwidth}{2em}

\makeatletter
\renewcommand*{\l@chapapp}[3]{%
\ifnum \c@tocdepth >\m@ne
\cftchapterbreak
\vskip \cftbeforechapterskip
{\memRTLleftskip \cftchapterindent\relax
\memRTLrightskip \@tocrmarg
\parfillskip -\memRTLrightskip
\parindent \cftchapterindent\relax
\@afterindenttrue
\interlinepenalty\@M
\leavevmode
\let\@cftbsnum \cftchapterpresnum
\let\@cftasnum \cftchapteraftersnum
\let\@cftasnumb \cftchapteraftersnumb
\makebox[0pt][l]{\makebox[\linewidth-\@tempdima][r]{\myformatpnum{#2}}}%<<<NEW LINE
\null\nobreak\hskip -\memRTLleftskip
{\cftchapterfont #1}\nobreak
\cftchapterfillnum{#2}}%
\fi}

\let\myformatpnum\cftchapterformatpnum
\renewcommand\cftchapterformatpnum[1]{}

\makeatother

\begin{document}
\frontmatter
\tableofcontents
\clearpage
\mainmatter

\chapter{This is the first chapter}
\chapter{This is the second chapter, This is the second chapter, This is the second chapter}
\chapter{This is the third chapter}
\chapter{This is the forth chapter, This is the forth chapter, This is the forth chapter}
\appendix

\chapter{Appendix one}
\end{docume

-
nice one. I'm actually a but lost here. Where does the indentation in front of the outer make box come from? The leftskip isn't altered until the line after it. –  daleif Mar 3 '11 at 19:43
@daleif Strange indeed. Looks if the box output is delayed until the paragraph is set (including the glues). I have changed the above code by moving the \makebox after the \leftskip setting. This is technically more correct i hope. –  Danie Els Mar 5 '11 at 4:07
I'm tempted to add hooks to be able to do this. I actually have several undocumented hooks in the memoir TOC code, to be able to do interesting things like measuring stuff. I'll document them at some point. –  daleif Mar 6 '11 at 14:45

Here's a minimal example that shows the problem, but I don't have a solution yet:

\documentclass{memoir}
\begin{document}
\tableofcontents*
\chapter{First chapter, in which many wonderful things happen. And then other things.}
\chapter{Second chapter, in which many wonderful things happen. And then other things.}
\end{document}


Memoir section 9.2 has the ToC methods given, and says the code for typesetting a numbered chapter is more or less

{\cftchapterfont {\cftchaptername \cftchapterpresnum SNUM\cftchapteraftersnum\hfil} \cftchapteraftersnumb TITLE}


with substitutions made for the chapter number as SNUM, chapter title as TITLE, and the page number as PAGE.

The default definition for \cftchapterformatpnum should be something like

\makeatletter
\renewcommand*{\cftchapterformatpnum}[1]{%
\hbox to \@pnumwidth{\hfil{\cftchapterpagefont #1}}}
\makeatother


but since the page number is limited to a box on the regular baseline, I don't know if there's an easy way of moving up a few lines before typesetting that box.

But maybe this will narrow down the problem enough for someone else to post a solution.

-