Lualatex convert — to custom command automatically

luatex

This answer shows a nicely spaced emdash. I'm wondering if with lualatex and all its power if it's possible to automatically convert --- to the \dash command in the above link? (say with the toggles \autodashON and \autodashOFF)

Best Answer

enter image description here

You can replace --- in the input by \dash

\documentclass{article}

\DeclareRobustCommand\dash{%
  \unskip\nobreak\thinspace\textemdash\allowbreak\thinspace\ignorespaces}

\directlua{
function dashes(s)
return string.gsub(s,'[-][-][-]','\string\\dash ')
end}

\def\autodashON{\directlua{%
luatexbase.add_to_callback("process_input_buffer",dashes,"spaced em dash")%
}}
\def\autodashOFF{\directlua{%
luatexbase.remove_from_callback("process_input_buffer","spaced em dash")%
}}

\setlength\parskip{20pt}
\setlength\parindent{0pt}
\begin{document}


One---two three --- four five.
One---two three --- four five.
One---two three --- four five.
One---two three --- four five.

\autodashON

One---two three --- four five.
One---two three --- four five.
One---two three --- four five.
One---two three --- four five.

\autodashOFF

One---two three --- four five.
One---two three --- four five.
One---two three --- four five.
One---two three --- four five.

\end{document}
Related Question