[Tex/LaTex] “Allowing line break at ‘,’ in inline math mode” breaks citations

citingline-breakingmath-mode

In answering another question allowing line break at ',' in inline math mode, Stefan Kottwitz suggested two excellent ways to allow line breaks in inline math mode after ,. One way was to use the \allowbreak command after the comma, and the other way was to globally redefine , so that line breaks are allowed.

I used the second approach, and was really fond of it, until I noted that the command \cite{} no longer works as expected: If I cited more than one reference, it showed [?] instead of the correct citation. Here's an example:

\documentclass{article}

%----------Redefining `,` ------------------%
\makeatletter
\def\old@comma{,}
\catcode`\,=13
\def,{%
  \ifmmode%
    \old@comma\discretionary{}{}{}%
  \else%
    \old@comma%
  \fi%
}
\makeatother
%-------------------------------------------%

\begin{document}

\cite{A,B}

\begin{thebibliography}{A}
\bibitem{A} ABC
\bibitem{B} DEF
\end{thebibliography}

\end{document}

What is wrong with redefining ,? How to correct it?

Best Answer

When \cite tries to split the argument, it doesn't find the expected delimiter, which is a category 12 comma and is different from a category 13 comma.

The correct way, other than using breqn is to make the comma "math active", just as it is done in the package icomma.

\AtBeginDocument{%
  \mathchardef\mathcomma\mathcode`\,
  \mathcode`\,="8000 
}
{\catcode`,=\active
  \gdef,{\mathcomma\discretionary{}{}{}}
}

But I should warn about making this definition, as TeX might break also formulas such as $(a,b)$. I would suggest

\mathchardef\breakingcomma\mathcode`\,
{\catcode`,=\active
  \gdef,{\breakingcomma\discretionary{}{}{}}
}
\newcommand{\mathlist}[1]{$\mathcode`\,=\string"8000 #1$}

and using \mathlist{a,b,c,d} for a list that can be split across lines. (The \string is useful in connection with babel, for some languages make the double quote active.)

Related Question