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

line-breakingmath-mode

In the inline math mode ($...$), if the formula is too long, LaTeX will try to break it on operators, e.g.

very long text followed by a very long equation like $a+b+c+d+e+f+g+h+i+j+k+l$ etc

may be rendered as

very long text followed
by a very long equation
like a+b+c+d+e+f+g+h+i+
j+k+l etc

However, the break won't happen if they are separated by commas, e.g.

very long text followed by a very long equation like $a,b,c,d,e,f,g,h,i,j,k,l$ etc

will overflow the page like

very long text followed
by a very long equation
like a,b,c,d,e,f,g,h,i,j,k,l
etc

How to make LaTeX able to insert line breaks after a comma too?

Best Answer

If the expression contains many commas then consider to break it into several math expressions, separated by commas. It reads like a list of math expressions. This way TeX can break the line.

To achieve line breaks after a comma, you could insert \allowbreak after the comma and before the next math symbol. If necessary, leave a blank after \allowbreak.

If you would like to have a document wide solution, you could redefine the comma. One solution, following the tip here would be:

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