# Line breaks of math expressions in subfloat environment

I would like to show a figure in which there are a graph representation above its edge and node lists in LaTeX. I have written something like this:

\begin{figure}[!ht]

\subfloat[First figure]{
\centering
\begin{tikzpicture}[>=stealth', shorten >=1pt, auto, node distance=3cm,
thick, main node/.style={circle,draw,font=\sffamily\Large\bfseries}]

\node[main node] (v1) at (-2,2) {2};
\node[main node] (v2) at (2,2) {3};
\node[main node] (v3) at (4,0) {6};
\node[main node] (v4) at (2,-2) {4};
\node[main node] (v5) at (-2,-2) {5};
\node[main node] (v6) at (-4,0) {1};
\draw  (v1) edge (v2);
\draw  (v2) edge (v3);
\draw  (v3) edge (v4);
\draw  (v4) edge (v5);
\draw  (v5) edge (v6);
\draw  (v6) edge (v1);
\draw  (v5) edge (v1);
\draw  (v5) edge (v2);
\draw  (v4) edge (v2);
\end{tikzpicture}
}

\subfloat[Second figure]{
$V(G) = \{1, 2, 3, 4, 5, 6\}$
$E(G) = \{(1, 2), (2, 1), (1, 5), (5, 1), (2, 5), (5, 2), (2, 3), (3, 2), (5, 3), (3, 5), (5, 4), (4, 5), (3, 4), (4, 3), (3, 6), (6, 3), (4, 6), 6, 5)\}$
}
\caption[Graph representation]{Graph representation.}
\label{fig:graphexample}

\end{figure}


My problem is that I would like the math expression for E(G) in the second subfig environment to span along multiple lines if it's too long. If possible, I would like the caption "Second figure" to be centered on the page, too.

How could I solve my problem?

Thank you.

-

Another alternative: use of array for the second figure.

Code

\documentclass{article}
\usepackage{tikz,subfig}
\usetikzlibrary{arrows}

\begin{document}

\begin{figure}[!ht]
\centering
\subfloat[First figure]{
\begin{tikzpicture}[>=stealth', shorten >=1pt, auto, node distance=3cm, thick,
main node/.style={circle,draw,font=\sffamily\Large\bfseries}]
\node[main node] (v1) at (-2,2) {2};
\node[main node] (v2) at (2,2) {3};
\node[main node] (v3) at (4,0) {6};
\node[main node] (v4) at (2,-2) {4};
\node[main node] (v5) at (-2,-2) {5};
\node[main node] (v6) at (-4,0) {1};
\draw  (v1) edge (v2);
\draw  (v2) edge (v3);
\draw  (v3) edge (v4);
\draw  (v4) edge (v5);
\draw  (v5) edge (v6);
\draw  (v6) edge (v1);
\draw  (v5) edge (v1);
\draw  (v5) edge (v2);
\draw  (v4) edge (v2);
\end{tikzpicture}
}\\
\subfloat[Second figure]{$\begin{array}{lll} V(G) &=& \{1, 2, 3, 4, 5, 6\}\\ E(G) &=& \{(1, 2), (2, 1), (1, 5), (5, 1), (2, 5),(5, 2), \\ &&(2, 3), (3, 2), (5, 3), (3, 5), (5, 4), (4, 5),\\ &&(3, 4), (4, 3), (3, 6), (6, 3), (4, 6), 6, 5)\} \end{array}$
}
\caption[Graph representation]{Graph representation.}
\label{fig:graphexample}
\end{figure}
\end{document}

-

It would be possible to get automatic line breaking for the list of edges, but a suitable width needs to be guessed at.

If you're not forced to use subfig because of document class constraints (say, IEEEtran), I believe it's better to use subcaption and its \subcaptionbox that's similar to \subfloat in that it doesn't need to specify a width. For the list of edges, alignedat can be used.

\documentclass{article}

\usepackage{amsmath}
\usepackage{subcaption}
\usepackage{tikz}
\usetikzlibrary{arrows}

\begin{document}

\begin{figure}[htp]
\centering

\subcaptionbox{First figure}{%
\begin{tikzpicture}[
>=stealth',
shorten >=1pt,
auto,
node distance=3cm,
thick,
main node/.style={circle,draw,font=\sffamily\Large\bfseries}
]
\node[main node] (v1) at (-2,2) {2};
\node[main node] (v2) at (2,2) {3};
\node[main node] (v3) at (4,0) {6};
\node[main node] (v4) at (2,-2) {4};
\node[main node] (v5) at (-2,-2) {5};
\node[main node] (v6) at (-4,0) {1};
\draw  (v1) edge (v2);
\draw  (v2) edge (v3);
\draw  (v3) edge (v4);
\draw  (v4) edge (v5);
\draw  (v5) edge (v6);
\draw  (v6) edge (v1);
\draw  (v5) edge (v1);
\draw  (v5) edge (v2);
\draw  (v4) edge (v2);
\end{tikzpicture}%
}

\medskip

\subcaptionbox{Second figure}{%
\!\begin{alignedat}{2} V(G) &= \{&& 1, 2, 3, 4, 5, 6\} \\ E(G) &= \{&& (1, 2), (2, 1), (1, 5), (5, 1), (2, 5), (5, 2),\\ &&& (2, 3), (3, 2), (5, 3), (3, 5), (5, 4), (4, 5),\\ &&& (3, 4), (4, 3), (3, 6), (6, 3), (4, 6), 6, 5)\} \end{alignedat}%
}
\caption[Graph representation]{Graph representation.}
\label{fig:graphexample}

\end{figure}

\end{document}


-

By using the multlined environment (package mathtools) or the aligned environment of package amsmath (of which mathtools is an extension), for example.

\documentclass[12pt]{article}

\usepackage{amsmath}
\usepackage{subfig, tikz}

\begin{document}

\begin{figure}[!ht]
\centering
\subfloat[First figure]{%
\begin{tikzpicture}[>=stealth, shorten >=1pt, auto, node distance=3cm,
thick, main node/.style={circle,draw,font=\sffamily\Large\bfseries}]
\node[main node] (v1) at (-2,2) {2};
\node[main node] (v2) at (2,2) {3};
\node[main node] (v3) at (4,0) {6};
\node[main node] (v4) at (2,-2) {4};
\node[main node] (v5) at (-2,-2) {5};
\node[main node] (v6) at (-4,0) {1};
\draw  (v1) edge (v2);
\draw  (v2) edge (v3);
\draw  (v3) edge (v4);
\draw  (v4) edge (v5);
\draw  (v5) edge (v6);
\draw  (v6) edge (v1);
\draw  (v5) edge (v1);
\draw  (v5) edge (v2);
\draw  (v4) edge (v2);
\end{tikzpicture}}

\subfloat[Second figure]{%
\begin{aligned}[t] V(G) &= \{1, 2, 3, 4, 5, 6\},\\ E(G) &= \begin{aligned}[t]\{% & (1, 2), (2, 1), (1, 5), (5, 1), (2, 5),\\ & (5, 2), (2, 3),(3, 2), (5, 3), (3, 5),\\ & (5, 4), (4, 5), (3, 4), (4, 3),\\ & (3, 6), (6, 3), (4, 6), (6, 5)\} \end{aligned} \end{aligned}}

\caption[Graph representation]{Graph representation.}
\label{fig:graphexample}
\end{figure}

\end{document}


By the way, your code had an error in >=stealth', the quote seems to be out of place, or another quote is missing (I don't know much about tikz). I chose to withdraw it. It would certainly have been better to provide a Minimal Working Example (MWE).

-
would look even nicer if the two expressions were themselves aligned at the equals signs. – barbara beeton Mar 15 '14 at 15:44
@barbarabeeton Done! :-) – Franck Pastor Mar 15 '14 at 16:22
another suggestion ... consider centering everything, so that both parts are lined up with the (apparently centered) caption. (sorry i didn't think of this before.) – barbara beeton Mar 15 '14 at 18:36
@barbarabeeton Again, agreed and done! – Franck Pastor Mar 15 '14 at 18:56

Here's one way, with stacks.

\documentclass{article}
\usepackage{subfig}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usepackage[usestackEOL]{stackengine}
\stackMath
\begin{document}
\begin{figure}[!ht]
\centering
\subfloat[First figure]{
\begin{tikzpicture}[>=stealth', shorten >=1pt, auto, node distance=3cm,
thick, main node/.style={circle,draw,font=\sffamily\Large\bfseries}]

\node[main node] (v1) at (-2,2) {2};
\node[main node] (v2) at (2,2) {3};
\node[main node] (v3) at (4,0) {6};
\node[main node] (v4) at (2,-2) {4};
\node[main node] (v5) at (-2,-2) {5};
\node[main node] (v6) at (-4,0) {1};
\draw  (v1) edge (v2);
\draw  (v2) edge (v3);
\draw  (v3) edge (v4);
\draw  (v4) edge (v5);
\draw  (v5) edge (v6);
\draw  (v6) edge (v1);
\draw  (v5) edge (v1);
\draw  (v5) edge (v2);
\draw  (v4) edge (v2);
\end{tikzpicture}
}

\subfloat[Second figure]{
\def\stackalignment{l}
\stackunder[8pt]{\kern-.3pt$V(G) = \{1, 2, 3, 4, 5, 6\}$}
{$E(G) = \Longunderstack[r]{\{(1, 2), (2, 1), (1, 5), (5, 1), (2, 5),\\ (5, 2), (2, 3), (3, 2), (5, 3), (3, 5),\\ (5, 4), (4, 5), (3, 4), (4, 3), (3, 6),\\ (6, 3), (4, 6), (6, 5)\}\kern-2.3pt}$
}}
\caption[Graph representation]{Graph representation.}
\label{fig:graphexample}

\end{figure}
\end{document}


By merely shifting the linebreaks (\\) around in the \Longunderstack, one could achieve this:

-
Thank you for your answer. Interesting, but pdflatex gives me this error: "! LaTeX Error: Unknown option usestackEOL' for package stackengine'.". I am using TexLive on Windows. Do you know how I could solve it? – JohnQ Mar 15 '14 at 13:24
@JohnQ Your package is out of date. Latest can be found at ctan.org/tex-archive/macros/latex/contrib/stackengine – Steven B. Segletes Mar 15 '14 at 13:46
Ah ok, thank you. – JohnQ Mar 15 '14 at 14:06