Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to have inline math text like

Figure \ref{figure:example graph} shows an example of a graph $G = (\{v_{1}, \dots, v_{5}\}, \{(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.

but then, LaTeX messes with the linebreak and the rendered text looks like in the attached screenshot.

enter image description here

Is there any way, I can tell LaTeX to break the formula within simple $'s?

share|improve this question

migrated from stackoverflow.com Oct 22 '12 at 14:08

This question came from our site for professional and enthusiast programmers.

3 Answers 3

up vote 9 down vote accepted

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.

enter image description here


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}

enter image description here

share|improve this answer
2  
For completeness, by default LaTeX is willing to break in-line formulae, but only after relations (e.g. =) and binary operators (e.g. +) not enclosed between {..}. \allowbreak inserts extra break points. –  Andrew Swann Oct 22 '12 at 14:32

The simplest way to achieve what you want is:

\documentclass{article}

\begin{document}
Figure \ref{figure:example graph} shows an example of a graph 
$G = (\{v_{1}, \dots, v_{5}\}$, 
 $\{(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}

enter image description here

share|improve this answer
2  
+1 This is the simplest to write, I agree. However the spaces after commas is not uniform as it is in the other solutions. On the other hand, the list of edges might be generated via a macro; the method in my answer can be easily extended to cope with this case. –  egreg Oct 22 '12 at 20:21
    
a +1 from egreg only gives me ten points? not 1000? ;-) –  jfbu Oct 22 '12 at 20:50
    
Sorry. Complain with the Powers That Be. :-) –  egreg Oct 22 '12 at 20:51

You have to tell LaTeX that it is allowable to break at (say) a ,. One option is to provide a list parser to insert a breakable point at every ,. That's provided by this version of \mathlist:

enter image description here

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\makeatletter
% https://groups.google.com/d/msg/comp.text.tex/PWt75pamgO8/Btouz_IsZq4J
\newcommand{\mathlist}[2][,]{% \mathlist[<separator>]{<csv list>}
  \begingroup
  % count the elements
  \@tempcnta=\z@
  \renewcommand*\do[1]{\advance\@tempcnta\@ne}
  \docsvlist{#2}% Process list first time
  \chardef\@listsize\@tempcnta
  \@tempcnta=\@ne
  \renewcommand*\do[1]{
    \ifnum\@tempcnta<\@listsize
      ##1#1\penalty\z@ \advance\@tempcnta\@ne
    \else
      \unpenalty\penalty\@M ##1
    \fi}
  \docsvlist{#2}% Process list second time
  \endgroup
}
\makeatother\begin{document}
Figure~1 shows an example of a graph $G = (\{v_{1}, \dotsc, v_{5}\}, 
\{(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.

Figure~1 shows an example of a graph $G = (\mathlist{\{v_{1}, \dotsc, v_{5}\}}, 
\{\mathlist{(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}

\mathlist parsers the comma-separated list twice. First to find the number of items, and second to set them; this time with a zero penalty after ,. The last item is not allowed to break.

If you're not concerned about the last item being breakable/not, you can use the following (slightly shorter) definition of \mathlist:

\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\makeatletter
\newcommand{\mathlist}[2][,]{% \mathlist[<separator>]{<csv list>}
  \begingroup
  \def\@temp{\penalty\z@}% Put penalty in single token
  \toks@={\@gobble}% Gobble first separator
  \renewcommand*\do[1]{\toks@=\@xp{\the\toks@#1\@temp##1}}
  \docsvlist{#2}% Process list
  \the\toks@% Print list
  \endgroup
}
\makeatother

etoolbox provides the list-parser \docsvlist, while amsmath provides \@xp (\expandafter) and \dotsc (for "comma dots").

\mathlist can be supplied with an optional first argument which changes the output from the default , to whatever you supply.

share|improve this answer
    
Sorry - does not work for me. I get ! You can't use `\spacefactor' in math mode. \@->\spacefactor \@m l.17 ... $G = (\mathlist{\{v_{1}, \dotsc, v_{5}\}} , \{\mathlist{(v_{1}, v_{2... ! Undefined control sequence. \\mathlist [#1]#2->\begingroup \@tempcnta=\z @ \renewcommand *\do [1]{\advan... l.17 ... $G = (\mathlist{\{v_{1}, \dotsc, v_{5}\}} –  Michael Knoll Oct 22 '12 at 8:07
    
@MichaelKnoll: Since the macro \mathlist contains @ symbols, you need to surround it with a \makeatletter...\makeatother pair. Did you do that? –  Werner Oct 22 '12 at 14:13
    
The code in the comp.text.tex message you're citing deals with a different case, where every comma is eligible as break point. This modification seems too complicated and error prone. –  egreg Oct 22 '12 at 14:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.