我想展示一个图形,其中在 LaTeX 中有一个图形表示,其边缘和节点列表上方。我写了类似这样的内容:
\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}
输出http://imageshack.com/a/img196/7216/y3ms.png
我的问题是,如果第二个子图环境中的 E(G) 数学表达式太长,我希望它能够跨越多行。如果可能的话,我希望标题“第二个图”也位于页面中央。
我该如何解决我的问题?
谢谢。
答案1
另一种选择:使用array
第二个数字。
代码
\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}
答案2
可以实现边列表的自动换行,但需要猜测合适的宽度。
如果您没有subfig
因为文档类限制而被迫使用(例如IEEEtran
),我相信最好使用subcaption
,它\subcaptionbox
与 类似,\subfloat
因为它不需要指定宽度。对于边列表,alignedat
可以使用。
\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}
答案3
例如,通过使用multlined
环境(包mathtools
)或包(其为扩展)aligned
的环境。amsmath
mathtools
\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}
顺便说一句,你的代码在 中有错误>=stealth'
,引用似乎不合适,或者缺少另一个引用(我对 tikz 了解不多)。我选择撤回它。提供一个最小工作示例 (MWE) 肯定会更好。
答案4
这是使用堆栈的一种方法。
\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}
\\
只需在 中移动换行符 ( ) \Longunderstack
,就可以实现这一点: