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

3
  • I'm sure this is a duplicate, but you could use \discretionary or \leaders Jun 18, 2020 at 16:37
  • Does tex.stackexchange.com/a/44951/4427 do what you're looking for?
    – egreg
    Jun 18, 2020 at 16:47
  • @DavidCarlisle As I mentioned, I couldn’t get discretionary to work. And doesn’t \leaders solve the opposite problem? Jun 18, 2020 at 16:48

2 Answers 2

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}
1
  • Wow, thanks! I can’t say I understand exactly how it works, but it works. 😃 Jun 18, 2020 at 16:59
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.