3

\stretch{someFactor} is a generalization of the rubber-space command \hfill in the sense that \hfill is the special case \stretch{1.0}. (I'm actually more interested in the starred version \hspace*{\fill} rather than \hspace{\fill} version because I want to be able to force a space at the end of a line.)

\dotfill is an analog of \hfill in the sense that \dotfill fills with spaced dots the same horizontal space that \hfill would fill with empty space.

I would like to define a \stretchDotfill{someFactor} command that fills with spaced dots the same space that \stretch{someFactor} would fill with empty space.

Note that my question is not the same as "How to change dot spacing in \dotfill?." Changing the dot spacing does not affect the length of the space that \dotfill fills; it only changes the density of the dots within the same length of horizontal space.

I understand that the definition of \dotfill is:

\leavevmode \cleaders \hb@xt@ .44em{\hss .\hss }\hfill \kern \z@

I used \meaning\stretch and got, approximately:

macro:#1-¿\z@ \@plus #1fill\relax

(I'm using Overleaf and I don't fully trust the characters it gave me. E.g., it gave me instead of \. I fixed those but I'm not sure about the ¿.)

However, I don't know how to proceed further.

[Why do I want to do this? I'm writing a title-page banner builder, where there are discrete horizontal boxes within a row, and the person building the banner can use \stretch to fine tune the spacing between adjacent boxes (or between a box on one end and the nearer margin). I'd like a "debug mode" to make the \stretch space visible. I thought I'd do that with \dotfill if I can generalize \dotfill to accept a stretch factor. (If I were using only \hfill to control the spacing, I would use \dotfill for the debug mode.)]

1
  • 2
    if using meaning then use \texttt (or T1 encoding) if you use verbatim codes in roman OT1 encoding you see the ¿ and other artefacts of that encoding, Dec 19, 2018 at 17:09

1 Answer 1

4

If you try latexdef -s dotfill stretch you're answered with

> latexdef -s dotfill stretch
% latex.ltx, line 617:
\def\dotfill{%
  \leavevmode
  \cleaders \hb@xt@ .44em{\hss.\hss}\hfill
  \kern\z@}

% latex.ltx, line 1629:
\def\stretch#1{\z@ \@plus #1fill\relax}

Recall that \hfill is equivalent to \hskip 0pt plus 1fill\relax and the definition will appear instantly:

\makeatletter
\newcommand{\dotfillstretch}[1]{%
  \leavevmode
  \cleaders\hb@[email protected]{\hss.\hss}\hskip\z@\@plus #1fill
  \kern\z@
}
\makeatother

Test file:

\documentclass{article}

\makeatletter
\newcommand{\dotfillstretch}[1]{%
  \leavevmode
  \cleaders\hb@[email protected]{\hss.\hss}\hskip\z@\@plus #1fill
  \kern\z@
}
\makeatother

\begin{document}

\noindent X\dotfill X

\noindent
A\dotfillstretch{1}B\dotfillstretch{2}C\dotfillstretch{3}D

\end{document}

enter image description here

3
  • That's great, @egreg. You make it look easy! Your solution even has the desirable-to-me property of being like \hspace* (rather than the unstarred \hspace). It's not clear to me what part of your solution ensures that end-of-line spaces are eliminated. (Now that I see that \dotfill already has that property, I guess your method inherits it from \dotfill.) Dec 19, 2018 at 19:25
  • Correction: I meant "ensures that end-of-line spaces are NOT eliminated" Dec 19, 2018 at 20:12
  • 2
    The \kern\z@ at the end is the key.
    – egreg
    Dec 19, 2018 at 22:16

You must log in to answer this question.

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