12

enter image description hereenter image description hereI am trying to typeset some old book in which I notice that all numbers are surrounded with 1 dot (except if it's first in a line in which case there is no dot before or last in a line in which case there is no dot after it). If present the dot is inside the normal interword space (as if by itself it takes no space).

Is there anyway in (lua)TEX (I use context) to typeset numbers in such a way?

I've tried very naive way, but it does not discard dots at beginning or end of lines, and I guess there are issues as well if glue shrinks too much.

\starttext

\dorecurse{100}{
Le \llap{.}1\rlap{.} element. }

\stoptext

Tried cleaders (which are discardable) but dots are centered (and not close to the digit) and do not seem to stretch as I'd expect.

Added picture for illustration. This shows the dots being discarded at end of lines but still too much spacing around the numbers.

Example of old typography (Olivetan french Bible, pdf available from http://www.e-rara.ch/gep_g/content/titleinfo/1751440 if you like gothic typography. It's not easy from the book what is the spacing amount.

Joseph
  • 307
  • 5
    Could you post a screenshot of something you'd like to replicate? – Mico Apr 22 '17 at 12:53
  • 1
    As background info, these dots seem to come from latin where they were needed to distinguish between numbers (in roman notation) and other normal words. Apparently in some middle age books some books were still following this convention. – Joseph Apr 22 '17 at 13:02
  • @Mico I have a simple MWE pdf but I do not know how to attach a file in a comment. – Joseph Apr 22 '17 at 13:08
  • 2
    @Joseph - You can edit your posting and click on the "Image" button (looks like a mountain range w/ sun in upper right corner) to attach a png file. – Mico Apr 22 '17 at 13:36
  • To clarify your requirements about spacing around the dots, can you add an image of what you'd actually desire? A photograph of a page of the book maybe? – ShreevatsaR Apr 23 '17 at 08:25

2 Answers2

10
\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

enter image description here

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

enter image description here

egreg
  • 1,121,712
  • Two dots might appear if the line has to be excessively stretched. – egreg Apr 22 '17 at 14:21
  • Thanks for suggestion, If I understand correctly how cleaders work the dot is centered between the words it is located. Is there any "leaders" similar mechanism that enables the dot to be left or right side (would be named lleaders / rleaders maybe), similar to cleaders but all space on top of box is either left or right side instead of being 1/2 left and 1/2 right ? – Joseph Apr 22 '17 at 15:17
  • 1
    @Joseph So the periods are attached to the number? – egreg Apr 22 '17 at 15:32
  • As a fast solution you can use \hss\rlap.\hss in the first \adddot and \hss\rlap.\hss in the second \adddot of \num. – Manuel Apr 22 '17 at 16:33
  • I beg your pardon, but isn’t your edit essentially the same as my answer? – GuM Apr 22 '17 at 23:46
  • @GustavoMezzetti I don't think so. It's much simpler. – egreg Apr 22 '17 at 23:48
  • 1
    Are you referring to \spaceskip and \xspaceskip? Edit: Oh, no, now I’ve seen that you didn’t mean that. In any case, of course I had thought of a solution like that, but I wanted to put the dots inside the interword spaces. – GuM Apr 22 '17 at 23:52
  • @GustavoMezzetti There's no point in reducing the space; it's bad typography anyway. – egreg Apr 22 '17 at 23:55
  • 1
    OK, but the problem is more interesting that way! (:-) – GuM Apr 22 '17 at 23:58
  • Hmm the question says “If present the dot is inside the normal interword space (as if by itself it takes no space)” — I think in the above example the dots do take space? – ShreevatsaR Apr 23 '17 at 07:11
  • @ShreevatsaR Without an image, it's just guessing – egreg Apr 23 '17 at 08:23
  • @egreg Yes you're right. As the OP mentions it twice (the second time in “Added picture for illustration. This shows […] still too much spacing around the numbers.”), I think they may be serious about reducing the space, but I agree that without an image we have incomplete information about the desired typography. – ShreevatsaR Apr 23 '17 at 08:27
  • Yes the dots are attached to number, but I realise that maybe it's just a matter of having a narrower glue before left dot and same after right dot, in this case a \hskip might do (learned Something new with TeX). Sorry for having made lost your time, and thanks for your replies and help. – Joseph Apr 23 '17 at 17:34
  • This is an outdated answer (2021) considering that now ConTeXt uses two engines (LuaTeX and LuaMetaTeX) and expl3, and hence this answer, just doesn't work in the latter. Overall, relying in packages written for LaTeX is a time bomb. –  Jan 28 '21 at 08:38
4

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:

Output of the code

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:

Output of the second code sample

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:

GuM
  • 21,558