2

I need to create something similar to Table of Contents manually. For that I need dot leaders to go from text of the "chapter" to page number of this "chapter". I tried to use either \dotfill, \leaders, \cleaders or \xleaders trying to achieve it.

With \dotfill I get this:

Dots alignment

I want to align all dot leaders to the same point as "shortest" leaders row that lead to 3 digit number (130). On image you can see that those leaders are aligned to different point depending on:

  1. how many digits are at the end of the line
  2. length of text preceeding leaders even if number at the end of the line is the same

Second issue seems to get a bit better by replacing \dotfill to \leaders, but it is still present (unless it's just my eyes playing tricks on me), but using \leaders does not solve first issue:

\leaders alignment

I imagine that I could achieve perfect alignment by using something like tabular, but if at all possible I would like to keep it simple, without need for dealing with table.

Here is MWE showing everything I have tried so far, with commented \mypara definitions being alternatives which I tested:

\documentclass{book}

\newcommand\myleaders{\leavevmode\kern3pt\leaders\hbox{.}\hfill\kern6pt}
\newcommand\mycleaders{\leavevmode\kern3pt\cleaders\hbox{.}\hfill\kern6pt}
\newcommand\myxleaders{\leavevmode\kern3pt\xleaders\hbox{.}\hfill\kern6pt}

% \def \mypara#1#2{\noindent#1~~\dotfill~~#2}
\def \mypara#1#2{\noindent#1\myleaders#2}
% \def \mypara#1#2{\noindent#1\mycleaders#2}
% \def \mypara#1#2{\noindent#1\myxleaders#2}

\def \mycomment#1{\noindent\textbf{#1}:}

\begin{document}

\mycomment{1, 2 or 3 digits}

\mypara{This is a text}{1}

\mypara{This is a text}{30}

\mypara{This is a text}{130}

\mycomment{Different text}

\mypara{This is just short text}{30}

\mypara{This is a little bit longer text}{30}

\mypara{This is quite long text that goes on and on}{30}

\mycomment{1, 2 or 3 digits combined with different text}

\mypara{This is just short text}{1}

\mypara{This is a little bit longer text}{30}

\mypara{This is quite long text that goes on and on}{130}

\end{document}

How can I choose where \leaders or \dotfill ends regardless of how many digits are there at the end of the line?

1
  • 3
    Set the number in a box of predetermined width.
    – egreg
    Commented Jan 15, 2016 at 9:43

1 Answer 1

4

As was suggested to you in the comments by egreg, you can enclose the number in a box with a set width so that regardless of its contents, the leaders will always stop at that distance.

So rewriting your command like this:

\def\mypara#1#2{\noindent#1\myleaders\makebox[3mm][r]{#2}}

yields the following result

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .