我想知道是否可以将节点的底部与特定点对齐。我已经有代码了,但它有两个问题:首先对齐不完美,我不知道为什么,而且代码写起来也不是很自然(也许库已经帮我做了计算?)。
目标:像那样对齐 C 框,没有错误(并且可能有一个不错的代码):
梅威瑟:
\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usetikzlibrary{fit}
\usetikzlibrary{decorations.pathmorphing}
\begin{document}
\begin{tikzpicture}[
box/.style={
rounded corners,
minimum width=2cm,
align=center,
fill=red!50!white,
},
]%
\node[box] (A) {$A$};
\node[box, below=of A] (B) {$B$\\multiline};
\path let \p1=($(A.north)-(B.south)$) in node[box, right=of A.north east,anchor=north west, minimum height={\y1}, inner sep=0pt] (C) {$C$};
\end{tikzpicture}%
\end{document}
答案1
编辑1:
如果你是真的对产生的 0.01411cm 的额外空间感到烦恼,您可以使用以下命令绘制没有边框的框 C:
\node[box,fit=(A)(B), right=of A.north east,anchor=north west,inner sep=-\pgflinewidth/2,outer sep=\pgflinewidth/2, draw=none] (C) {$C$};
或者使用以下默认边框(line width=0.4pt
)厚度:
\node[box,fit=(A)(B), right=of A.north east,anchor=north west,inner sep=-\pgflinewidth,outer sep=\pgflinewidth, draw=blue] (C) {$C$};
笔记:由于我们讨论的是极小的数字,并且 pdf viewer 使用整像素,因此可能会发生舍入感觉对于像素的精度来说它并不完美。
以下是 MWE:
\documentclass[tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usetikzlibrary{fit}
\usetikzlibrary{decorations.pathmorphing}
\begin{document}
\begin{tikzpicture}[
box/.style={
rounded corners,
minimum width=2cm,
align=center,
fill=red!50!white,
},
]%
\node[box] (A) {$A$};
\node[box, below=of A] (B) {$B$\\multiline};
\node[box,fit=(A)(B), right=of A.north east,anchor=north west,inner sep=-\pgflinewidth/2,outer sep=\pgflinewidth/2, draw=none] (C) {$C$};
\node[box,fit=(A)(B), right=of C.north east,anchor=north west,inner sep=-\pgflinewidth,outer sep=\pgflinewidth, draw=blue] (D) {With border};
\end{tikzpicture}%
\end{document}
原来的:
使用fit
您已导入的选项。
\documentclass[tikz, border=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{positioning}
\usetikzlibrary{fit}
\usetikzlibrary{decorations.pathmorphing}
\begin{document}
\begin{tikzpicture}[
box/.style={
rounded corners,
minimum width=2cm,
align=center,
fill=red!50!white,
},
]%
\node[box] (A) {$A$};
\node[box, below=of A] (B) {$B$\\multiline};
\node[box,fit=(A)(B), right=of A.north east,anchor=north west, inner sep=0pt] (C) {$C$};
\end{tikzpicture}%
\end{document}