我有以下 tikz 圖:
\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\coordinate (origin) at (0,0) ;
\node[above=10pt of origin,anchor=east] (topnode) {Short};
\node[below=10pt of origin,anchor=east] (bottomnode) {Longer Node};
\end{tikzpicture}
\end{document}
我的目標是在位置 (x,y) 放置一個節點,y=0,但 x 等於兩個節點 (topnode) 和 (bottomnode) 中最左邊的節點。我想在不對位置進行硬編碼的情況下執行此操作。這樣,如果我更改這兩個節點的內容,位置仍然是正確的。
答案1
已知local bounding box
內部所有內容的左/右/頂部/底部座標。
\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\coordinate (origin) at (0,0) ;
\begin{scope}[local bounding box=nodes]
\node[above=10pt of origin,anchor=east] (topnode) {Short};
\node[below=10pt of origin,anchor=east] (bottomnode) {Longer Node};
\end{scope}
\node[anchor=west] at (origin-|nodes.west) {here I am};
\end{tikzpicture}
\end{document}
答案2
另一種方式:使用\pgfgetlastxy
:
\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}
\newdimen\xone
\newdimen\y
\newdimen\xtwo
\begin{document}
\begin{tikzpicture}
\coordinate (origin) at (0,0) ;
\node[above=10pt of origin,anchor=east,inner sep=0pt] (topnode) {Short};
\node[below=10pt of origin,anchor=east,inner sep=0pt] (bottomnode) {Longer Node};
\path (topnode.west);
\pgfgetlastxy{\xone}{\y};
\path (bottomnode.west);
\pgfgetlastxy{\xtwo}{\y};
\draw ({min(\xone,\xtwo)},0) circle (1pt);
\end{tikzpicture}
\end{document}
我在想要的地方畫了一個圓。您可以將其更改為您想要的任何內容,甚至可以使用它作為座標: