15

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?

1 Answer 1

25

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.)

6
  • Is it possible to change the catcode of , only in the mathmode, so that \cite does not encounter such problem? May 26, 2011 at 10:19
  • @Sadeq: the first piece of code I proposed does just this.
    – egreg
    May 26, 2011 at 10:22
  • 1
    +1, cool that \mathlist! Jun 11, 2012 at 13:38
  • 1
    @LoverofStructure It shouldn't matter where you put the definitions. The second method shouldn't have any problem with other packages; in case some package or macro wants to make the comma an active character there would, of course, be a conflict.
    – egreg
    Feb 3, 2013 at 8:55
  • 1
    (1) I think a very useful thing would be a way to set the comma to be math-active by default but to allow manual overrides (one could call it \disallowlinebreak). (2) Similarly, if I include \allowbreak in a macro, I'd like a manual way of disabling this (for both the case where \allowbreak occurs at the beginning and where it occurs at the end of a macro definition). — Let me know if you think it'd be useful for me to ask about these two things in one or two separate questions. Feb 3, 2013 at 12:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .