Typeset dots around numbers (old typography)
\input expl3-generic % just for not using ConTeXt
\ExplSyntaxOn
\cs_set_eq:NN \dorecurse \prg_replicate:nn
\ExplSyntaxOff
\def\num#1{\unskip\adddot#1\adddot\ignorespaces}
\def\adddot{%
\cleaders\hbox to 1.5\dimexpr\fontdimen2\font-\fontdimen4\font\relax{\hss.\hss}%
\hskip1.5\fontdimen2\font plus \fontdimen3\font minus \fontdimen4\font
}
\dorecurse{100}{The \num{2} element \num{2} xy. }
\bye
If the period should be attached to the number, it's even simpler.
\input expl3-generic
\ExplSyntaxOn
\cs_set_eq:NN \dorecurse \prg_replicate:nn
\ExplSyntaxOff
\newbox\periodbox
\def\num#1{\leavevmode\addleftdot#1\addrightdot}
\def\addleftdot{%
\setbox\periodbox=\hbox{.}%
\cleaders\copy\periodbox\hskip\wd\periodbox\relax
}
\def\addrightdot{%
\setbox\periodbox=\hbox{.}%
\cleaders\copy\periodbox\hskip\wd\periodbox\relax
}
\dorecurse{100}{The \num{2} element \num{2} xy. }
\bye
The order in which you combine the two skips (the \leaders
and the non-\leaders
one) doesn’t matter: they will always vanish as a unit.
% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly
% declare the paper format.
\usepackage[T1]{fontenc} % Not always necessary, but recommended.
% End of standard header. What follows pertains to the problem at hand.
\makeatletter
\newsavebox\@NWD@dot@box
\newlength\@NWD@space@skip
\newcommand*\numberwithdots[1]{%
\unskip
\setbox\@NWD@dot@box \hbox{.}% use current font
\@NWD@space@skip
\fontdimen\tw@ \font
\@plus \fontdimen\thr@@ \font
\@minus \fontdimen 4 \font
\hskip \glueexpr \@NWD@space@skip-\wd\@NWD@dot@box
\cleaders \copy\@NWD@dot@box \hskip \wd\@NWD@dot@box
#1%
\cleaders \copy\@NWD@dot@box \hskip \wd\@NWD@dot@box
\hskip \glueexpr \@NWD@space@skip-\wd\@NWD@dot@box
\ignorespaces
}
\makeatother
\begin{document}
\numberwithdots{1} note that the indent does \emph{not} count as a ``begin of
line'': not sure if this is what is wanted. Some text \numberwithdots{2} that
includes \numberwithdots{3} some numbers: I~hope that \numberwithdots{4} some
\numberwithdots{5} of them will fall \numberwithdots{6} on a line boundary,
although \numberwithdots{7} it is \numberwithdots{8} always possible
\numberwithdots{9} to arrange things in order \numberwithdots{10} to make
\numberwithdots{11} this happen. And it \numberwithdots{12} is also
possible\linebreak[4] \numberwithdots{13} to force \numberwithdots{14} a line
\numberwithdots{15} break\linebreak[4] at a specific \numberwithdots{16}
position.
Two numbers in a row, though, do not \numberwithdots{123} \numberwithdots{456}
look well.
\end{document}
This is the output I get:
Addition
Perhaps, it would be preferable to use wider spaces around numbers with dots; and it would be even better to allow the user to customize this aspect.
% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly
% declare the paper format.
\usepackage[T1]{fontenc} % Not always necessary, but recommended.
% End of standard header. What follows pertains to the problem at hand.
\makeatletter
\newsavebox\@NWD@dot@box
\newlength\@NWD@space@skip
\newcommand*\@NWD@factor{1.5} % redefine default as you please
\newcommand*\numberwithdots[1]{%
\unskip
\setbox\@NWD@dot@box \hbox{.}% use current font
\@NWD@space@skip
\@NWD@factor\fontdimen\tw@ \font
\@plus \fontdimen\thr@@ \font
\@minus \fontdimen 4 \font
\hskip \glueexpr \@NWD@space@skip-\wd\@NWD@dot@box
\cleaders \copy\@NWD@dot@box \hskip \wd\@NWD@dot@box
#1%
\cleaders \copy\@NWD@dot@box \hskip \wd\@NWD@dot@box
\hskip \glueexpr \@NWD@space@skip-\wd\@NWD@dot@box
\ignorespaces
}
\newcommand*\nwdsetdotspacing[1]{\def\@NWD@factor{#1}}
\newcommand*\nwdlinebreak{\unskip\unskip\linebreak}
\makeatother
\begin{document}
\numberwithdots{1} note that the indent does \emph{not} count as a ``begin of
line'': not sure if this is what is wanted. Some text \numberwithdots{2} that
includes \numberwithdots{3} some numbers: I~hope that \numberwithdots{4} some
\numberwithdots{5} of them will fall \numberwithdots{6} on a line boundary,
although \numberwithdots{7} it is \numberwithdots{8} always possible
\numberwithdots{9} to arrange things in order \numberwithdots{10} to make
\numberwithdots{11} this happen. And it \numberwithdots{12} is also
possible\nwdlinebreak[4] \numberwithdots{13} to force \numberwithdots{14} a line
break \numberwithdots{15}\nwdlinebreak[4] at a specific \numberwithdots{16}
position.
Two numbers in a row, though, do not \numberwithdots{123} \numberwithdots{456}
look well.
\nwdsetdotspacing{} % empty argument amounts to 1
\numberwithdots{1} note that the indent does \emph{not} count as a ``begin of
line'': not sure if this is what is wanted. Some text \numberwithdots{2} that
includes \numberwithdots{3} some numbers: I~hope that \numberwithdots{4} some
\numberwithdots{5} of them will fall \numberwithdots{6} on a line boundary,
although \numberwithdots{7} it is \numberwithdots{8} always possible
\numberwithdots{9} to arrange things in order \numberwithdots{10} to make
\numberwithdots{11} this happen. And it \numberwithdots{12} is also
possible\nwdlinebreak[4] \numberwithdots{13} to force \numberwithdots{14} a line
break \numberwithdots{15}\nwdlinebreak[4] at a specific \numberwithdots{16}
position.
\nwdsetdotspacing{2}
\numberwithdots{1} note that the indent does \emph{not} count as a ``begin of
line'': not sure if this is what is wanted. Some text \numberwithdots{2} that
includes \numberwithdots{3} some numbers: I~hope that \numberwithdots{4} some
\numberwithdots{5} of them will fall \numberwithdots{6} on a line boundary,
although \numberwithdots{7} it is \numberwithdots{8} always possible
\numberwithdots{9} to arrange things in order \numberwithdots{10} to make
\numberwithdots{11} this happen. And it \numberwithdots{12} is also
possible\nwdlinebreak[4] \numberwithdots{13} to force \numberwithdots{14} a line
break \numberwithdots{15}\nwdlinebreak[4] at a specific \numberwithdots{16}
position.
\end{document}
Unfortunately, for a TeXnical reason, in order to force a line break, we also had to replace the \linebreak
command with our \nwdlinebreak
.
Output:
An additional degree of freedom: a factor also for the amount of backspacing. In addition to this, the following code excerpt presents an alternative, and more robust, implementation:
\makeatletter
% Alternative approach that permits nesting: use temporaries, but with grouping:
\newcommand*\@NWD@dot@factor{}
\newcommand*\@NWD@spc@factor{1.5}
\newcommand*\numberwithdots[1]{%
\unskip
\setbox\z@ \hbox{.}% use current font
\dimen@ \@NWD@dot@factor\wd\z@
\skip@
\@NWD@spc@factor\fontdimen\tw@ \font
\@plus \fontdimen\thr@@ \font
\@minus \fontdimen 4 \font
% More elegant with subroutines (or perhaps not):
\@NWD@nonleader@hskip \@NWD@leader@hskip
{#1}%
\@NWD@leader@hskip \@NWD@nonleader@hskip
\ignorespaces
}
\newcommand*\@NWD@nonleader@hskip{%
\hskip \glueexpr \skip@-\dimen@ \relax % for robustness
}
\newcommand*\@NWD@leader@hskip{%
\cleaders \copy\z@ \hskip \wd\z@
}
\newcommand*\nwdsetdotspacing[2][]{%
\def\@NWD@dot@factor{#1}%
\def\@NWD@spc@factor{#2}%
}
\newcommand*\nwdlinebreak{\unskip\unskip\linebreak}
\makeatother
Further readings
The trick of including a box in a glob of glue as \leaders
, in order to have it disappear at line or page breaks, is well known: as far as I can tell, it was first proposed by Marcin Woliński in 2007, see How to make a box disappear at a line break.
See also:
Check if at begin of a line;
Is something the first item on a page?;
Is it possible to redefine the white space command?.