3

I would like to use semantic line breaks in my document's source code, but I'm not sure what to do with em dashes: ---.

sentence-per-line, no em-dash spaces:

The food---which was delicious---reminded me of home.
The food, which was delicious, reminded me of home.

clause-per-line, inconsistent em-dash spaces:

The food
---which was delicious---
reminded me of home.
The food,
which was delicious,
reminded me of home.

I would like both sources to render the same, but the clause-per-line version introduces a space before or after an em-dash.

I could instead use

sentence-per-line, em-dash spaces:

The food --- which was delicious --- reminded me of home.

and

clause-per-line, em-dash spaces:

The food
--- which was delicious ---
reminded me of home.

which render the same as each other and make the em-dash spacing situation consistent with the comma situation.

I would like to defer the style choice of how much space is around an em-dash, and be able to try different choices without modifying the source throughout.

I would prefer to continue using triple ASCII dash ---, and could also consider using a Unicode character, but I do not want to use any backslash \ to invoke a command.

Related: Semantic linefeeds and problem with space before footnote mark

2
  • 1
    a newline is the same as a space so using a newline before --- is like using a newline (or space) before . it is almost always wrong. – David Carlisle Feb 18 at 21:43
  • @DavidCarlisle a new line is the same as a space to TeX. But I use other tools on these text files, and to those tools a newline and a space are different. That is the point of semantic line breaks. Is there a way I can redefine --- so that it always eats up the space around it? Or eats it up and then adds some back in? – Gus Feb 18 at 23:18
2

This answer, which drew directly from my answer at Automatic text highlighting based on a dictionary, uses the same technique: running the tokens of the environment through a scanner in search of, in this case - tokens. If they are found to occur in a group of three, the tokens are prepended with \unskip and appended with \ignorespaces, before the token list is executed.

To start the environment, invoke \def\currentword{}\tokencyclexpress and to close it out, invoke \endtokencyclexpress. This can be done once, at the beginning and end of the document.

\documentclass{article}
\usepackage{tokcycle,listofitems,xcolor}
\setsepchar{-}
\newcommand\testdict{%
  \if\relax\detokenize\expandafter{\currentword}\relax\else
    {\ignoreemptyitems
      \greadlist\dictcompA{\currentword}}%
    \ifnum\listlen\dictcompA[]=0\relax
      \addcytoks[1]{\autohighlightStyleA}%
      \addcytoks[1]{\expandafter{\currentword}}
    \else
      \addcytoks[1]{\currentword}%
    \fi
  \fi
  \gdef\currentword{}%
}
\makeatletter
\Characterdirective{\tctestifx 
  {-#1}{\g@addto@macro\currentword{#1}}
  {\testdict\addcytoks{#1}}}
\stripgroupingtrue
\Groupdirective{\testdict\groupedcytoks{\processtoks{#1}\testdict}}
\Macrodirective{\g@addto@macro\currentword{#1}}
\Spacedirective{\testdict\addcytoks{#1}}
\makeatother
\newcommand\autohighlightStyleA{\zz}
\def\emdashref{---}
\newcommand\zz[1]{\def\tmp{#1}\ifx\emdashref\tmp
  \unskip#1\ignorespaces\else#1\fi}
\begin{document}
\def\currentword{}\tokencyclexpress
The food---which was delicious---reminded me of home.
The food, which was delicious, reminded me of home. - . -- .

The food
---which was delicious---
reminded me of home.
The food,
which was delicious,
reminded me of home. - . -- .
\endtokencyclexpress
\end{document}

enter image description here

0

You can add % at the end of a line to suppress the space:

The food%
---which was delicious---%
reminded me of home.
The food,
which was delicious,
reminded me of home.
1
  • Thanks, I should have anticipated this answer in my question. I would like to not have to add those trailing comment characters if possible. I'm settling toward always adding (in the source code) one space (or line break) on each side of ---. Could I for example redefine --- so that it "eats up" the spaces (in the layout) before and after it? – Gus Feb 18 at 23:24

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.