将 TikZ 绘图并排放置在小页面中

将 TikZ 绘图并排放置在小页面中

我在对齐使用 TikZ 制作的一些绘图时遇到了一些问题:我希望下面这两幅 TikZ 并排显示,并且两个标题在同一水平面上。不幸的是,现在第二幅图的标题出现在第一幅标题的下方,因为第二幅图稍大一些。我如何在第一幅图上方添加一些空白空间,以便两个标题垂直对齐?

\documentclass{article}
\usepackage{tikz}
\usepackage{caption}
\begin{document}
\begin{minipage}{0.6\textwidth}
\begin{center}
\begin{tikzpicture}
[level distance=20mm,
every node/.style={fill=blue!75,circle, draw, inner sep=2pt},
level 1/.style={sibling distance=40mm,nodes={fill=blue!60}},
level 2/.style={sibling distance=20mm,nodes={fill=blue!45}},
level 3/.style={sibling distance=10mm,nodes={fill=blue!35}}]
\node {5}
      child {node {2}}
      child {node {9}};
\end{tikzpicture}
\captionsetup{font=footnotesize}
\captionof{figure}{Caption 1}
\end{center}
\end{minipage}
\begin{minipage}{0.6\textwidth}
\begin{center}
\begin{tikzpicture}
[level distance=20mm,
every node/.style={fill=blue!75,circle, draw, inner sep=2pt},
level 1/.style={sibling distance=40mm,nodes={fill=blue!60}},
level 2/.style={sibling distance=20mm,nodes={fill=blue!45}},
level 3/.style={sibling distance=10mm,nodes={fill=blue!35}}]
\node {1}
      child {node {5}
         child {node {1}}
         child[missing]
      }
      child {node {8}};
\end{tikzpicture}
\captionsetup{font=footnotesize}
\captionof{figure}{Caption 2}
\end{center}
\end{minipage}

\end{document}

答案1

这正是命令\subcaptionbox(来自subcaption套件)的设计目的在于:

\documentclass{article}
\usepackage{tikz}
\usepackage{caption}
\usepackage{subcaption}

\begin{document}

\begin{figure}
\captionsetup[subfigure]{font=footnotesize}
\centering
\subcaptionbox{Caption for first subfigure}[.5\textwidth]{%
\begin{tikzpicture}
[level distance=20mm,
every node/.style={fill=blue!75,circle, draw, inner sep=2pt},
level 1/.style={sibling distance=40mm,nodes={fill=blue!60}},
level 2/.style={sibling distance=20mm,nodes={fill=blue!45}},
level 3/.style={sibling distance=10mm,nodes={fill=blue!35}}]
\node {5}
      child {node {2}}
      child {node {9}};
\end{tikzpicture}}%
\subcaptionbox{Caption for the seconf figure. This will span several lines for the example; in fact, it will span three lines}[.5\textwidth]{\begin{tikzpicture}
[level distance=20mm,
every node/.style={fill=blue!75,circle, draw, inner sep=2pt},
level 1/.style={sibling distance=40mm,nodes={fill=blue!60}},
level 2/.style={sibling distance=20mm,nodes={fill=blue!45}},
level 3/.style={sibling distance=10mm,nodes={fill=blue!35}}]
\node {1}
      child {node {5}
         child {node {1}}
         child[missing]
      }
      child {node {8}};
\end{tikzpicture}}
\end{figure}

\end{document}

在此处输入图片描述

答案2

一种选择是使用[b]位置说明符minipage

\documentclass{article}
\usepackage{tikz}
\usepackage{caption}
\begin{document}
\begin{minipage}[b]{0.6\textwidth}
\centering
\begin{tikzpicture}
[level distance=20mm,
every node/.style={fill=blue!75,circle, draw, inner sep=2pt},
level 1/.style={sibling distance=40mm,nodes={fill=blue!60}},
level 2/.style={sibling distance=20mm,nodes={fill=blue!45}},
level 3/.style={sibling distance=10mm,nodes={fill=blue!35}}]
\node {5}
      child {node {2}}
      child {node {9}};
\end{tikzpicture}
\captionsetup{font=footnotesize}
\captionof{figure}{Caption 1}
\end{minipage}
\begin{minipage}[b]{0.6\textwidth}
\centering
\begin{tikzpicture}
[level distance=20mm,
every node/.style={fill=blue!75,circle, draw, inner sep=2pt},
level 1/.style={sibling distance=40mm,nodes={fill=blue!60}},
level 2/.style={sibling distance=20mm,nodes={fill=blue!45}},
level 3/.style={sibling distance=10mm,nodes={fill=blue!35}}]
\node {1}
      child {node {5}
         child {node {1}}
         child[missing]
      }
      child {node {8}};
\end{tikzpicture}
\captionsetup{font=footnotesize}
\captionof{figure}{Caption 2}
\end{minipage}

\end{document}

在此处输入图片描述

您还可以在 sheight中指定 aminipage并使用\vfillafter \end{tikzpicture},以便两张图片均在顶行对齐:\begin{minipage}[b][0.28\textheight]{0.6\textwidth}

约束:如果两个字幕跨越的行数不同,则第一行不会顶部对齐。

使用tabular*

\documentclass{article}
\usepackage{tikz}
\usepackage{caption}
\begin{document}
\noindent
\begin{tabular*}{\textwidth}{@{}cc@{}}
\begin{minipage}{\dimexpr0.5\textwidth-2\tabcolsep}
\centering
\begin{tikzpicture}
[level distance=20mm,
every node/.style={fill=blue!75,circle, draw, inner sep=2pt},
level 1/.style={sibling distance=40mm,nodes={fill=blue!60}},
level 2/.style={sibling distance=20mm,nodes={fill=blue!45}},
level 3/.style={sibling distance=10mm,nodes={fill=blue!35}}]
\node {5}
      child {node {2}}
      child {node {9}};
\end{tikzpicture}
\end{minipage}% 
&
\begin{minipage}{\dimexpr0.5\textwidth-2\tabcolsep}
\centering
\begin{tikzpicture}
[level distance=20mm,
every node/.style={fill=blue!75,circle, draw, inner sep=2pt},
level 1/.style={sibling distance=40mm,nodes={fill=blue!60}},
level 2/.style={sibling distance=20mm,nodes={fill=blue!45}},
level 3/.style={sibling distance=10mm,nodes={fill=blue!35}}]
\node {1}
      child {node {5}
         child {node {1}}
         child[missing]
      }
      child {node {8}};
\end{tikzpicture}
\end{minipage}
\\
\begin{minipage}[t]{\dimexpr0.6\textwidth-2\tabcolsep}
\captionsetup{font=footnotesize}
\captionof{figure}{Caption 1}
\end{minipage}
&
\begin{minipage}[t]{\dimexpr0.4\textwidth-2\tabcolsep}
\captionsetup{font=footnotesize}
\captionof{figure}{Caption 2 is some long caption that may go for a second line}
\end{minipage}
\end{tabular*}%

\end{document}

在此处输入图片描述

您还可以使用p{...}列说明符和常规tabular代替tabular*

由于您使用了\captionof宏,我假设您不会尝试浮动图片,因此不会显示使用subcaption和的尝试。subfig

相关内容