Circuitikz Spacing – Voltage Shift Command in Circuitikz Package

circuitikzspacing

Im currently trying to change the distance between the components and the voltage arrow.
But im running into problems.

Without shift is the left circuit.
With shift is the right one.

It seems that the voltage shift commands applys relativ to every component, but i dont understand in what way. Every component seems to be the same size as the other component, so i cannot be relativ to their size. It may be relativ to their initial distance?

Is there a way to set the voltage shift to be an absolut value.
Or is there a way to apply the voltage shift command to every single component type?
I could use this:

\draw (2,0) to [R,v=$U_R$, voltage shift = 2] (2,-1)

But i want to change it globally.

MWE:

\documentclass{report}
\usepackage{graphicx} % Required for inserting images

\usepackage{tikz}
\usepackage[european,straightvoltages]{circuitikz}

\ctikzset{resistors/scale=0.55}
\ctikzset{inductors/scale=0.55}
\ctikzset{capacitors/scale=0.6}
\ctikzset{sources/scale=.8}
\ctikzset{bipoles/thickness=1}
\ctikzset{tripoles/thickness=1}
\tikzset{every picture/.style = {transform shape, line width=1pt}}

\begin{document}

\hbox{
    \begin{circuitikz}
     % Switch
    \draw (2,0) node[spdt, rotate=90,anchor=in] (sw) {};
    \draw (sw.out 2)  node[right] {(2)};
    \draw (sw.out 1)  node[left] {(1)};

     %Komponenen
    \draw (0.5,0)  to [V_=$U$] (0.5,-1);
    \draw (2,0) to [R,v=$U_R$] (2,-1) to [L,v=$U_L$] (2,-2);
    \draw (3.5,0) to [C,v=$U_C$] (3.5,-2);

     %Verkabelung
    \draw (0.5,0) to (0.5,1.5) -| (sw.out 1);
    \draw (3.5,0) to (3.5,1.5) -| (sw.out 2);
    \draw (2,-2) to (2,-2.5) -| (0.5,-1);
    \draw (3.5,-2) |- (2,-2.5) node[circ]{};

    \draw (4.5,1) [open,v=$U$] to (4.5,-2);
        
    \end{circuitikz}

    \hspace{2cm}
    
    \ctikzset{voltage shift = 2}
    \begin{circuitikz}
    % Switch
    \draw (2,0) node[spdt, rotate=90,anchor=in] (sw) {};
    \draw (sw.out 2)  node[right] {(2)};
    \draw (sw.out 1)  node[left] {(1)};

    %Komponenen
    \draw (0.5,0)  to [V_=$U$] (0.5,-1);
    \draw (2,0) to [R,v=$U_R$] (2,-1) to [L,v=$U_L$] (2,-2);
    \draw (3.5,0) to [C,v=$U_C$] (3.5,-2);

     %Verkabelung  
    \draw (0.5,0) to (0.5,1.5) -| (sw.out 1);
    \draw (3.5,0) to (3.5,1.5) -| (sw.out 2);
    \draw (2,-2) to (2,-2.5) -| (0.5,-1);
    \draw (3.5,-2) |- (2,-2.5) node[circ]{};

    \draw (4.5,1) [open,v=$U$] to (4.5,-2);
        
    \end{circuitikz}
}

\end{document}

enter image description here

Best Answer

I checked it, and yes, it's a misfeature or a bug if you want.

The code for the voltage label positioning is very old*, with a lot of magic numbers, and I fear that the straightvoltage option has been tested less than desirable.

I tried some simple changes, but the effect on the backward compatibility is impossible to control. So, changes over there will be a "retirement task", I fear, unless somebody steps in to help...

workaround

Newer versions: Since version 1.6.5, there will be no need to do the patching because I added a tunable to workaround the problem; see https://github.com/circuitikz/circuitikz/pull/750 --- now you just use \ctikzset{voltage shift sources adjust=0.2}

(Up to version 1.6.4): You can tune the effect on the shift for voltage sources with the following code by tuning the 0.2 in the patching code (maybe I'll add this to the stock code, although it is a bit of a brown-paper-bag fix). As you can see, independent from that, there is some pesky things going on with straightvoltages and the open component, that I fixed setting its shift to zero in the third example.

\documentclass[border=3mm]{standalone}
\usepackage{graphicx} % Required for inserting images
\usepackage[european,straightvoltages]{circuitikz}
%%%% hack: tune the 0.2 here to control the voltage source shift
%%%% the other bug: now the "open component" misbehave, but that's
%%%% another story
\usepackage{etoolbox}
\makeatletter
\patchcmd{\pgf@circ@drawvoltagegenerator}
    {\pgfmathsetmacro{\bumpaplus}{\bumpa + 0.5*\shiftv}}
    {\pgfmathsetmacro{\bumpaplus}{\bumpa + 0.2*\shiftv}}
    {\relax}{\ERROR}
\makeatother
\ctikzset{resistors/scale=0.55}
\ctikzset{inductors/scale=0.55}
\ctikzset{capacitors/scale=0.6}
\ctikzset{sources/scale=.8}
\ctikzset{bipoles/thickness=1}
\ctikzset{tripoles/thickness=1}
\tikzset{every picture/.style = {transform shape, line width=1pt}}

\newcommand{\circuit}[1][]{%
    \begin{circuitikz}
        % Switch
        \draw (2,0) node[spdt, rotate=90,anchor=in] (sw) {};
        \draw (sw.out 2)  node[right] {(2)};
        \draw (sw.out 1)  node[left] {(1)};
        %Komponenen
        \draw (0.5,0)  to [V_=$U$] (0.5,-1);
        \draw (2,0) to [R,v=$U_R$] (2,-1) to [L,v=$U_L$] (2,-2);
        \draw (3.5,0) to [C,v=$U_C$] (3.5,-2);
        %Verkabelung
        \draw (0.5,0) to (0.5,1.5) -| (sw.out 1);
        \draw (3.5,0) to (3.5,1.5) -| (sw.out 2);
        \draw (2,-2) to (2,-2.5) -| (0.5,-1);
        \draw (3.5,-2) |- (2,-2.5) node[circ]{};
        %
        \draw (4.5,1) [open,v=$U$,#1] to (4.5,-2);
    \end{circuitikz}
}

\begin{document}

\circuit
\quad
\ctikzset{voltage shift = 2}
\circuit
\quad
\circuit[voltage shift=0] % explicitly put to zero voltage shift for open

\end{document}

enter image description here

Anyway, my suggestion is to stick with the normal size and proportion, and if you want a smaller circuit, just scale it wholesome. I prepared a small example here to show the effect...

\documentclass{report}
\usepackage[a3paper]{geometry}
\usepackage{graphicx} % Required for inserting images

\usepackage[european,straightvoltages]{circuitikz}
\ctikzset{bipoles/thickness=1}
\ctikzset{tripoles/thickness=1}
\tikzset{every picture/.style = {line width=1pt}}

\newcommand{\circuit}{%
    \begin{tikzpicture}
        % Switch
        \draw (2,0) node[spdt, rotate=90,anchor=in] (sw) {};
        \draw (sw.out 2)  node[right] {(2)};
        \draw (sw.out 1)  node[left] {(1)};
        %Komponenen
        \draw (0.5,0)  to [V_=$U$] (0.5,-1);
        \draw (2,0) to [R,v=$U_R$] (2,-1) to [L,v=$U_L$] (2,-2);
        \draw (3.5,0) to [C,v=$U_C$] (3.5,-2);
        %Verkabelung
        \draw (0.5,0) to (0.5,1.5) -| (sw.out 1);
        \draw (3.5,0) to (3.5,1.5) -| (sw.out 2);
        \draw (2,-2) to (2,-2.5) -| (0.5,-1);
        \draw (3.5,-2) |- (2,-2.5) node[circ]{};
        %
        \draw (4.5,1) [open,v=$U$] to (4.5,-2);
    \end{tikzpicture}
}
\newcommand{\circuitlarge}[1][]{%
    \begin{tikzpicture}[#1]
        % Switch
        \draw (4,0) node[spdt, rotate=90,anchor=in] (sw) {};
        \draw (sw.out 2)  node[right] {(2)};
        \draw (sw.out 1)  node[left] {(1)};
        %Komponenen
        \draw (1,0)  to [V_=$U$] (1,-2);
        \draw (4,0) to [R,v=$U_R$] (4,-2) to [L,v=$U_L$] (4,-4);
        \draw (7,0) to [C,v=$U_C$] (7,-4);
        %Verkabelung
        \draw (1,0) to (1,3) -| (sw.out 1);
        \draw (7,0) to (7,3) -| (sw.out 2);
        \draw (4,-4) to (4,-5) -| (1,-2);
        \draw (7,-4) |- (4,-5) node[circ]{};
        %
        \draw (9,2) [open,v=$U$] to (9,-4);
    \end{tikzpicture}
}


\begin{document}
{%
    \ctikzset{resistors/scale=0.55}%
    \ctikzset{inductors/scale=0.55}%
    \ctikzset{capacitors/scale=0.6}%
    \ctikzset{sources/scale=.8}%
    \circuit
}
\quad
{
    \ctikzset{sources/scale=1.2}%
    \ctikzset{switches/scale=1.8}%
    \LARGE
    \circuitlarge[scale=0.55, transform shape]
    \ctikzset{voltage shift=1.5}
    \circuitlarge[scale=0.55, transform shape]
    
}

\end{document}

enter image description here

I opened an issue on the GitHub project and try to tackle it in the future. Really, the voltage thing should be completely rewritten (first of all, to use edges for the arrows so that they can be styled independently from the wires, using more reliable anchors, and so on).


(*) yes, it's mine. Old nevertheless.