TikZ: how to memorize current coordinate into a variable during path creation

I am creating electrical circuits using CircuiTikZ.

Quite often I need to connect to a certain point, which has been created previously using relative coordinates.

Right now I have to calculate these coordinates in advance. Instead, I want to be able to create a "coordinate variable" during the path creation, and use this variable later. Another way would be to extract the coordinates of a bipole terminal.

Needed result Solution 1 (absolute coordinates)

This solution is unacceptable, because it leads to a very fragile code.

\begin{circuitikz}
\draw (0,0) to [R=$R$] ++(2,0) to [R=$R$] ++(2,0);
\draw (0,0) -- (0,1) to [R=$R$] (4,1) -- (4,0);
\end{circuitikz}

Solution 2 (using arcane math)

This is my current solution. Obviously it looks simple and no big deal here because it's an MWE, but in my schemes it gets pretty complicated.

\newcommand{\mylength}{2}
\begin{circuitikz}
\draw (0,0) to [R=$R$] ++(\mylength,0) to [R=$R$] ++(\mylength,0);
\draw (0,0) -- (0,1) to [R=$R$] (\mylength*2,1) -- (\mylength*2,0);
\end{circuitikz}

Preferred solution №1 (anchors)

Unfortunately this does not work, because the anchor is on the bipole itself, and not on the terminal. (\currentcoordinate marco taken from here).

\makeatletter
\newcommand\currentcoordinate{\the\tikz@lastxsaved,\the\tikz@lastysaved}
\makeatother
\begin{circuitikz}
\draw (0,0) to [R=$R$] ++(2,0) to [R=$R$, name=MyResistor] ++(2,0);
\draw (0,0) -- (0,1) to [R=$R$] (MyResistor.east |- \currentcoordinate) -- (MyResistor.east);
\end{circuitikz}

Preferred solution №2 (memorizing coords in a variable)

This would be perfect.

\begin{circuitikz}
\draw (0,0) to [R=$R$] ++(2,0) to [R=$R$, name=MyResistor] ++(2,0) [\MyVariable=\currentcoordinate];
\draw (0,0) -- (0,1) to [R=$R$] (\MyVariable |- \currentcoordinate) -- (\MyVariable);
\end{circuitikz}
• Can't you just replace in your last snippet [\MyVariable=\currentcoordinate] by coordinate (X) and use X thereafter? You would help us a lot if you would provide a complete MWE for that, which we could just copy.
– user121799
Feb 21 '19 at 0:17

Named coordinates already exist in TikZ. They even have nicer syntax than what you propose.

\documentclass{standalone}
\usepackage{circuitikz}
\begin{document}
\begin{circuitikz}
\draw (0,0) to [R=$R$] ++(2,0) to [R=$R$, name=MyResistor] ++(2,0) coordinate (lr);
\draw (0,0) -- (0,1) coordinate (ul) to [R=$R$] (lr |- ul) -- (lr);
\end{circuitikz}
\end{document} You could also just draw the whole thing on a single path and not use named coordinates at all.

\documentclass{standalone}
\usepackage{circuitikz}
\begin{document}
\begin{circuitikz}
\draw ( 0,0) to [R=$R$]
++( 2,0) to [R=$R$]
++( 2,0) to [short]
++( 0,1) to [R,l_=$R$]
++(-4,0) to[short]
( 0,0) -- cycle;
\end{circuitikz}
\end{document}
• coordinate is exactly what I needed. Feb 21 '19 at 1:07