How to align consecutive \leaders properly?

I’m trying to build a construct with a box of a fixed filled with \leaders followed by \leaders with \hfill. However at the moment there is a little gap between these two making the pattern irregular, but I want equidistant symbols for the whole line. In the following MWE you'll see the problem in (output) line: The gap between :::: and ..... is a bit too large (red arrows). Also the distance between A and the following leaders is different depending on the presence of the \hbox (green arrows).

It gets better with \cleaders instead of \leaders (as shown in the lower four lines).

\documentclass{article}

\newlength{\dotskip}
\setlength{\dotskip}{1mm}

\begin{document}

A \hbox to 20\dotskip {\leaders\hbox to \dotskip {\hss:\hss}\hfill}B

A \leaders\hbox to \dotskip {\hss:\hss}\hfill B

A \leaders\hbox to \dotskip {\hss.\hss}\hfill B

A \hbox to 5\dotskip {\leaders\hbox to \dotskip {\hss:\hss}\hfill}%
\leaders\hbox to \dotskip {\hss.\hss}\hfill B

\bigskip
A \hbox to 20\dotskip {\cleaders\hbox to \dotskip {\hss:\hss}\hfill}B

A \cleaders\hbox to \dotskip {\hss:\hss}\hfill B

A \cleaders\hbox to \dotskip {\hss.\hss}\hfill B

A \hbox to 5\dotskip {\cleaders\hbox to \dotskip {\hss:\hss}\hfill}%
\cleaders\hbox to \dotskip {\hss.\hss}\hfill B

\end{document}


Rephrased question

How can I align \leaders contained in an \hbox (colons in the example) with those lying on the “global” grid of \leaders outside of an \hbox (periods in the example).

\documentclass[a5paper]{scrbook}

\usepackage[right=50mm,marginparsep=5mm,marginparwidth=40mm,left=10mm]{geometry}
\usepackage{xcolor}
\setlength{\parindent}{0pt}
\newcommand{\margbox}[1]{%
\unskip\marginpar{%
\itshape
\tiny
#1\par
}\ignorespaces
}

\newlength{\dotskip}
\setlength{\dotskip}{3mm}

\begin{document}

{\tiny\itshape
\textcolor{green}{left edge of leader hbox grid box}\qquad
\textcolor{red}{middle of box}\qquad
\textcolor{blue}{right edge of box}
\par}

\kern0pt
\strut\leaders\hbox to \dotskip {%
\smash{\color{green}\vrule height 1ex depth 100mm width 0.15pt}%
\hss
\smash{\color{red}\vrule height 1ex depth 100mm width 0.3pt}%
\hss
\smash{\color{blue}\vrule height 1ex depth 100mm width 0.15pt}%
}\hfill\strut
\normalcolor

\minisec{\texttt{\string\leaders}}
A \hbox to 10\dotskip {\leaders\hbox to \dotskip {\hss:\hss}\hfill}B
\margbox{fixed width hbox}

A \leaders\hbox to \dotskip {\hss.\hss}\hfill B
\margbox{hfill}

A \hbox to 5\dotskip {\leaders\hbox to \dotskip {\hss:\hss}\hfill}%
\leaders\hbox to \dotskip {\hss.\hss}\hfill B
\margbox{fixed width hbox + hfill}

AAAA \hbox to 5\dotskip {\leaders\hbox to \dotskip {\hss:\hss}\hfill}%
\leaders\hbox to \dotskip {\hss.\hss}\hfill B
\margbox{fixed width hbox + hfill}

\minisec{\texttt{\string\cleaders}}
A \hbox to 10\dotskip {\cleaders\hbox to \dotskip {\hss:\hss}\hfill}B
\margbox{fixed width hbox}

A \cleaders\hbox to \dotskip {\hss.\hss}\hfill B
\margbox{hfill}

A \hbox to 5\dotskip {\cleaders\hbox to \dotskip {\hss:\hss}\hfill}%
\cleaders\hbox to \dotskip {\hss.\hss}\hfill B
\margbox{fixed width hbox + hfill}

AAAA \hbox to 5\dotskip {\cleaders\hbox to \dotskip {\hss:\hss}\hfill}%
\cleaders\hbox to \dotskip {\hss.\hss}\hfill B
\margbox{fixed width hbox + hfill}

\minisec{\texttt{\string\leaders}}
A \hbox to 10\dotskip {\xleaders\hbox to \dotskip {\hss:\hss}\hfill}B
\margbox{fixed width hbox}

A \xleaders\hbox to \dotskip {\hss.\hss}\hfill B
\margbox{hfill}

A \hbox to 5\dotskip {\xleaders\hbox to \dotskip {\hss:\hss}\hfill}%
\xleaders\hbox to \dotskip {\hss.\hss}\hfill B
\margbox{fixed width hbox + hfill}

AAAA \hbox to 5\dotskip {\xleaders\hbox to \dotskip {\hss:\hss}\hfill}%
\xleaders\hbox to \dotskip {\hss.\hss}\hfill B
\margbox{fixed width hbox + hfill}

\end{document}


The example starts with some colored lines showing the “global” grid (red line = middle of the boxes on the grid). And then the different results of \leaders, \cleaders and \xleaders. In fact I wan’t the behaviour of \leaders, i.e. stick to a grid, but it seems like an \hbox gets its own grid instead of sticking to the global one.

• I asked this question, because I wanted to prepend a flexible width leaders box with a fixed one to make sure that there is always a minimum of leaders. In the meantime I found out that this can easily be don by replacing \hfill with \hskip 1em plus 1fill. However for me the (rephrased) question is still interesting to learn more about the functionality of leaders.
– Tobi
Jul 18, 2018 at 10:32
• LuaTeX provides a \gleaders (where g stands for global) command which aligns the symbols on the largest enclosing box. Jul 18, 2018 at 11:42
• @Metafox: Thanks for the tip. do you know of an äquivalent for pdfLaTeX? However you may consider making an answer out of your comment ;-)
– Tobi
Jul 18, 2018 at 11:57

2 Answers

First off, only \leaders guarantee alignment across different lines.

However, \leaders are guaranteed to be aligned when they share the enclosing box level.

Consider the following plain TeX example:

\parindent=0pt

\def\lb{\hbox{\kern1mm\hss.\hss\kern1mm}}

\hbox to 4cm{\leaders\lb\hfil}

\hbox{\kern3.7mm\hbox to 3.63cm{\leaders\lb\hfil}}

\vtop{
\leavevmode\leaders\lb\hskip4cm\kern0pt\par
\leavevmode\kern3.7mm\leaders\lb\hskip3.63cm\kern0pt\par
\leavevmode\hbox to 4cm{\leaders\lb\hskip4cm}\par
\leavevmode\hbox to 4cm{\kern3.7mm\leaders\lb\hskip3.63cm}\par
}

\bye


As you can see, the first two rows of periods are not aligned, whereas the last four are.

• Thanks. thats what I thought, so the answer to my question is simply “No”, correct?
– Tobi
Jul 18, 2018 at 11:58
• @Tobi I'm afraid so. Jul 18, 2018 at 12:02
• Well a “no” is a valid answer and at the moment no problem, since I found another solution (see my comment under my question). Maybe you like to add an explicit “no” to your answer …
– Tobi
Jul 18, 2018 at 12:58

Ref: Heiko's excellent description at https://tex.stackexchange.com/a/253303/25858:

• \leaders sets the boxes at fixed grid position. It is intended for dots in the table of contents, for example. Then no matter, where the dots start, they are always vertically aligned.

• \xleaders calculates, how many boxes are needed to fill the space without overfilling. Then the space is divided in equal sized segments and the box is horizontally centered. The example below fills the space 20 pt with a box of width 7 pt. Thus two boxes fit in the space and the result is a "dashed" line.

• \cleaders. Since the "dash" effect of the previous version is sometimes not desirable, \cleaders moves the boxes in the middle and put the remaining unfilled space at both sides of the space to be filled.

The problem arises when the space to be filled is not an exact multiple of the leader-period. What to do? Grid align (\leaders), centered with excess space on the outside (\cleaders), or distribute excess space between cycles (\xleaders).

So, I changed all \leaders and \cleaders to \xleaders.

\documentclass{article}

\newlength{\dotskip}
\setlength{\dotskip}{1mm}

\begin{document}

A \hbox to 20\dotskip {\xleaders\hbox to \dotskip {\hss:\hss}\hfill}B

A \xleaders\hbox to \dotskip {\hss:\hss}\hfill B

A \xleaders\hbox to \dotskip {\hss.\hss}\hfill B

A \hbox to 5\dotskip {\xleaders\hbox to \dotskip {\hss:\hss}\hfill}%
\xleaders\hbox to \dotskip {\hss.\hss}\hfill B

\bigskip
A \hbox to 20\dotskip {\xleaders\hbox to \dotskip {\hss:\hss}\hfill}B

A \xleaders\hbox to \dotskip {\hss:\hss}\hfill B

A \xleaders\hbox to \dotskip {\hss.\hss}\hfill B

A \hbox to 5\dotskip {\xleaders\hbox to \dotskip {\hss:\hss}\hfill}%
\xleaders\hbox to \dotskip {\hss.\hss}\hfill B

\end{document}


• Thanks for your explanation, this seems to solve my problem and showed me that my question wasn’t precise enough … I rephrased it and edited my question: I actually want the leaders to stick to a grid.
– Tobi
Jul 18, 2018 at 10:30