
如何取得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}