3

Is it possible have a command insert some text except in the case that it will be placed at the end of a line? Here’s a simple (LuaLaTeX) example:

\documentclass{scrartcl}

\usepackage{polyglossia} \disablehyphenation

\newcommand{\sep}{\unskip\nolinebreak[4]~*~\linebreak[3]}

\begin{document}

\parbox{9cm}{% Arabel \sep Dusty \sep Octavia \sep Dinny \sep Demetris \sep Thomasa \sep Cherie \sep Deeann \sep Page \sep Jesse \sep Kitti \sep Ruthie \sep Cory \sep Kathleen \sep Kaylyn \sep Chelsie \sep Floe \sep Verna \sep Torey \sep Cissy \sep Nora \sep Krissie \sep Hesther \sep Virgie \sep Liliane \sep Abagael \sep Joyann \sep Siobhan \sep Pierette% \parfillskip=0pt\par}

\end{document}

enter image description here

The \nolinebreak[4]and \linebreak[3] commands are there to avoid having a line break before the separator character and to encourage a line break after it (especially useful if the names separated by\sep contain spaces).

I want to have the separator printed between all names, but not if it comes at the end of the line (there the line break acts as a separator). I have tried using a \discretionary, but that didn’t work, since the \linebreak command adds a linebreak after the discretionary, not inside it.

I use LuaLaTeX, so I’m open to a LuaTex solution. Here’s how I want the result to look (though perhaps with other line breaks).

enter image description here

2 Answers2

4

leaders acts like glue so is dropped at a line break

enter image description here

\documentclass{scrartcl}

\usepackage{polyglossia} \disablehyphenation

\newcommand{\sep}{\unskip\space\xleaders\hbox{*}\hskip\fontcharwd\font`*\relax\space}

\begin{document}

\parbox{9cm}{% Arabel \sep Dusty \sep Octavia \sep Dinny \sep Demetris \sep Thomasa \sep Cherie \sep Deeann \sep Page \sep Jesse \sep Kitti \sep Ruthie \sep Cory \sep Kathleen \sep Kaylyn \sep Chelsie \sep Floe \sep Verna \sep Torey \sep Cissy \sep Nora \sep Krissie \sep Hesther \sep Virgie \sep Liliane \sep Abagael \sep Joyann \sep Siobhan \sep Pierette% \parfillskip=0pt\par}

\end{document}

David Carlisle
  • 757,742
3

Building on https://tex.stackexchange.com/a/44951/4427

You can still use \sep, if you want (but not in the words environment).

\documentclass{scrartcl}

\usepackage{polyglossia} \disablehyphenation

\newcommand{\sep}{\unskip \hskip \fontdimen2\font plus 2\fontdimen2\font minus \fontdimen3\font \cleaders\copy\ANDbox\hskip\wd\ANDbox \hskip \fontdimen2\font plus 2\fontdimen2\font minus \fontdimen3\font \ignorespaces} \newsavebox\ANDbox \sbox\ANDbox{*}

\newenvironment{words}[1][\textwidth] {\par\addvspace{\topsep}\begin{minipage}{#1}\obeyspaces\activatesep} {\parfillskip=0pt\par\end{minipage}\par\addvspace{\topsep}} \newcommand{\activatesep}{% \begingroup\lccode~= \lowercase{\endgroup\let~}\sep }

\begin{document}

\begin{words}[5cm] Arabel Dusty Octavia Dinny Demetris Thomasa Cherie Deeann Page Jesse Kitti Ruthie Cory Kathleen Kaylyn Chelsie Floe Verna Torey Cissy Nora Krissie Hesther Virgie Liliane Abagael Joyann Siobhan Pierette \end{words}

\begin{words}[9cm] Arabel Dusty Octavia Dinny Demetris Thomasa Cherie Deeann Page Jesse Kitti Ruthie Cory Kathleen Kaylyn Chelsie Floe Verna Torey Cissy Nora Krissie Hesther Virgie Liliane Abagael Joyann Siobhan Pierette \end{words}

\begin{words} Arabel Dusty Octavia Dinny Demetris Thomasa Cherie Deeann Page Jesse Kitti Ruthie Cory Kathleen Kaylyn Chelsie Floe Verna Torey Cissy Nora Krissie Hesther Virgie Liliane Abagael Joyann Siobhan Pierette \end{words}

\end{document}

enter image description here

egreg
  • 1,121,712