3

I like to use thin spaces in some in-line text lists, .e.g. of year numbers. Example: 2001, 2002, 2005, 2022. To compress the space needed, instead of space, I have been using \, between these year numbers.

However, occasionally, I get problems with line breaks, because the years are glued together by the \,.

So, the question is which command allows breakable thin spaces.

ingli
  • 853

4 Answers4

2

Instead of adding some command after the comma, which is error prone, I suggest to define your own command for these series.

\documentclass{article}

\ExplSyntaxOn

\NewDocumentCommand{\tbspace}{}{\hspace{0.16667em}}

\NewDocumentCommand{\series}{sm} { \IfBooleanTF{#1} { \clist_use:nnnn { #2 } { ~ and\nobreakspace } { , ~ } { ~ and\nobreakspace } } { \clist_use:nn { #2 } { ,\tbspace } } }

\ExplSyntaxOff

\begin{document}

\series{2001, 2002, 2005, 2022}

\bigskip

\parbox[t]{0pt}{\series{2001, 2002, 2005, 2022}}

\bigskip

\series*{2001, 2002, 2005, 2022}

\end{document}

You see that with just the addition of * you can change the formatting, if need be. The zero width \parbox shows that line breaks are possible after the comma.

enter image description here

egreg
  • 1,121,712
2

The horizontal space is normally breakable when you use \hskip primitive without preceding penalty. I set the same amount of \tbspace like in egreg's answer, but (of course) I used only TeX primitives.

\def\series #1{\def\seriesX{\def\seriesX{,\tbspace}}\seriesA #1, {}, }
\def\seriesA #1, {\ifx^#1^\else \seriesX#1\expandafter\seriesA\fi}
\def\tbspace{\hskip.16667em\relax}

\series{2001, 2002, 2005, 2022}

wipet
  • 74,238
1

For good measure, a LuaLaTeX-based solution. :-)

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{luacode} % for '\luaexec' macro
\luaexec{ function series ( s )
            return ( s:gsub ( "[\%s]+" , "\\hspace{0.16666em}" ) )
          end }
\newcommand\series[1]{\directlua{tex.sprint(series("#1"))}}

\begin{document} 2001, 2002, 2005, 2022

\series{2001, 2002, 2005, 2022} % replace space with breakable thinspace \end{document}


Addendum: Just for completeness (and maybe to make @wipet's day too...), here's a solution that makes the Lua function series work with both LaTeX and PlainTeX. I'm not posting an associated screenshot as the output is the same as what's shown above.

% !TEX TS-program = lualatex
\documentclass{article}
\directlua{ function series ( s )
              return ( s:gsub ( "[\csstring\%s]+" , "\string\\hskip0.16666em\string\\relax" ) )
            end }
\newcommand\series[1]{\directlua{tex.sprint(series("#1"))}}

\begin{document} 2001, 2002, 2005, 2022

\series{2001, 2002, 2005, 2022} \end{document}

Mico
  • 506,678
  • 1
    If you replace \\hspace{0.16666em} by \string\\hskip0.16666em\string\\relax and \luaexec by \directlua then your Lua function becomes independent of LaTeX. – wipet Jan 25 '23 at 06:40
  • @wipet - Thanks. :-) The reason I use \luaexec instead of \directlua in the answer shown above is that \luaexec lets me easily "smuggle" a % symbol over to the Lua side. (I need the % symbol in the pattern-matching expression [%s]+, which produces a pattern match for one-or-more instances of %s, Lua's "magic" character for whitespace.) Of course, I could use \directlua if I first replaced \% with \percentchar, where the latter could be defined via, say, \begingroup\catcode`%=12\relax\gdef\percentchar{%}\endgroup. I'll post an addendum... – Mico Jan 25 '23 at 06:57
  • 1
    OK, I forgot the \% problem because the \% is a expandable macro which expands to % in my macro package. I can use simply \% in \directlua. Plain TeX defines \% using \chardef, then we have to write \csstring\% instead \% in \directlua. – wipet Jan 25 '23 at 07:07
  • @wipet - Thanks for this pointer. I'll update the addendum accordingly. – Mico Jan 25 '23 at 07:10
0

I found Should "et al." have a thin or full, non-breaking vs. breaking space? which got me to the code et\hspace{.16667em}al..

I now have a new command \newcommand{\tbspace}{\hspace{.16667em}}.

This allows me to work with e.g. \tbspace{2001},\tbspace{2002}.

ingli
  • 853
  • 2
    please note that your \tbspace doesn't take an argument. Correct would be 2000,\tbspace 2001,\tbspace 2002. The extra braces don't really hurt though. – Skillmon Aug 27 '22 at 16:44