# Handling optional commas in text

I'm writing a document in Danish using the memoir LaTeX class.

In Danish we have two different systems for placing commas in a text; one system uses many commas, the other system uses fewer commas. I want my document to be able to generate both.

The simple solution is to introduce an \optionalcomma command, and then define it as either

\newcommand{\optionalcomma}{, }


or

\newcommand{\optionalcomma}{ }


depending on what output I want.

However, placing innumerable \optionalcomma commands in the text, makes it harder for me, the author, to read.

So I wondered, is it possible to have a very unobtrusive way of writing the optional comma. Is there, for example, a way to use a double comma (or some other "small footprint" command) for an optional comma?

This would enable me to write this in my LaTeX file:

Han så,, at jeg kom, og at jeg var glad.


and with minimal changes to the LaTeX file have it produce either this output:

Han så, at jeg kom, og at jeg var glad.


or this:

Han så at jeg kom, og at jeg var glad.


Unobtrusive parts in a TeX file can be made with a single character if it is an active character. Here is how you can do it with ; for \optionalcomma (if you don't need real semicolons).

\documentclass{memoir}

\newcommand{\optionalcomma}{,}
% \newcommand{\optionalcomma}{}

\catcode;=\active
\newcommand;{\optionalcomma}

\begin{document}

\end{document}

• This is exactly what I need. Thank you. – oz1cz Jan 2 '20 at 12:16
• You will want to move the \catcode;=\active section after \begin{document}. This might or might not also break TikZ. – Henri Menke Jul 25 '20 at 4:43

You might (ab)use \, for the purpose. Use \thinspace if you really need the original meaning of \, in text mode.

\DeclareRobustCommand{\,}{\ifmmode\mskip\thinmuskip\else\optionalcomma\fi}
\newcommand{\optionalcomma}{} % standard, no comma


## Example with no optional comma

\documentclass{memoir}

\DeclareRobustCommand{\,}{\ifmmode\mskip\thinmuskip\else\optionalcomma\fi}
\newcommand{\optionalcomma}{} % standard, no comma

\begin{document}

\end{document}


## Example with optional comma activated

\documentclass{memoir}

\DeclareRobustCommand{\,}{\ifmmode\mskip\thinmuskip\else\optionalcomma\fi}
\newcommand{\optionalcomma}{} % standard, no comma

\activateoptionalcomma

\begin{document}

\end{document}


Piece of cake if you're free to use LuaLaTeX. :-)

The code below sets up two Lua functions, called functionA and functionB, and two LaTeX utility macros, called \OptionA and \OptionB to activate the respective Lua functions. By "activating" a Lua function, I mean assigning it to LuaTeX's process_input_buffer callback, which operates on the input at a very early stage, i.e., before TeX starts its usual macro expansion operations. Both functionA and functionB employ Lua's powerful gsub ("global substitution") string function.

I would assume that, in practice, your document will only contain either an \OptionA directive or an \OptionB directive. Ideally, you'd issue the directive immediately after \begin{document}.

The following screenshot shows the output of Han så,, at jeg kom, og at jeg var glad. if \OptionA is executed at the start of the document environment.

% !TEX TS-program = lualatex
\documentclass{memoir}
\usepackage[danish]{babel}

%% Set up two Lua functions:
\directlua{%
function functionA ( s )  return ( s:gsub(",," ,",") )  end
function functionB ( s )  return ( s:gsub(",," , "") )  end
}

%% Set up two LaTeX macros to activate the corresponding Lua functions: