我有以下抽象代码。我不知道为什么文本test
没有居中。你知道如何让它居中吗?
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, math, fit, backgrounds}
\begin{document}
\begin{tikzpicture}
\node [draw = black] {A TEST};
\coordinate (a) at (current bounding box.south east); \draw (a) circle (3pt);
\coordinate (b) at ([yshift = -15 pt]current bounding box.south west);
\node (MEM) [inner sep = 0, fit = (a) (b), fill=black!20!red] {test};
\end{tikzpicture}
\end{document}
结果:
答案1
这是 Ti 的预定义行为钾Z,请参阅 pgfmanual 的第 52 节。您可以通过将另一个节点放置在 MEM 节点的中心来将“测试”放置在节点的中心。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning, math, fit, backgrounds}
\begin{document}
\begin{tikzpicture}
\node [draw = black] {A TEST};
\coordinate (a) at (current bounding box.south east); \draw (a) circle (3pt);
\coordinate (b) at ([yshift = -15 pt]current bounding box.south west);
\draw (b) circle (3pt);
\node (MEM) [inner sep = 0, fit = (a) (b), fill=black!20!red] {};
\node (test) at (MEM.center){test};
\end{tikzpicture}
答案2
在“fited”节点中,文本基线位于节点的中心。如果您希望文本垂直居中,可以使用label=center:Test
如下方法
\documentclass[tikz,border=7pt]{standalone}
\usetikzlibrary{fit}
\begin{document}
\begin{tikzpicture}
\node [draw = black] {A TEST};
\coordinate (a) at (current bounding box.south east); \draw (a) circle (3pt);
\coordinate (b) at ([yshift = -15 pt]current bounding box.south west);
\node (MEM) [inner sep = 0, fit = (a) (b), fill=black!20!red, label = center:Test]{};
\end{tikzpicture}
\end{document}