Tikz shorten with circuitikz

circuitikztikz-pgf

in circuitikz double bipoles are drawn as nodes (not via to[...]), but sometimes one needs voltage arrows for double bipoles as well.

As I didn't find a way to do this directly (with circuitikz), I decided to do it manually. So I came up with \draw (pol.B1) to node[currarrow] {} (pol.B2);. With this path the arrow obviously goes from wire to wire which is kinda ugly.

So I tried to shorten the arrow and came up with

\documentclass[margin=1cm]{standalone}

\usepackage{tikz}
\usepackage{circuitikz}

\begin{document}
\begin{tikzpicture}
    \draw (0,0) node[transformer core] (pol) {};
    \draw[shorten <=.5cm, red] (pol.B1) to node[currarrow,sloped,pos=1] {} (pol.B2);
\end{tikzpicture}

\begin{tikzpicture}
    \draw (0,0) node[transformer core] (pol) {};
    \draw[blue] (pol.B1) to[shorten <=.5cm] node[currarrow,sloped,pos=1] {} (pol.B2);
\end{tikzpicture}

\end{document}

This results in
Result

well I guess the blue try does not work as shorten ... is a key that has to be specified at \draw[...] and not at to[...] since it shortens the whole path, not a segment of the path.

Just to be sure, is this right?

But then I'm still confused by the lines at (1) and (2) why are they there? (without shorten these lines are not present) And how can I get rid of them?

EDIT: And in addition the question, is there a way to shorten the segment created by to (to avoid that one always has to begin a new path to create the arrow)?
I came up with

\tikzset{
    skip/.style={
        to path={
            ($(\tikztostart)!#1!(\tikztotarget)$) --
            ($(\tikztotarget)!#1!(\tikztostart)$)
            \tikztonodes
        }
    }
}

but I'm wondering if there is a nicer way.

Best Answer

Basically, you have a couple of possibilities here. The first (my suggested one) is to use the transformer's anchor to add a voltage over an open bipole (that will maintain the general look of the voltages in the circuits). Like this:

\documentclass[margin=1cm]{standalone}

\usepackage{tikz}
\usepackage{circuitikz}

\begin{document}
\begin{tikzpicture}
    \draw (0,0) node[transformer core](pol) {};
    \draw ([xshift=0.2cm]pol.outer dot B2)
        to[open, voltage=straight, v=$v_2$]
          ([xshift=0.2cm]pol.outer dot B1);
\end{tikzpicture}
\end{document}

enter image description here

With this option, you can use also the techniques shown in "advanced voltages..." in the manual to have reference points where to put real TikZ arrows.

The other option, if you need to add several "decorations" to the single inductances, is to draw the transformer "manually" --- the double dipoles are there just as a convenience for the most common applications, but using two inductances is not difficult, and you have plenty of anchors to position them and the core lines.

On the other hand, the problem with the extra stroke is a bug in circuitikz: I (well, circuitikz authors) am filling a path without correctly closing it and it leaks somehow when shorten is used. I will try to fix it asap, but for now you can check with the workaround below.

\documentclass[margin=1cm]{standalone}

\usepackage{tikz}
\usepackage{circuitikz}
\makeatletter
\pgfdeclareshape{currarrow}{
    \savedanchor{\northeast}{%
        \pgf@circ@res@step = \pgf@circ@Rlen
        \divide \pgf@circ@res@step by \ctikzvalof{current arrow scale}
        \pgf@x=.5\pgf@circ@res@step
        \pgf@y=\pgf@x%
    }
    \anchor{north}{\northeast\pgf@x=0cm\relax}
    \anchor{east}{\northeast\pgf@y=0cm\relax}
    \anchor{south}{\northeast\pgf@y=-\pgf@y \pgf@x=0cm\relax}
    \anchor{west}{\northeast\pgf@y=0cm\pgf@x=-\pgf@x}
    \anchor{north east}{\northeast}
    \anchor{north west}{\northeast\pgf@x=-\pgf@x}
    \anchor{south east}{\northeast\pgf@y=-\pgf@y}
    \anchor{south west}{\northeast\pgf@y=-\pgf@y\pgf@x=-\pgf@x}
    \anchor{center}{
        \pgfpointorigin
    }
    \anchor{tip}{
        \pgfpointorigin
        \pgf@circ@res@step = \pgf@circ@Rlen
        \divide \pgf@circ@res@step by \ctikzvalof{current arrow scale}
        \pgf@x  =\pgf@circ@res@step
    }
    \behindforegroundpath{
        \pgfscope
            \ifpgfcirc@really@draw@currarrow
                \pgf@circ@res@step = \pgf@circ@Rlen
                \divide \pgf@circ@res@step by \ctikzvalof{current arrow scale}
                \pgfpathmoveto{\pgfpoint{-.7\pgf@circ@res@step}{0pt}}
                \pgfpathlineto{\pgfpoint{-.7\pgf@circ@res@step}{-.8\pgf@circ@res@step}}
                \pgfpathlineto{\pgfpoint{1\pgf@circ@res@step}{0pt}}
                \pgfpathlineto{\pgfpoint{-.7\pgf@circ@res@step}{.8\pgf@circ@res@step}}
                %\pgfpathlineto{\pgfpoint{-.7\pgf@circ@res@step}{0pt}}
                \pgfpathclose
                \pgfsetcolor{\ctikzvalof{color}}
                \pgfusepath{draw,fill}
            \fi
        \endpgfscope
    }
}
\makeatother

\begin{document}
\begin{tikzpicture}
    \draw (0,0) node[transformer core] (pol) {};
    \draw[shorten <=.5cm, red] (pol.B1) to node[currarrow,sloped,pos=1] {} (pol.B2);
\end{tikzpicture}

\begin{tikzpicture}
    \draw (0,0) node[transformer core] (pol) {};
    \draw[blue] (pol.B1) to[shorten <=.5cm] node[currarrow,sloped,pos=1] {} (pol.B2);
\end{tikzpicture}

\end{document}

Notice however that pos=1 still refers to the unshortened line (this is a TikZ thing, not circuitikz-specific) so probably the result is not the one you are expecting when using

\draw[shorten <=.5cm, shorten >=0.5cm, color=red] (pol.B1)
     to node[currarrow,sloped,pos=1] {} (pol.B2);