[Tex/LaTex] TikZ circuits: Resistor is rotated by 90 degrees

tikz-circuit-libtikz-pgf

I have a problem with TikZ circuit library. The following code produces a circuit, but the resistor is rotated by 90 degrees:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{circuits.ee.IEC}

\begin{document}
  \begin{tikzpicture} [circuit ee IEC]
    \node (pow) [battery]   at (0,0)  {};
    \node (res) [resistor]  at (-1,1) {};
    \draw (pow) -| (res) -- (-1,2);
  \end{tikzpicture}
\end{document}

The resistors long side is orthorgonal to the line. How do I change this so that the long side is along the wire?

Best Answer

As you place the nodes before you actually draw the lines connecting these nodes the nodes can have absolutely no knowledge about their rotation. This is only possible with to paths that internally use the markings library which makes it possible that nodes are placed on that line. (See /pgf/decoration/mark connection node at the end of section 30.5 “Arbitrary Markings”, p. 331.)

You can easily construct your path with the following code, note that I used at end to

  • place the battery at (0,0)
  • without having a part of the line behind it (as in your example).

It is not possible to use |- or -| in the same kind of manner without a little effort (see second TikZ picture).

Code

\documentclass[tikz]{standalone}
\usetikzlibrary{circuits.ee.IEC}
\begin{document}
\begin{tikzpicture}[circuit ee IEC]
\begin{scope}[red]
\node (pow) [battery]   at (0,0)  {};
\node (res) [resistor]  at (-1,1) {};
\draw (pow) -| (res) -- (-1,2);
\end{scope}
\draw (-1,2) to[resistor] (-1,0) to[battery={at end}] (0,0);
\end{tikzpicture}

\begin{tikzpicture}[circuit ee IEC]
\begin{scope}[red]
\node (pow) [battery]   at (0,0)  {};
\node (res) [resistor]  at (-1,1) {};
\draw (pow) -| (res) -- (-1,2);
\end{scope}
\draw (-1,2) to[resistor={at end}] (-1,1) to (-1,1 |- 0,0) to [battery={at end}]  (0,0);
\end{tikzpicture}
\end{document}

Output

enter image description here