当将包含 TikZ 图片本身的多个节点相对于彼此定位时(使用positioning
TikZ 库),似乎所包含的图片中的节点略微未对齐:
该图片由以下代码生成
\documentclass{article}
\usepackage{tikz}
\usepackage{fp}
\usetikzlibrary{positioning}
\tikzstyle{vertex}=[circle, fill=black, minimum size=2pt, inner sep=0pt]
\newcommand{\makeTikzTree}[1]{%
\FPeval{\result}{#1/2}
\begin{tikzpicture}[thick, scale=0.4]
\draw (0, 0) -- (0, -1) node[vertex]{};
\ifodd#1
\draw (0, 1) node[vertex]{} -- (0, 0);
\foreach \x in {1, ..., \result} {
\draw (-\x, 1) node[vertex]{} -- (0, 0);
\draw (\x, 1) node[vertex]{} -- (0, 0);
}
\else
\foreach \x in {0.5, ..., \result} {
\draw (-\x, 1) node[vertex]{} -- (0, 0);
\draw (\x, 1) node[vertex]{} -- (0, 0);
}
\fi
\end{tikzpicture}
}
\begin{document}
\begin{center}
\begin{tikzpicture}
\node (tree1) {\makeTikzTree{5}};
\node [right=of tree1] (tree2) {\makeTikzTree{4}};
\node [right=of tree2] (tree3) {\makeTikzTree{7}};
\end{tikzpicture}
\end{center}
\end{document}
注意第一棵树的对齐方式很好。这是怎么回事?如果为节点指定绝对位置,但使用相对定位却不行,为什么这样做可以正常工作?
答案1
不幸的是,您的代码嵌套了tikzpicture
s。应避免这种情况,因为环境图片的 pgf 键的值会被继承。您也不需要这样做,Ti钾Z 有pic
s ,用于此目的。即便如此,您仍需要确保 不会vertex
从定位中继承锚点。right
意味着锚点是west
,这解释了所有顶点都连接到其左侧。您可以通过添加到anchor=center
的选项来避免这种情况vertex
。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{center}
\begin{tikzpicture}[vertex/.style={circle, fill=black, minimum
size=2pt,inner sep=0pt,anchor=center},
pics/little tree/.style={code={
\pgfmathtruncatemacro{\xmax}{#1/2}
\draw (0, 0) -- (0, -1) node[vertex]{};
\ifodd#1
\draw (0, 1) node[vertex]{} -- (0, 0);
\foreach \x in {1, ..., \xmax} {
\draw (-\x, 1) node[vertex]{} -- (0, 0);
\draw (\x, 1) node[vertex]{} -- (0, 0);
}
\else
\foreach \x in {0.5, ..., \xmax} {
\draw (-\x, 1) node[vertex]{} -- (0, 0);
\draw (\x, 1) node[vertex]{} -- (0, 0);
}
\fi}}]
\node[matrix] (tree1) {\pic[scale=0.4]{little tree=5};\\};
\node[matrix,right=of tree1] (tree2) {\pic[scale=0.4]{little tree=4};\\};
\node[matrix,right=of tree2] (tree3) {\pic[scale=0.4]{little tree=7};\\};
\end{tikzpicture}
\end{center}
\end{document}
答案2
就像薛定谔的猫一样,我会将您的树实现为pic
。您可以将代码简化为:
\tikzset{
vertex/.style={circle, fill=black, minimum size=2pt, inner sep=0pt},
pics/tree/.style = {
code = {
\begin{scope}[scale=0.4, thick]
\node[vertex] (0) at (0,0){};
\draw(0)--(0,1);
\foreach \x in {1,...,#1} {
\node[vertex] (n\x) at (\x-#1/2-1/2,2){};
\draw(0,1)--(n\x);
}
\end{scope}
}
}
}
特别注意,您不需要\FPeval
或 使用\ifodd
。此外,\tikzstyle
已贬值,取而代之的是\tikzset
。
有了这个,我从示例中对定位库的理解是,以下代码应该可以工作
\begin{tikzpicture}
\pic (tree1) at (0,0){tree=5};
\pic[right=of tree1] (tree2) {tree=4};
\pic[right=of tree2] (tree3) {tree=7};
\end{tikzpicture}
但出于我不明白的原因,这会导致错误。positioning library
我更喜欢手动种植树木,而不是使用:
或者,你可以使用薛定谔猫的巧妙技巧:
\begin{tikzpicture}
\node[matrix] (tree1) {\pic{tree=5};\\};
\node[matrix,right=of tree1] (tree2) {\pic{tree=4};\\};
\node[matrix,right=of tree2] (tree3) {\pic{tree=7};\\};
\end{tikzpicture}
完整代码如下:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset{
vertex/.style={circle, fill=black, minimum size=2pt, inner sep=0pt},
pics/tree/.style = {
code = {
\begin{scope}[scale=0.4, thick]
\node[vertex] (0) at (0,0){};
\draw(0)--(0,1);
\foreach \x in {1,...,#1} {
\node[vertex] (n\x) at (\x-#1/2-1/2,2){};
\draw(0,1)--(n\x);
}
\end{scope}
}
}
}
\begin{document}
\begin{center}
\begin{tikzpicture}
\pic at (0,0){tree=5};
\pic at (2,0){tree=4};
\pic at (4.5,0){tree=7};
\end{tikzpicture}
\end{center}
% Schrödinger's cat's nice trick
\begin{tikzpicture}
\node[matrix] (tree1) {\pic{tree=5};\\};
\node[matrix,right=of tree1] (tree2) {\pic{tree=4};\\};
\node[matrix,right=of tree2] (tree3) {\pic{tree=7};\\};
\end{tikzpicture}
\end{document}