0

Is it possible, within a TikZ path, to refer to the "current coordinate" as another coordinate?

To make it clear what I want, take this example, which I often end up doing because I don't know better:

\documentclass[tikz,margin=1cm]{standalone}
\begin{document}
\begin{tikzpicture}
\coordinate (origin) at (0,0);
% suppose the "1,1" is a very complicate (possibly unknown) coordinate
% that I would love not to repeat in the code:
\draw (origin) -| (1,1) coordinate (THIS) -- (THIS-|origin);
% This is what I would like to do:
% \draw (origin) -| (1,1) -- (THIS-|origin);
\end{tikzpicture}
\end{document}

What I end up doing is:

\draw (origin) -| (1,1) coordinate (THIS) -- (THIS-|origin);

because I (say) want to draw a straight line from the current point to what would be a point perpendicular to another. But it is a pain to type coordinate (THIS) all the time, and I would like to know if there is a magical internal coordinate that would allow me to do:

\draw (origin) -| (1,1) -- (THIS-|origin);

and THIS would know that it means 1,1.

Does THIS exist, or what I'm doing is the best I can get?

1 Answer 1

2

If you are willing to use to instead of --, then the last coordinate is stored in \tikztostart, and can be used in the same way you use THIS.

\documentclass[tikz,margin=1cm]{standalone}
\begin{document}
\begin{tikzpicture}
\coordinate (origin) at (0,0);
% suppose the "1,1" is a very complicate (possibly unknown) coordinate
% that I would love not to repeat in the code:
% This is what I would like to do:
% \draw (origin) -| (1,1) -- (THIS-|origin);
 \draw (origin) -| (1,1)  to (\tikztostart-|origin);
\end{tikzpicture}
\end{document}

enter image description here

Of course, you can also use \THIS.

\documentclass[tikz,margin=1cm]{standalone}
\begin{document}
\begin{tikzpicture}
\coordinate (origin) at (0,0);
\def\THIS{\tikztostart}
% suppose the "1,1" is a very complicate (possibly unknown) coordinate
% that I would love not to repeat in the code:
 \draw (origin) -| (1,1)  to (\THIS-|origin);
% This is what I would like to do:
% \draw (origin) -| (1,1) -- (THIS-|origin);
\end{tikzpicture}
\end{document}
6
  • Thanks, that's nice, but it doesn't work for me because I want to continue the path after that (as I said, it's only an example), so it has to work with -- or other path construction operations
    – LaTeXer
    Nov 29, 2020 at 0:20
  • @LaTeXer You can continue the path after to just like after --. Try e.g. \draw (origin) -| (1,1) to (\tikztostart-|origin) -- ++ (0,1);.
    – user229669
    Nov 29, 2020 at 0:21
  • I feel stupid (epic facepalm). Thanks a LOT! I was confusing to with edge, that stops the path. Is there any downside of using to rather than --?
    – LaTeXer
    Nov 29, 2020 at 0:26
  • @LaTeXer As long as the usage is very basic, there is no downside. Once you start to play with keys like every to, then to will not necessarily be a straight path any more (because you asked for that). Strictly speaking to is also a tiny bit slower than -- (since among other things it defines \tikztostart) but this should not be noticeable.
    – user229669
    Nov 29, 2020 at 0:30
  • Thanks! I'm definitely not expert enough to need every to, and speed is not a problem, as I'm using TikZ anyway rather than some reasonable graphics application 😉 Thanks a again
    – LaTeXer
    Nov 29, 2020 at 0:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .