如何在两个图表下添加标题(每个图表一个)?以下是代码
\begin{center}
\tikzset{ellipsenode/.style={draw, ellipse, thick, text width=5ex, align=center, inner sep=2pt}}
\begin{tikzpicture}[node distance={1mm}, thick]
\node (center) [circle] {};
\node (1) at ($(center)+(240:25mm)$)[ellipsenode] {$y_{1}$};
\node (2) at ($(center)+(300:25mm)$)[ellipsenode] {$y_{2}$};
\node (3) at ($(center)$)[ellipsenode] {$y_{3}$};
\node (4) at ($(center)+(60:25mm)$)[ellipsenode] {$y_{4}$};
\node (5) at ($(center)+(120:25mm)$)[ellipsenode] {$y_{5}$};
\draw[-] (1) -- (3);
\draw[-] (1) -- (2);
\draw[-] (2) -- (3);
\draw[-] (4) -- (3);
\draw[-] (5) -- (3);
\draw[-] (5) -- (4);
\end{tikzpicture}
\hspace{30mm}
\tikzset{ellipsenode/.style={draw, ellipse, thick, text width=5ex, align=center, inner sep=2pt}}
\begin{tikzpicture}[node distance={1mm}, thick]
\node (center) [circle] {};
\node (1) at ($(center)+(240:25mm)$)[ellipsenode] {$y_{1}$};
\node (2) at ($(center)+(300:25mm)$)[ellipsenode] {$y_{2}$};
\node (3) at ($(center)$)[ellipsenode] {$y_{3}$};
\node (4) at ($(center)+(60:25mm)$)[ellipsenode] {$y_{4}$};
\node (5) at ($(center)+(120:25mm)$)[ellipsenode] {$y_{5}$};
\draw[-] (1) -- (3);
\draw[-] (1) -- (2);
\draw[-] (2) -- (3);
\draw[-] (5) -- (3);
\end{tikzpicture}
\end{center}
答案1
我假设您希望有两张带有自己标题的平行图片。您可以通过将图片放入tabularx
表格中轻松实现这一点:
在下面的 MWE(最小工作示例)中,使用环境保留它们在文档中的插入center
,并使用包\captionof {figure}{<caption text>}
中定义的标题caption
,或者您可以使用小包capt-of
:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,
shapes.geometric}
\usepackage{caption}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\begin{document}
\begin{center}
\tikzset{ellipsenode/.style = {% common style for nodes
ellipse, draw, thick, text width=2em, align=center, inner sep=2pt},
node distance = 21mm and 7mm
}
\begin{tabularx}{\linewidth}{@{} C @{\hspace{30mm}} C @{}}
\begin{tikzpicture}
\begin{scope}[nodes=ellipsenode]
\node (1) {$y_{1}$};
\node (2) [above right=of 1] {$y_{3}$};
\node (3) [below right=of 2] {$y_{2}$};
\node (4) [above left=of 2] {$y_{5}$};
\node (5) [above right=of 2] {$y_{4}$};
\end{scope}
%
\draw (1) -- (3) -- (2) -- (1)
(2) -- (4) -- (5) -- (2);
\end{tikzpicture}
\captionof{figure}{Caption of left diagram}
\label{fig:diagram-left}
&
\begin{tikzpicture}
\begin{scope}[nodes=ellipsenode]
\node (1) {$y_{1}$};
\node (2) [above right=of 1] {$y_{3}$};
\node (3) [below right=of 2] {$y_{2}$};
\node (4) [above left=of 2] {$y_{5}$};
\node (5) [above right=of 2] {$y_{4}$};
\end{scope}
%
\draw (1) -- (3) -- (2) -- (1)
(2) -- (4);
\end{tikzpicture}
\captionof{figure}{Caption of right diagram}
\label{fig:diagram-right}
\end{tabularx}
\end{center}
\end{document}
但是,如果您希望将图形浮动到文档中的最佳位置,您只需替换center
并figure
使用标准标题命令:
% preamble
\begin{figure}[ht]
\tikzset{ellipsenode/.style = {% common style for nodes
ellipse, draw, thick, text width=2em, align=center, inner sep=2pt},
node distance = 21mm and 7mm
}
\begin{tabularx}{\linewidth}{@{} C @{\hspace{30mm}} C @{}}
\begin{tikzpicture}
\begin{scope}[nodes=ellipsenode]
\node (1) {$y_{1}$};
\node (2) [above right=of 1] {$y_{3}$};
\node (3) [below right=of 2] {$y_{2}$};
\node (4) [above left=of 2] {$y_{5}$};
\node (5) [above right=of 2] {$y_{4}$};
\end{scope}
%
\draw (1) -- (3) -- (2) -- (1)
(2) -- (4) -- (5) -- (2);
\end{tikzpicture}
\caption{Caption of left diagram}
\label{fig:diagram-left}
&
\begin{tikzpicture}
\begin{scope}[nodes=ellipsenode]
\node (1) {$y_{1}$};
\node (2) [above right=of 1] {$y_{3}$};
\node (3) [below right=of 2] {$y_{2}$};
\node (4) [above left=of 2] {$y_{5}$};
\node (5) [above right=of 2] {$y_{4}$};
\end{scope}
%
\draw (1) -- (3) -- (2) -- (1)
(2) -- (4);
\end{tikzpicture}
\caption{Caption of right diagram}
\label{fig:diagram-right}
\end{tabularx}
\end{figure}
我擅自更改了您的图表代码:
- 定义了两张图片的共同节点样式,
- 通过使用
positioning
Ti钾Z 库节点彼此相对定位。 - 节点按以下方式分组
scope
- 节点之间的连接以两个循环的形式绘制
现在代码稍微短一点。
答案2
最好的方法是在 -environment\caption
中使用figure
。使用方式如下:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{figure}[ht!]
\centering
\tikzset{ellipsenode/.style={draw, ellipse, thick, text width=5ex, align=center, inner sep=2pt}}
\begin{tikzpicture}[node distance={1mm}, thick]
\node (center) [circle] {};
\node (1) at ($(center)+(240:25mm)$)[ellipsenode] {$y_{1}$};
\node (2) at ($(center)+(300:25mm)$)[ellipsenode] {$y_{2}$};
\node (3) at ($(center)$)[ellipsenode] {$y_{3}$};
\node (4) at ($(center)+(60:25mm)$)[ellipsenode] {$y_{4}$};
\node (5) at ($(center)+(120:25mm)$)[ellipsenode] {$y_{5}$};
\draw[-] (1) -- (3);
\draw[-] (1) -- (2);
\draw[-] (2) -- (3);
\draw[-] (4) -- (3);
\draw[-] (5) -- (3);
\draw[-] (5) -- (4);
\end{tikzpicture}
\caption{Some cool figure}
\end{figure}
\begin{figure}[ht!]
\centering
\tikzset{ellipsenode/.style={draw, ellipse, thick, text width=5ex, align=center, inner sep=2pt}}
\begin{tikzpicture}[node distance={1mm}, thick]
\node (center) [circle] {};
\node (1) at ($(center)+(240:25mm)$)[ellipsenode] {$y_{1}$};
\node (2) at ($(center)+(300:25mm)$)[ellipsenode] {$y_{2}$};
\node (3) at ($(center)$)[ellipsenode] {$y_{3}$};
\node (4) at ($(center)+(60:25mm)$)[ellipsenode] {$y_{4}$};
\node (5) at ($(center)+(120:25mm)$)[ellipsenode] {$y_{5}$};
\draw[-] (1) -- (3);
\draw[-] (1) -- (2);
\draw[-] (2) -- (3);
\draw[-] (5) -- (3);
\end{tikzpicture}
\caption{Another cool figure}
\end{figure}
\end{document}
您还可以将数字放在每个数字旁边:将两幅图像放在一起
这回答了你的问题吗?