我目前使用 TikZ 矩阵来排列图像及其节点,以便在边缘放置一些标签。看看附图。出于某种原因,我无法移动连接的路径。例如,我想将垂直线向左移动,这样它就不会交叉tux A
或tux B
标记。然而,[xshift=]
奇怪的是,不会引起任何转变。
\documentclass{article}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\begin{document}
\begin{tikzpicture}[font=\sffamily\small,ampersand replacement=\&]
\matrix [column sep=0mm,row sep=0mm,inner sep=0mm]{%
\node(i1){\includegraphics{linux-pinguin.png}}; \&
\node(i2){\includegraphics{linux-pinguin.png}};\\
\node(i3){\includegraphics{linux-pinguin.png}}; \&
\node(i4){\includegraphics{linux-pinguin.png}};\\
\node(i5){\includegraphics{linux-pinguin.png}}; \&
\node(i6){\includegraphics{linux-pinguin.png}};\\
};
\node [rotate=90] at (i3.west) {tux B};
\node [rotate=90] at (i5.west) {tux A};
\draw [xshift=-2cm] (i5.south west) -- (i3.north west) node [rotate=90,midway,above] {benevolent};
\draw (i2.north west) -- (i2.north east) node [midway,above] {1 col};
\end{tikzpicture}
\end{document}
答案1
这坐标变换 xshift
(和类似的)不适用于节点(或它们的锚点)。
但您可以在坐标规范内进行转换:
\draw[green] ([shift={(-8mm,0mm)}] i5.south west) -- node [sloped,above] {benevolent}
([xshift=-8mm] i3.north west);
这无需calc
图书馆就能工作。
这画布变换确实适用于节点,但有点棘手,例如它不会更新边界框。使用xshift=-16mm
) 测试下面发布的 MWE 中的以下示例,亲自查看:
\draw[
red,
transform canvas={
xshift=+5mm
}
] (i5.south west) -- node [sloped,above] {benevolent} (i3.north west);
检查PGF 手册更加容易引起混淆(见 22.3 节“坐标变换”和 22.4 节“画布变换”)。
不过,在你的例子中,我会使用不同的方法来获得你想要的线条。使用角落
(i3.north west -| tuxB.north)
和(tuxA.north |- i5.south west)
在节点上方放置一条线。
现在,该线与两个较小节点之间的间距与它自己的节点之间的间距相同(您可以通过draw
查看节点来检查这一点)。
优点:不会弄乱一些随机距离,并且如果您移动节点,它也会随之移动(请参见下文)。
在“1 col”示例中,您从上角画了一条线并将节点放置在above
其中。
在“tux A”和“tux B”的例子中,您将节点直接放置在 处.west
,即图像的边界处,而不是above
它。如果您移除%
前面的above
,您可以看到“仁慈”线也会移动。
代码
\documentclass[tikz,border=2pt]{standalone}
\begin{document}
\begin{tikzpicture}[font=\sffamily\small,ampersand replacement=\&]
\matrix [column sep=0mm,row sep=0mm,inner sep=0mm]{%
\node(i1){\includegraphics{linux-pinguin.png}}; \&
\node(i2){\includegraphics{linux-pinguin.png}};\\
\node(i3){\includegraphics{linux-pinguin.png}}; \&
\node(i4){\includegraphics{linux-pinguin.png}};\\
\node(i5){\includegraphics{linux-pinguin.png}}; \&
\node(i6){\includegraphics{linux-pinguin.png}};\\
};
\draw (i2.north west) -- (i2.north east) node [midway,above] {1 col};
\node [rotate=90,
% above
] at (i3.west) (tuxB) {tux B};
\node [rotate=90,
% above
] at (i5.west) (tuxA) {tux A};
% without shifting:
\draw (i3.north west -| tuxB.north) -- (tuxA.north |- i5.south west) node [rotate=90,midway,above] {benevolent};
% with shifting A
\draw[green] ([shift={(-8mm,0mm)}]i5.south west) -- node [sloped,above] {benevolent} ([xshift=-8mm]i3.north west);
% with shifting B
\draw[red,transform canvas={xshift=+5mm}] (i5.south west) -- node [sloped,above] {benevolent} (i3.north west);
\end{tikzpicture}
\end{document}
输出
答案2
我增加了,inner sep
以便您获得一些空间。您可以xshift
在之前rotate
或yshift
之后使用rotate
:
代码
\documentclass{article}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\begin{document}
\begin{tikzpicture}[font=\sffamily\small,ampersand replacement=\&]
\matrix [column sep=0mm,row sep=0mm,inner sep=0mm,inner sep=4mm]{% %% inner sep here
\node(i1){\includegraphics{linux-pinguin.png}}; \&
\node(i2){\includegraphics{linux-pinguin.png}};\\
\node(i3){\includegraphics{linux-pinguin.png}}; \&
\node(i4){\includegraphics{linux-pinguin.png}};\\
\node(i5){\includegraphics{linux-pinguin.png}}; \&
\node(i6){\includegraphics{linux-pinguin.png}};\\
};
\node [xshift=2mm,rotate=90,] at (i3.west) {tux B}; %% <--- Here
\node [rotate=90,yshift=-2mm] at (i5.west) {tux A}; %% <--- and here
\draw [xshift=-2cm] (i5.south west) -- (i3.north west) node [rotate=90,midway,above] {benevolent};
\draw (i2.north west) -- (i2.north east) node [midway,above] {1 col};
\end{tikzpicture}
\end{document}
另一种方法
您可以使用calc
库:
\documentclass{article}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{positioning,calc}
\begin{document}
\begin{tikzpicture}[font=\sffamily\small,ampersand replacement=\&]
\matrix [column sep=0mm,row sep=0mm,inner sep=0mm]{%
\node(i1){\includegraphics{linux-pinguin.png}}; \&
\node(i2){\includegraphics{linux-pinguin.png}};\\
\node(i3){\includegraphics{linux-pinguin.png}}; \&
\node(i4){\includegraphics{linux-pinguin.png}};\\
\node(i5){\includegraphics{linux-pinguin.png}}; \&
\node(i6){\includegraphics{linux-pinguin.png}};\\
};
\node [rotate=90,] at (i3.west) {tux B};
\node [rotate=90] at (i5.west) {tux A};
\draw ($(i5.south west)+(-2mm,0)$) -- ($(i3.north west)+(-2mm,0)$) node [rotate=90,midway,above] {benevolent}; %% <--- here
\draw (i2.north west) -- (i2.north east) node [midway,above] {1 col};
\end{tikzpicture}
\end{document}