![将子标题与 tikz 图片垂直对齐](https://linux22.com/image/237734/%E5%B0%86%E5%AD%90%E6%A0%87%E9%A2%98%E4%B8%8E%20tikz%20%E5%9B%BE%E7%89%87%E5%9E%82%E7%9B%B4%E5%AF%B9%E9%BD%90.png)
一个简单的后续问题如何在子图中放置和缩放 TikZ 图片?我厚颜无耻地复制了答案中提供的相同代码,因为它完全符合我的需要。
也许代码不需要在这里重复,因为可以从上面的链接中看到。无论如何,这就是它的输出。(本来想在这里插入图像,但作为新用户,我似乎无法插入,请查看链接)。
看起来不错,但我希望只使用“(a)”、“(b”) 等作为副标题。例如,我可以通过修改子图行到“\subfigure []”来实现这一点。当我这样做时,图片下方的字母不会与第一个节点(图片中“树”的根)垂直对齐。我尝试在节点下方/上方的 tikzpicture 中添加标签,但效果并不理想,而且感觉很不合时宜。有什么简单的方法可以实现这一点?
答案1
两个选项:
第一种方法(我更喜欢这种方法)使用subcaption
包在 内部节点的 bax 中生成子标题,这为您提供了很大的灵活性(例如,您可以将子标题放在图片的任何位置,以更好地利用空白区域)。如果您需要使用包或依赖 来缩放整个图片,tikzpicture
则这种方法将不起作用,因为这也会缩放子标题。subfigure
\resizebox
创建用于保存子标题的节点可以包含在如下命令中
\newcommand{\alignedsublabel}[2]{%
\node at ($(current bounding box.south west)!(#1)!(current bounding box.south east)$) [anchor=base,text depth=0pt,yshift=-3ex] {\parbox{10em}{\subcaption[]{#2}}};%
}
以下是您所链接的问题中的设置的一个简化示例:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{caption,subcaption}
\usetikzlibrary{arrows,automata}
\newcommand{\alignedsublabel}[1]{%
\node at ($(current bounding box.south west)!(#1)!(current bounding box.south east)$) [anchor=base,text depth=0pt,yshift=-3ex] {\parbox{10em}{\subcaption[]{}}};%
}
\begin{document}
\begin{figure}[htbp]
\begin{tikzpicture}[node distance=2.0cm,semithick]
\node[state] (A) {1};
\node[state] (B) [below left of = A] {2};
\node[state] (C) [below right of = A] {3};
\node[state] (D) [below right of = C] {4};
\draw [gray,ultra thick] (current bounding box.north west) rectangle (current bounding box.south east);
\alignedsublabel{A.center}{}
\end{tikzpicture}
%
\hfill
\begin{tikzpicture}[node distance=2.0cm,semithick]
\node[state] (A) {1};
\node[state] (B) [below left of = A] {2};
\node[state] (C) [below right of = A] {3};
\node[state] (D) [below right of = C] {4};
\draw [gray,ultra thick] (current bounding box.north west) rectangle (current bounding box.south east);
\alignedsublabel{A.center}
\end{tikzpicture}
\caption{Several options}
\end{figure}
\end{document}
如果您确实依赖于\resizebox
内容,则需要将标签保留在 之外tikzpicture
。为了正确对齐,最准确的做法是扩展 的边界框,tikzpicture
使其围绕指定中心对称。这可以通过path
在 的末尾指定 来完成tikzpicture
,可以使用库进行计算calc
。缺点是这会在图片侧面引入额外的空白空间:
\documentclass{article}
\usepackage{tikz}
\usepackage{subfigure}
\usepackage{array}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\usetikzlibrary{calc,automata}
\newcommand{\centeraround}[1]{
\path (#1) -- +($($(current bounding box.east)!(#1)!(current bounding box.west)$) - (current bounding box.east)$);
}
\begin{document}
\begin{figure}[htbp]
\begin{tabular}{C{.48\textwidth}C{.48\textwidth}}
\subfigure [] {
\resizebox{0.4\textwidth}{!}{%
\begin{tikzpicture}[node distance=2.0cm,semithick]
\node[state] (A) {1};
\node[state] (B) [below left of = A] {2};
\node[state] (C) [below right of = A] {3};
\node[state] (D) [below right of = C] {4};
\draw [gray,ultra thick] (current bounding box.north west) rectangle (current bounding box.south east);
\centeraround{A.center}
\end{tikzpicture}
}
} &
\subfigure [] {
\resizebox{0.4\textwidth}{!}{%
\begin{tikzpicture}[node distance=2.0cm,semithick]
\node[state] (A) {1};
\node[state] (B) [below left of = A] {2};
\node[state] (C) [below right of = A] {3};
\node[state] (D) [below right of = C] {4};
\draw [gray,ultra thick] (current bounding box.north west) rectangle (current bounding box.south east);
\centeraround{A.center}
\end{tikzpicture}
}
} \\
\end{tabular}
\caption{Several options}
\end{figure}
\end{document}