I would say

```
\newcommand{\x}[1]{%
{}$% get out of math
\kern-2\mathsurround % in case it's non zero
$% reenter math
\binoppenalty10000 \relpenalty10000 #1% typeset the subformula
{}$% get out of math
\kern-2\mathsurround % in case it's non zero
$% reenter math for the rest of the formula
}
```

TeX breaks formulas only after binary operators or relation symbols, the desirability of such breaks is measured by the two mentioned parameters. However the values used the penalties are those valid at the end of the formula, so simply enclosing `#1`

in `\begingroup...\endgroup`

and setting the values wouldn't do anything.

Of course this can work only if used in suitable places of the formula, for example `$a+\x{b+c}$`

would have the right spacing after the first `+`

(because of the empty subformula); the last empty subformula does nothing.

My opinion is still that bad breaks must be solved with suitably placed `\nobreak`

commands.

Some examples:

```
\documentclass[a4paper,draft]{book}
\newcommand{\x}[1]{{}$\kern-2\mathsurround${}
\binoppenalty10000 \relpenalty10000 #1{}$\kern-2\mathsurround${}}
\begin{document}
\parbox{5cm}{
A formula \(a+\x{c+d}\)\break showing that spaces are right
A new formula \(a+\x{c+d}\) showing that spaces are right
A brand new formula x \(a+\x{c+d}\) showing that spaces are right
A brand new formula xx \(a+\x{c+d}\) showing that spaces are right
A brand new formula xxx \(a+\x{c+d}\) showing that spaces are right
A brand new formula xxxx \(a+\x{c+d}\) showing that spaces are right
Another brand new formula \(a+\x{c+d}\) showing that spaces are right
Right: $\sin(\x{a+b})$
Wrong: $\sin\x{(a+b)}$
\mathsurround=30pt
A formula xxxxxxx \(a+\x{c+d}\) showing mathsurround
A formula xxxxxxx \(a+c+d\) showing mathsurround
}
\end{document}
```

**Addition about usage**

The `\x`

macro (possibly with a more descriptive name) should be used in specific places. Its contents must

(1) start with an ordinary symbol or be preceded by an ordinary symbol;

(2) end with an ordinary symbol or be followed by one.

It doesn't support the style declarations `\displaystyle`

, `\textstyle`

, `\scriptstyle`

, or `\scriptscriptstyle`

; it may make sense to carry a `\displaystyle`

declaration, this might be done with a *-variant.

It doesn't support `\left`

or `\right`

: it's not allowed something like

```
$...\left(\x{a+b}\right)...$
```

but this is not a problem, as no formula can be split at relation or operation symbols between `\left`

and `\right`

and the spaces around these symbols never participates to stretching or shrinking.

The simplest way to cope with this is to define a new command for a breakable comma:

```
\documentclass{article}
\newcommand{\bcomma}{,\allowbreak}
\begin{document}
Figure \ref{figure:example graph} shows an example of a graph
$G = (\{v_{1}, \dots, v_{5}\},
\{(v_{1}, v_{2})\bcomma
(v_{2}, v_{3})\bcomma
(v_{1}, v_{3})\bcomma
(v_{2}, v_{5})\bcomma
(v_{3}, v_{4}),
(v_{4}, v_{5})\})$
with $|V| = n = 5$ vertices and $|E| = m = 6$ edges.
\end{document}
```

I'd prefer not to use `\bcomma`

after the list of vertices and before the last edge, but in an emergency you can change also those.

A more complex solution can be written with LaTeX3 facilities:

```
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\mathlist}{ O{,} m m }
{
\egreg_mathlist:nnn { #1 } { #2 } { #3 }
}
\seq_new:N \l__egreg_mathlist_seq
\cs_new_protected:Npn \egreg_mathlist:nnn #1 #2 #3
{
\seq_set_split:Nnn \l__egreg_mathlist_seq { #1 } { #3 }
\seq_use:Nnnn \l__egreg_mathlist_seq { #2 } { #2 } { #2 }
}
\ExplSyntaxOff
\begin{document}
Figure \ref{figure:example graph} shows an example of a graph
$G = (\{\mathlist{,}{v_{1}, \dots, v_{5}}\},
\{\mathlist[;]{,\allowbreak}
{(v_{1}, v_{2});
(v_{2}, v_{3});
(v_{1}, v_{3});
(v_{2}, v_{5});
(v_{3}, v_{4});
(v_{4}, v_{5})}
\})$
with $|V| = n = 5$ vertices and $|E| = m = 6$ edges.
\end{document}
```

The `\mathlist`

command has an optional argument (the item separator, default a comma); the first mandatory argument tells what to put in place of the separator, the second argument is the list.

The first usage of `\mathlist`

in the example is of course redundant; the second one isn't: since the comma is used in the ordered pairs, I choose a semicolon as separator and it's substituted after processing by `,\allowbreak`

as in the simpler definition above.

If the list of edges is stored in a macro, the method above doesn't work. But the definition can be extended to cope with this case:

```
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\mathlist}{ s O{,} m m }
{
\IfBooleanTF{#1}
{ \egreg_mathlist:nnV { #2 } { #3 } #4 }
{ \egreg_mathlist:nnn { #2 } { #3 } { #4 } }
}
\seq_new:N \l__egreg_mathlist_seq
\cs_new_protected:Npn \egreg_mathlist:nnn #1 #2 #3
{
\seq_set_split:Nnn \l__egreg_mathlist_seq { #1 } { #3 }
\seq_use:Nnnn \l__egreg_mathlist_seq { #2 } { #2 } { #2 }
}
\cs_generate_variant:Nn \egreg_mathlist:nnn { nnV }
\ExplSyntaxOff
\newcommand{\edgelist}{(v_{1}, v_{2}); (v_{2}, v_{3}); (v_{1}, v_{3});
(v_{2}, v_{5}); (v_{3}, v_{4}); (v_{4}, v_{5})}
\begin{document}
Figure \ref{figure:example graph} shows an example of a graph
$G = (\{\mathlist{,}{v_{1}, \dots, v_{5}}\},
\{\mathlist[;]{,\allowbreak}
{(v_{1}, v_{2});
(v_{2}, v_{3});
(v_{1}, v_{3});
(v_{2}, v_{5});
(v_{3}, v_{4});
(v_{4}, v_{5})}
\})$
with $|V| = n = 5$ vertices and $|E| = m = 6$ edges.
The list of edges can also be obtained by
$\mathlist*[;]{,\allowbreak}{\edgelist}$.
\end{document}
```

## 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: