![tikz 节点尺寸](https://linux22.com/image/357772/tikz%20%E8%8A%82%E7%82%B9%E5%B0%BA%E5%AF%B8.png)
如何获取tikz
节点尺寸(例如高度和宽度)?我有兴趣使用这些参数来精确计算对象的位置,例如<a.height>
在以下示例中使用:
\documentclass{article}
\thispagestyle{empty}
\usepackage{graphicx}
\usepackage{tikz}
\begin{document}
\begin{figure}
\begin{tikzpicture}
\node(a) {
\includegraphics[width=0.8\textwidth]{example-image-a}
};
\node(b) at (0, <a.height>*3/4){label};
\end{tikzpicture}
\end{figure}
\end{document}
答案1
两个示例,在不知道节点尺寸的情况下获得所需结果。第二个使用calc
tikzlibrary
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\node(a) {
\includegraphics[width=0.8\textwidth]{example-image-a}
};
\path (a.center)--(a.north) node[pos=.75, right] {label without calc};
\node[left] at ($(a.center)!.75!(a.north)$) {label with calc};
\end{tikzpicture}
\end{document}
更新:
假设我们希望标签label without calc
从上例中的实际位置向右移动 2 厘米,向上移动 1 厘米:\path (a.center)--(a.north) node[pos=.75, right] {label without calc}
。我们可以使用positioning
库将节点相对于路径上的相应点移动。在这种情况下,我们可以说above right=1cm and 2cm
但这将使用south west
锚点进行放置,并且我们想要west
,然后我们在选项anchor=west
后添加:above right
\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc, positioning}
\begin{document}
\begin{tikzpicture}
\node[inner sep=0pt] (a) {
\includegraphics[width=0.8\textwidth]{example-image-a}
};
\path (a.center)--(a.north) node[draw, pos=.75, above right=1cm and 2cm, anchor=west, red] (b) {label without calc};
\node[left] (c) at ($(a.center)!.75!(a.north)$) {label with calc};
%Some auxiliary elements.
\draw[red,<->] (a.center)--(c.east) node[midway, right] {75\%};
\draw[red,<->] (c.east)--(a.north) node[midway,right] {25\%};
\fill[red] (c.east) circle (1pt);
\draw[red,<->] (c.east)-|(b.west) node[pos=0.25,below] {2 cm} node[pos=0.75,left] {1 cm};
\end{tikzpicture}
\end{document}