147

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?

kennytm
  • 6,392
  • 6
  • 27
  • 18
  • I tried breqn but it uses "expl3.sty" which can not be found by Latex ALTHOUGH I downloaded it and put in the same folder where breqn.sty exists! –  Jan 08 '11 at 22:09
  • @Ahmad: If you've got a question, then you should ask it in a new post. Please do this with the "Ask Question" link. In your new question you could link to this one. – Hendrik Vogt Jan 08 '11 at 22:09
  • @Ahmad: Just a note to confirm Hendrik's comment, this ought to be reposted as a question for you to get the best chance of it being answered. – Andrew Stacey Jan 08 '11 at 22:09
  • Late to the party, but just for the record: You can make the comma be treated like a binary or relation operator with the commands \mathbin{,} or \mathrel{,}. For instance, $stuff \mathrel{,} morestuff$ will allow the linebreak between the two stuffs. – phfaist Dec 04 '17 at 22:27
  • Automatical breaking at + does happen neither between \left and \right nor between { and }. I had to suppress them manually in latex's export of matlab's Live Editor. – user2987828 Sep 28 '23 at 07:41
  • Ah and please see more recent question https://tex.stackexchange.com/questions/555544/how-to-automatically-linebreak-an-inline-math-formula – user2987828 Sep 28 '23 at 07:41

6 Answers6

130

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
Stefan Kottwitz
  • 231,401
  • 8
    Thanks. My expression is actually a set with 48 elements, so splitting them into several expressions may not sound mathematically logical. I will try \allowbreak. – kennytm Aug 18 '10 at 15:50
  • 3
    +1, excellent answer! However, there's a complication: Please see http://tex.stackexchange.com/q/19094/1347. – Sadeq Dousti May 26 '11 at 09:51
  • 10
    Note that the \allowbreak solution does not work if you have \left...\right delimiters that span the break in your equation – Mosby Jan 07 '16 at 15:45
  • 11
    Any trick for \allowbreak working in \left...\right delimiters? – loved.by.Jesus Mar 21 '17 at 15:29
  • 3
    This document-wide solution seems to break tikz... – xuhdev Aug 13 '17 at 04:22
  • 1
    @xuhdev if you use ~ in math environment for spacing, then redefining ~ instead of , (with old value \) is a solution which does not break tikz. – Peeyush Kushwaha Oct 23 '19 at 08:39
  • Thank you for your code. Your code works for me. – Md Amiruzzaman Feb 08 '20 at 03:35
  • 2
    @loved.by.Jesus : one possibility (not ideal but can work in some circumstances) is to replace \left(, \right) e.g. by \Big(, \Big). Then allowbreak or linebreak will work. – Watson Mar 01 '21 at 15:29
44

You could take a look at the breqn package, which is aimed at solving this problem in a general sense.

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
21

Here is a solution that doesn't make the comma globally active:

\documentclass{article}

\newcommand{\splitatcommas}[1]{%
  \begingroup
  \begingroup\lccode`~=`, \lowercase{\endgroup
    \edef~{\mathchar\the\mathcode`, \penalty0 \noexpand\hspace{0pt plus 1em}}%
  }\mathcode`,="8000 #1%
  \endgroup
}

\begin{document}

\setlength{\lineskiplimit}{2pt}\setlength{\lineskip}{3pt} % for this particular case

$\splitatcommas{
  \frac{1}{2},\frac{3}{5},\frac{8}{13},\frac{21}{34},\frac{55}{89},
  \frac{144}{233},\frac{377}{610},\frac{987}{1597},\frac{2584}{4181},
  \frac{6765}{10946},\frac{17711}{28657},\frac{46368}{75025},
  \frac{121393}{196418},\frac{317811}{514229},\frac{832040}{1346269},
  \frac{2178309}{3524578},\frac{5702887}{9227465},
  \frac{14930352}{24157817},\frac{39088169}{63245986},\frac{102334155}{165580141}
}$

\end{document}

The setting of \lineskiplimit and \lineskip are for the particular case where fractions are needed in the argument.

enter image description here

A variant that allows nesting:

\documentclass{article}

\newcommand{\splitatcommas}[1]{%
  \begingroup
  \ifnum\mathcode`,="8000
  \else
    \begingroup\lccode`~=`, \lowercase{\endgroup
      \edef~{\mathchar\the\mathcode`, \penalty0 \noexpand\hspace{0pt plus 1em}}%
    }\mathcode`,="8000
  \fi
  #1%
  \endgroup
}

\newcommand{\tuple}[1]{(\splitatcommas{#1})}
\newcommand{\set}[1]{\{\splitatcommas{#1}\}}

\begin{document}

\setlength{\lineskiplimit}{2pt}\setlength{\lineskip}{3pt} % for this particular case

$\splitatcommas{
  \frac{1}{2},\frac{3}{5},\frac{8}{13},\frac{21}{34},\frac{55}{89},
  \frac{144}{233},\frac{377}{610},\frac{987}{1597},\frac{2584}{4181},
  \frac{6765}{10946},\frac{17711}{28657},\frac{46368}{75025},
  \frac{121393}{196418},\frac{317811}{514229},\frac{832040}{1346269},
  \frac{2178309}{3524578},\frac{5702887}{9227465},
  \frac{14930352}{24157817},\frac{39088169}{63245986},\frac{102334155}{165580141}
}$

$\set{
  \tuple{a,b,c,d},\tuple{1,2,3,4,5,6},\tuple{11,22,33,44,55,66,77,88},
  \tuple{a,b,c,d},\tuple{1,2,3,4,5,6},\tuple{11,22,33,44,55,66,77,88},
  \tuple{a,b,c,d},\tuple{1,2,3,4,5,6},\tuple{11,22,33,44,55,66,77,88},
  \tuple{a,b,c,d},\tuple{1,2,3,4,5,6},\tuple{11,22,33,44,55,66,77,88}
}$

\end{document}

enter image description here

egreg
  • 1,121,712
  • 1
    @Nasser With breqn this is guaranteed not to work. Probably something can be done, I'll work on your problem later. – egreg May 14 '16 at 16:59
  • Did you manage to find the workaround for breqn? – azetina Jul 13 '16 at 13:51
  • 2
    @azetina I don't consider breqn a usable piece of software. – egreg Jul 13 '16 at 14:15
  • @egreg Nice solution! But if I do nesting like \splitatcommas{a , b,\splitatcommas{c, d} I’m getting errors like ! Bad mathchar (32768). Do you have any idea to fix this? – Ronny Jan 07 '18 at 23:02
  • @Ronny I'm not sure I understand what the nesting is for. – egreg Jan 07 '18 at 23:27
  • @egreg The nesting occurs if I use \splitatcommas in some math commands I defined myself. E. g. \newcommand{\tuple}[1]{(\splitatcommas{#1})} and \newcommand{\set}[1]{\{\splitatcommas{#1}\}}. If I use them like \set{\tuple{a,b}}, I get the error above. – Ronny Jan 08 '18 at 13:33
  • 1
    @Ronny Added the variant – egreg Jan 08 '18 at 13:46
  • @egreg You are great! Thank you very much. I’ve looked for something like \ifnum\mathcode,="8000`, but wasn’t able to implement. My dissertation will look much better. – Ronny Jan 08 '18 at 14:11
  • @egreg I tried your code with a set of ordered pairs but it breaks lines also within the pairs. How can one modify so that with {(a,b), (c,d)} it can split at '), ' but not at 'a,b' and 'c,d'? – Geoff Sep 04 '20 at 14:31
  • @Geoff Did you see \tuple? – egreg Sep 04 '20 at 14:39
  • @egreg According to the image it breaks the tuple (1,2,3,4,5,6) whereas I don't want tuples to be broken. – Geoff Sep 04 '20 at 14:49
  • @Geoff Ah, OK. You need to define differently \tuple, then. Something like \mathchardef\standardcomma=\mathcode`\, \newcommand{\tuple}[1]{\begingroup\mathcode`\,=\standardcomma(#1)\endgroup} – egreg Sep 04 '20 at 14:56
  • It's as "simple" as that! Many thanks! – Geoff Sep 04 '20 at 15:02
  • I know the question asks about inline math, but I was hoping that this would work inside \begin{equation} ... \end{equation}. It does not. – Gus Jan 12 '22 at 06:41
  • @Gus TeX does not break lines inside equation – egreg Jan 12 '22 at 09:44
16

Just try inserting \allowbreak in between your inline equations.

$x_1, x_2,...\allowbreak, y_1,y_2,y_n$. The line won't reach out and break at before y_1

4

In luatex you have a new possibility that does not involve active characters, you can declare , to be a mathbin (like +) so that line breaking is allowed and then set the mathord-mathbin spacing to zero so it gets no space before, like punctuation:

enter image description here

\documentclass{article}

\begin{document}

$
\mathcode`\,="213B % mathbin
\Umathordbinspacing\textstyle 0mu % no space before
a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,
a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a,a$
\end{document}
David Carlisle
  • 757,742
2

If you can split the equation into several sub equation using $, and if you are using braces use \left. and \right. (with dot) to balance the braces.

Example:

 $X = \left\{\right.a$, $b$, $c$, $d\left.\right\}$

X = { a, b, c, d }

This should allow line breaks behind the commas.

maitek
  • 31