158

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?

6
  • 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! Commented Jan 8, 2011 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. Commented Jan 8, 2011 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. Commented Jan 8, 2011 at 22:09
  • 1
    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. Commented Dec 4, 2017 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. Commented Sep 28, 2023 at 7:41

7 Answers 7

140

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 
8
  • 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. Commented Aug 18, 2010 at 15:50
  • 3
    +1, excellent answer! However, there's a complication: Please see tex.stackexchange.com/q/19094/1347. Commented May 26, 2011 at 9:51
  • 11
    Note that the \allowbreak solution does not work if you have \left...\right delimiters that span the break in your equation Commented Jan 7, 2016 at 15:45
  • 12
    Any trick for \allowbreak working in \left...\right delimiters? Commented Mar 21, 2017 at 15:29
  • 4
    @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. Commented Mar 1, 2021 at 15:29
45

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

4
  • 13
    Wow, breqn allows \left and \right to work across line breaks! Commented Aug 18, 2010 at 17:13
  • 8
    Indeed, amongst other things. The late Michael Downes was a very clever guy! Commented Aug 18, 2010 at 18:02
  • 2
    For commas, this does not work with all types of atoms. See the discussion here. Commented Apr 26, 2015 at 12:47
  • 1
    The intro to the package reads "provides solutions to a number of common difficulties in writing displayed equations". The question was about inline equations - does it help with those as well? Commented Jun 10 at 18:18
24

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.

first image

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} 

second output


The latter code in expl3 lingo.

\documentclass{article} \ExplSyntaxOn \NewDocumentCommand{\splitatcommas}{m} { \group_begin: \int_compare:nNnF { \char_value_mathcode:n {`,} }= {"8000} { \char_set_active_eq:nN {`,} \__kennytm_comma: \char_set_mathcode:nn {`,} {"8000} } #1 \group_end: } \cs_new_protected:Ne \__kennytm_comma: { \mathchar\char_value_mathcode:n {`,}~ \penalty0~\exp_not:n { \hspace{0pt plus 1em} } } \ExplSyntaxOff \newcommand{\tuple}[1]{(\splitatcommas{#1})} \newcommand{\set}[1]{\{\splitatcommas{#1}\}} \begin{document} \setlength{\lineskiplimit}{2pt}\setlength{\lineskip}{3pt} % for this particular case \setlength{\parindent}{0pt} % just for this example $\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} }$ \medskip $\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} 

third output

20
  • 1
    @Nasser With breqn this is guaranteed not to work. Probably something can be done, I'll work on your problem later. Commented May 14, 2016 at 16:59
  • 3
    @azetina I don't consider breqn a usable piece of software. Commented Jul 13, 2016 at 14:15
  • 1
    @Ronny Added the variant Commented Jan 8, 2018 at 13:46
  • 1
    You should and an L3 version if possible. Commented May 17 at 15:16
  • 1
    @azetina You're welcome! Commented May 17 at 15:46
18

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

2
  • 2
    Welcome ! Could you please expand your answer a bit, with a small example for instance ? Commented Nov 5, 2018 at 15:38
  • 1
    That's it! It also works in plain TeX. Commented Sep 7, 2022 at 9:17
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} 
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.

2
  • 11
    You might as well just omit \left and \right. Putting the matching brace directly adjacent obviates any point to having scaling braces at all. Commented Mar 15, 2014 at 18:36
  • disregarding the pointless \left and \right commands, ;) it is a quick workaround. Commented Jun 6, 2016 at 10:39
0

Following up on the discussion in this thread regarding allowing line breaks after commas in inline math mode, I wanted to share an approach I found compelling, which I perceive as better than some of the simpler suggestions like just using \mathbin{,} or manually adding \allowbreak every time.

The challenge is twofold:

  1. Ensuring that a line break can occur after the comma.
  2. Achieving a desirable visual spacing around the comma, ideally something closer to the spacing in text mode or the math.

My preferred approach is to define a command that gives a specific visual spacing while ensuring breakability. I use this:

\newcommand{\comma}{\mathbin{\ooalign{$\hidewidth{,}$\cr$\,$}}}. 

Or this

\newcommand{\comma}{,\allowbreak} 

The first command gives spaces similar to how spaces work in math mode, and the other breaks the math equation, with only a small space after the comma, identical to how vectors are written in mathematics.

Lastly, as an additional command:

\newcommand{\comma}{\mathbin{\ooalign{$\hidewidth{,}$\cr$\mathmakebox[LENGTH em]{}$}}} 

This last command lets you insert any size inside LENGTH, but it is quite unconventional.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.