Page LayoutEntries in TOC ragged right with page numbers on first line

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
Bram
Posts: 20
Joined: Sat Jul 14, 2012 12:54 pm

Entries in TOC ragged right with page numbers on first line

Post by Bram »

I have a publication with chapter titles running over several lines. I would like to have them in the table of contents ragged right, with the page numbers after te first line, not after the last line of the title.

So I'ld like to put page number 5 in the example below at the first line, something like:

1. This is a really long chapter title 5
and it continues here, although we
already gave the page number

..and get all the lines ragged right.

Any help would be appreciated!

This is my code:

Code: Select all

\documentstyle{memoir}
\begin{document}
\tableofcontents*
\chapter{This is a normal chapter}
\chapter{This is a chapter with a long title, but I can't really 
make it shorter, how hard I try}
\chapter{This is a chapter -- I hesitate to say it -- 
with an extreme long title, really, but the authors didn't want 
to have it curtailed, so I am afraid I will have to cope with it, 
although I will still try to make it shorter}
\chapter{This is not too bad again}
\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Entries in TOC ragged right with page numbers on first line

Post by cgnieder »

Hi,

the following is an adaption of a piece of code I found here. It partially solves your problem.

Code: Select all

\documentclass{memoir}
\usepackage{titletoc}

% This command "floats" the page number to the far right of the current line.
\newcommand\floatcontentspage{%
  \rlap{%
    \hspace{-\leftskip}\hspace{\linewidth}\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{\thecontentslabel}%
      \hspace{1.5em}}\bfseries\raggedright}
  {\bfseries}              % 5. <numberless-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{This is a normal chapter}

\chapter{This is a chapter with a long title, but I can't really
make it shorter, how hard I try}

\chapter{This is a chapter -- I hesitate to say it --
with an extreme long title, really, but the authors didn't want
to have it curtailed, so I am afraid I will have to cope with it,
although I will still try to make it shorter}

\chapter{This is not too bad again}
\end{document}
Regards
site moderator & package author
Bram
Posts: 20
Joined: Sat Jul 14, 2012 12:54 pm

Entries in TOC ragged right with page numbers on first line

Post by Bram »

Thanks a lot, this is really helpful and it already looks much more natural. The only problem remaining is that the titles are still not ragged right (which would be much more elegant). I found a nice solution with the package tocstyle, but is doesn't combine with your titletoc solution for the page numbers:

Code: Select all

\usepackage{tocstyle}
\usetocstyle{standard}
\settocfeature{raggedhook}{\raggedright}
I don't see immediately how to combine the two. Does anybody have a good idea?
Post Reply