在下面的代码片段中,我期望节点垂直对齐。但看起来锚点被忽略了。
\documentclass{article}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{positioning,matrix}
\newcommand{\imgwidth}{2cm}
\begin{document}
\begin{tikzpicture}
\node[inner sep=5, draw, rectangle] (baz) {\includegraphics[height=\imgwidth,width=\imgwidth]{example-grid-100x100pt}};
%
\node[inner sep=5, draw, rectangle, anchor=north west, below=of baz.south west] (bar)
{\includegraphics[width=\imgwidth,height=\imgwidth]{example-grid-100x100pt}};
%
\node[inner sep=5, draw, rectangle, anchor=north west, below=of bar.south west] (foo)
{\includegraphics[width=\imgwidth,height=\imgwidth]{example-grid-100x100pt}};
\end{tikzpicture}
\end{document}
为什么布局中不使用锚点?
答案1
正如我在评论中提到的,锚点必须位于节点选项的最后一个位置(稍微缩短的代码):
\documentclass{article}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{positioning,matrix}
\newcommand{\imgwidth}{2cm}
\begin{document}
\begin{tikzpicture}[every node/.style={rectangle, draw, inner sep=5}]
\node (baz)
{\includegraphics[height=\imgwidth,width=\imgwidth]{example-grid-100x100pt}};
%
\node[below=of baz.south west, anchor=north west] (bar)
{\includegraphics[width=\imgwidth,height=\imgwidth]{example-grid-100x100pt}};
%
\node[below=of bar.south west,anchor=north west] (foo)
{\includegraphics[width=\imgwidth,height=\imgwidth]{example-grid-100x100pt}};
\end{tikzpicture}
\end{document}
但是,使用更简单的代码就可以获得相同的结果:
\documentclass{article}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{positioning}
\newcommand{\imgwidth}{2cm}
\begin{document}
\begin{tikzpicture}[every node/.style={rectangle, draw, inner sep=5}]
\node (baz) {\includegraphics[height=\imgwidth,width=\imgwidth]{example-grid-100x100pt}};
%
\node[below=of baz] (bar)
{\includegraphics[width=\imgwidth,height=\imgwidth]{example-grid-100x100pt}};
%
\node[below=of bar] (foo)
{\includegraphics[width=\imgwidth,height=\imgwidth]{example-grid-100x100pt}};
\end{tikzpicture}
\end{document}