![为两个并排的图表添加标题](https://linux22.com/image/421202/%E4%B8%BA%E4%B8%A4%E4%B8%AA%E5%B9%B6%E6%8E%92%E7%9A%84%E5%9B%BE%E8%A1%A8%E6%B7%BB%E5%8A%A0%E6%A0%87%E9%A2%98.png)
如何在每个图表下添加标题,同时保持它们彼此相邻?
\documentclass{article}
\usepackage{tikz}
\begin{document}
\usetikzlibrary{positioning}
\tikzset{main node/.style={circle,fill=black,draw,minimum size=.2cm,inner sep=0pt},}
\begin{center}
\begin{tikzpicture}
\node[main node, label=above:{$a$},] (1) {};
\node[main node, label=right:{$b$}] (2) [below right = 1cm and 1cm of 1] {};
\node[main node, label=left:{$c$}] (3) [below left = 1cm and 1cm of 1] {};
\path[draw, thick]
(1) edge node {} (2)
(2) edge node {} (3)
(1) edge node {} (3);
\end{tikzpicture} \hspace{2cm}
\begin{tikzpicture}
\node[main node, label=left:{$a$},] (1) {};
\node[main node, label=left:{$b$}] (2) [below = 1cm of 1] {};
\node[main node, label=right:{$c$}] (3) [right = 1cm of 1] {};
\node[main node, label=right:{$d$}] (4) [below = 1 cm of 3] {};
\path[draw, thick]
(1) edge node {} (2)
(2) edge node {} (4)
(3) edge node {} (4)
(1) edge node {} (3);
\end{tikzpicture}
\end{center}
\end{document}
答案1
有很多方法可以实现这一点。对于完全没有标题的情况,您可以使用 M S 的答案,但不使用环境figure
,minipage
而是使用 而不是subfigure
,并在 之后直接写入文本\end{tikzpicture}
,没有\caption
。不过,请在文本前添加一个空行(段落分隔符)。
这里我展示了一种完全不同的方法,使用单个tikzpicture
,第二个图表用 向右移动\begin{scope}[xshift=5cm, local bounding box=b]
。local bounding box
创建一个适合 内容的节点scope
,我使用它来定位\node
和文本。
另一个细节:一般不要\usetikzlibrary{..}
在之后使用\begin{document}
。在这里似乎不会造成问题,但我见过它不起作用的情况,所以最好在序言中加载库。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\tikzset{main node/.style={circle,fill=black,draw,minimum size=.2cm,inner sep=0pt},}
\begin{center}
\begin{tikzpicture}[
captiontext/.style={below=3mm, text width=5cm}
]
\node[main node, label=above:{$a$}] (1) {};
\node[main node, label=right:{$b$}, below right = 1cm and 1cm of 1] (2) {};
\node[main node, label=left:{$c$}, below left = 1cm and 1cm of 1] (3) {};
\draw [thick] (1) -- (2) -- (3) -- (1);
\node [captiontext] at (current bounding box.south) {Text text and more text ad infinitum and so on.};
\begin{scope}[xshift=5cm, local bounding box=b]
\node[main node, label=left:{$a$},] (1) {};
\node[main node, label=left:{$b$},below = 1cm of 1] (2) {};
\node[main node, label=right:{$c$}, right = 1cm of 1] (3) {};
\node[main node, label=right:{$d$}, below = 1 cm of 3] (4) {};
\draw [thick] (1) -- (2) -- (4) -- (3) -- (1);
\end{scope}
\node [captiontext] at (b.south) {More text text and more text ad infinitum and so on going on for a bit.};
\end{tikzpicture}
\end{center}
\end{document}
答案2
您可以使用两个并排的小页面。要解决的问题是让标题出现在同一水平,即使图片和标题的垂直尺寸不同。
我的解决方案是将图片嵌入嵌套的迷你页面中,并使其底部对齐。外部迷你页面则采用顶部对齐,这实际上意味着它们的参考点将是顶部框的参考点,也就是图片的底部。这会将标题放在页面上的相同垂直位置。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset{
main node/.style={
circle,
fill=black,
draw,
minimum size=.2cm,
inner sep=0pt
},
}
\begin{document}
\begin{figure}
\begin{minipage}[t]{0.45\textwidth}
% left column
\begin{minipage}[b]{\textwidth}
\centering
\begin{tikzpicture}
\node[main node, label=above:{$a$},] (1) {};
\node[main node, label=right:{$b$}] (2) [below right = 1cm and 1cm of 1] {};
\node[main node, label=left:{$c$}] (3) [below left = 1cm and 1cm of 1] {};
\path[draw, thick]
(1) edge node {} (2)
(2) edge node {} (3)
(1) edge node {} (3);
\end{tikzpicture}
\end{minipage}
\caption{This is the figure on the left, with a not so lengthy caption}
\end{minipage}\hfill % <--- don't forget
\begin{minipage}[t]{0.45\textwidth}
% right column
\begin{minipage}[b]{\textwidth}
\centering
\begin{tikzpicture}
\node[main node, label=left:{$a$},] (1) {};
\node[main node, label=left:{$b$}] (2) [below = 1cm of 1] {};
\node[main node, label=right:{$c$}] (3) [right = 1cm of 1] {};
\node[main node, label=right:{$d$}] (4) [below = 1 cm of 3] {};
\path[draw, thick]
(1) edge node {} (2)
(2) edge node {} (4)
(3) edge node {} (4)
(1) edge node {} (3);
\end{tikzpicture}
\end{minipage}
\caption{This is the figure on the right, with a long caption,
that should form at least three lines; we add text so that
it's long enough}
\end{minipage}
\end{figure}
\end{document}
答案3
希望下面提到的代码能满足您的需求。它基本上是一个带有两个子标题的图形。如果您需要两个带有标题的不同图形,请告诉我。
根据您最近的评论。您可以评论主图标题部分。
\documentclass{article}
\usepackage{tikz,subcaption}
\begin{document}
\usetikzlibrary{positioning}
\tikzset{main node/.style={circle,fill=black,draw,minimum size=.2cm,inner sep=0pt},}
\begin{figure}
\centering
\begin{subfigure}[b]{0.4\linewidth}
\centering
\begin{tikzpicture}
\node[main node, label=above:{$a$},] (1) {};
\node[main node, label=right:{$b$}] (2) [below right = 1cm and 1cm of 1] {};
\node[main node, label=left:{$c$}] (3) [below left = 1cm and 1cm of 1] {};
\path[draw, thick]
(1) edge node {} (2)
(2) edge node {} (3)
(1) edge node {} (3);
\end{tikzpicture}
\caption{caption1} \label{fig:M1}
\end{subfigure}
\begin{subfigure}[b]{0.4\linewidth}
\centering
\begin{tikzpicture}
\node[main node, label=left:{$a$},] (1) {};
\node[main node, label=left:{$b$}] (2) [below = 1cm of 1] {};
\node[main node, label=right:{$c$}] (3) [right = 1cm of 1] {};
\node[main node, label=right:{$d$}] (4) [below = 1 cm of 3] {};
\path[draw, thick]
(1) edge node {} (2)
(2) edge node {} (4)
(3) edge node {} (4)
(1) edge node {} (3);
\end{tikzpicture}
\caption{caption2} \label{fig:M2}
\end{subfigure}
\caption{main caption}
\end{figure}
\end{document}
输出: