我经常发现自己写的代码如下:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\node (aNodeWithALongName) at (1,1) {a};
\node (bNodeWithALongName) at (1,-1) {b};
\node (c) at (0,0) {c};
\draw[->] (aNodeWithALongName) -- (aNodeWithALongName -| c);
\draw[->] (bNodeWithALongName) -- (bNodeWithALongName -| c);
\end{tikzpicture}
\end{document}
我正在绘制一条路径,需要使用第一个点的位置来计算第二个点,并且我想避免重复aNodeWithALongName
,例如这样:
\draw[->] (aNodeWithALongName) -- (current -| c);
\draw[->] (bNodeWithALongName) -- (current -| c);
还有更复杂的东西:
\draw[->] ($(aNodeWithALongName)!.5!(bNodeWithALongName)$) -- (current -| c);
\draw[->] (bNodeWithALongName) -- ++(0, 1cm) -- (current -| c);
是否有类似的语法可供我使用?
答案1
我找到了解决方案这个 TeX-SX 答案:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\makeatletter
\newcommand\currentcoordinate{\the\tikz@lastxsaved,\the\tikz@lastysaved}
\makeatother
\begin{document}
\begin{tikzpicture}
\node (aNodeWithALongName) at (1,1) {a};
\node (bNodeWithALongName) at (1,-1) {b};
\node (c) at (0,0) {c};
\draw[->] (aNodeWithALongName) -- (\currentcoordinate -| c);
\draw[->] (bNodeWithALongName) -- (\currentcoordinate -| c);
\draw[->] ($(aNodeWithALongName)!.5!(bNodeWithALongName)$) -- (\currentcoordinate -| c);
\draw[->] (bNodeWithALongName) -- ++(0, 1cm) -- (\currentcoordinate -| c);
\end{tikzpicture}
\end{document}