假设我有一个节点,其中包含一个按宽度缩放的图像(比例未知)。因此高度未知。现在我想在其左侧添加 2 个节点,其 y 和高度与此“未知高度图片”相同
所以这:
MWE(实际上 5 厘米的高度是未知的)
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node[draw,minimum height=5cm,minimum width=5cm] (scaledImageHeightUnknown) at (2cm,2cm) {Image};
\node[draw] (textLeftOfScaledImage) at (scaledImageHeightUnknown.north west)[anchor=north west] {Directly left of Image};
\node[draw] (textLeftOfText) at (textLeftOfScaledImage.north west) {Directly left of Text};
\end{tikzpicture}
\end{document}
生成:
答案1
只是阐述@ignasi的简短评论推荐使用内部 sep=0pt 的拟合节点。使其适合所需节点的北锚点和南锚点,固定最小宽度并将其移动或锚定到您想要的位置。
\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{fit}
\pgfdeclareimage[width=5cm]{somepicture}{example-image-a}
\begin{document}
\begin{tikzpicture}
\node[inner sep=0mm, outer sep=0mm ,draw] (pic) {\pgfuseimage{somepicture}};
\node[inner sep=0mm, outer sep=0mm, draw,
fit=(pic.north)(pic.south),minimum width=1cm,anchor=east,
label=center:B] (leftpic) at (pic.west) {};
\node[inner sep=0mm, outer sep=0mm, draw,
fit=(pic.north)(pic.south),minimum width=1cm,anchor=east,
label=center:C] (leftleftpic) at (leftpic.west) {};
\end{tikzpicture}
\end{document}
编辑:按照@ignasi 的第二条评论,我们使用label=center:...
定位标签而不是将它们放在命令末尾的括号之间\node
,以正确居中它们。
答案2
像这样?
\documentclass[border=3mm,tikz]{standalone}
\usetikzlibrary{calc,positioning}
\usepackage{graphicx}
\begin{document}
\begin{tikzpicture}[
node distance = 0pt,
box/.style = {draw,inner ysep=0pt,outer sep=0pt,
minimum height=#1}
]
\node (image) [inner sep=0pt] {\includegraphics[scale=0.3]{example-image}};
\path let \p1 = ($(image.north)-(image.south)$),
\n1 = {veclen(\y1,\x1)} in
node (n1) [box=\n1-2\pgflinewidth,left=of image]
{\rotatebox{90}{box left of image}}
node (n2) [box=\n1-2\pgflinewidth,left=of n1]
{\rotatebox{90}{box left of Text}};
\end{tikzpicture}
\end{document}
首先,我将图像包含在节点中,然后测量节点高度,该值用于定义左框(最小)高度。此节点中的文本由包\rotatebox
中的帮助宏旋转。graphicx