6

I'm attempting to draw a battery (using the battery1 component) that includes positive and negative signs at the appropriate ends. A previous question that was answered here (using version 2.4) shows exactly what I mean:

1

Only, of course, I want to be using a battery1 symbol instead. So, I have my code as follows:

\usepackage[americanvoltages,fulldiodes,siunitx]{circuitikz}
\begin{document}
\begin{circuitikz}
    \draw (0,0) to [battery1=\SI{9}{V}] (3,0)
                to [pC,v=$v_C$] (6,0);
\end{circuitikz}
\end{document}

And I end up with this:

enter image description here

Notice the lack of +/- signs (I know how circuits work so I don't need to be actually shown these signs, but for the sake of copying my class notes diagrams I want these signs to be included in my latex diagram).

In either case, I think it may have something to do with the version of Circuitikz I'm using because I'm using the latest version (I believe, 0.3.0) but in the previous question linked above, they were using 2.4 and it worked out for them.

I would really like to find a way to add +/- without having to manually add it with Tikz or something, i.e. just have it naturally part of the component without having to add nodes or extra Tikz code etc. If that's not possible, then I'll have to settle for that as a last result.

Thank you for your help!

EDIT: I should also mention that I've tried using battery1, v=$9V$ instead of battery1=\SI{9}{V} and to no avail (the +/- signs still don't show up).

0

4 Answers 4

5

A solution meeting the stated requirements: the + and - signs are placed natively without extra packages nor manually adding them. This implies redefining \pgf@circ@drawvoltagegenerator as defined in pgfcircvoltage.sty (the only thing that has to be done is to comment out a conditional that prevents the signs from appearing):

\documentclass{article}
\usepackage[americanvoltages,fulldiodes,siunitx]{circuitikz}
\usepackage{ifthen}

\makeatletter
  \def\pgf@circ@drawvoltagegenerator{
    \ifpgf@circuit@bipole@voltage@below
        coordinate (pgfcirc@Vcont1) at ($(\ctikzvalof{bipole/name}.center) ! \ctikzvalof{voltage/bump a} ! (\ctikzvalof{bipole/name}.-120)$)
        coordinate (pgfcirc@Vcont2) at ($(\ctikzvalof{bipole/name}.center) ! \ctikzvalof{voltage/bump a} ! (\ctikzvalof{bipole/name}.-60)$)
    \else
        coordinate (pgfcirc@Vcont1) at ($ (\ctikzvalof{bipole/name}.center) ! \ctikzvalof{voltage/bump a} ! (\ctikzvalof{bipole/name}.120)$)
        coordinate (pgfcirc@Vcont2) at ($ (\ctikzvalof{bipole/name}.center) ! \ctikzvalof{voltage/bump a} ! (\ctikzvalof{bipole/name}.60)$)
    \fi

    \ifpgf@circuit@europeanvoltage
        \ifpgf@circuit@bipole@voltage@backward
            (pgfcirc@Vcont2)  -- node[currarrow, sloped,  allow upside down, pos=1] {} (pgfcirc@Vcont1)
        \else
            (pgfcirc@Vcont1)  -- node[currarrow, sloped,  allow upside down, pos=1] {} (pgfcirc@Vcont2)
        \fi

    \else % american voltage

        \pgfextra{
            \edef\pgf@temp{\pgfkeysvalueof{/tikz/circuitikz/bipole/kind}}
            \def\pgf@circ@temp{battery}
            \ifx\pgf@temp\pgf@circ@temp
                \def\pgf@circ@batteria{battery}
            \else
                \edef\pgf@circ@temp{battery1}
                \ifx\pgf@temp\pgf@circ@temp
                    \edef\pgf@circ@batteria{battery}
                \else
                    \edef\pgf@circ@batteria{false}
                \fi
            \fi
            \def\pgf@circ@temp{battery1}
        }

        \ifx\pgf@temp\pgf@circ@temp % if it is a battery, must put + and -
            \ifpgf@circuit@bipole@voltage@backward
                (pgfcirc@Vcont2)  node[xshift=2pt,yshift=-6pt] {$-$}  (pgfcirc@Vcont1) node[xshift=-2pt,yshift=-6pt] {$+$}
            \else
                (pgfcirc@Vcont1)  node[xshift=-2pt,yshift=-6pt] {$-$}  (pgfcirc@Vcont2) node[xshift=2pt,yshift=-6pt] {$+$}
            \fi
        \fi

    \fi
}
\makeatother
\begin{document}
\begin{circuitikz}
    \draw (0,0) to [battery1,v>=\SI{9}{V}] (3,0)
                to [pC,v=$v_C$] (6,0) 
                to[cV, v=\SI{9}{V}] (9,0)
                ;
\end{circuitikz}

\end{document}

enter image description here

A remark:

I think that the missing + and - signs might be a bug in circuitikz. If one looks at the relevant code I modified:

\else % american voltage
        
        \pgfextra{
            \def\pgf@temp{\pgfkeysvalueof{/tikz/circuitikz/bipole/kind}}
            \def\pgf@circ@temp{battery}
            \ifx\pgf@temp\pgf@circ@temp
                \edef\pgf@circ@batteria{battery}
            \else
                \def\pgf@circ@temp{battery1}
                \ifx\pgf@temp\pgf@circ@temp
                    \edef\pgf@circ@batteria{battery}
                \else
                    \edef\pgf@circ@batteria{false}
                \fi
            \fi
            \edef\pgf@circ@temp{battery}
        }
        
        \ifx\pgf@circ@batteria\pgf@circ@temp % if it is a battery, must put + and -
            \ifpgf@circuit@bipole@voltage@backward
                (pgfcirc@Vcont2)  node {$-$}  (pgfcirc@Vcont1) node {$+$}
            \else
                (pgfcirc@Vcont1)  node {$-$}  (pgfcirc@Vcont2) node {$+$}
            \fi
        \fi
        
    \fi

The line

\ifx\pgf@circ@batteria\pgf@circ@temp % if it is a battery, must put + and -

suggests that the package author intended to include the + and - signs for batteries; however, the conditional above the mentioned line won't produce the expected condition since an \def was used instead of \edef here:

\def\pgf@temp{\pgfkeysvalueof{/tikz/circuitikz/bipole/kind}}

and the last conditional should be

\ifx\pgf@temppgf@circ@temp

instead of

\ifx\pgf@circ@batteria\pgf@circ@temp 

Perhaps it would be a good idea to contact the package author and ask him if this is indeed a bug or if he decided not to add the signs in all cases.

4
  • Hi, thank you very much for providing a more permanent solution; however, when I have (for example) to[cV, v=\SI{9}{V}] as a component, the +/- signs appear on top of this component even though this component (long form: american controlled voltage source) already has +/- signs embedded within it. How can I fix this particular one? Also if I want to bring the +/- signs down a bit just for the battery1 what should I adjust? Thank you!
    – Alborz
    Commented Mar 12, 2014 at 23:02
  • 1
    @Alborz fixed. In fact, I think the lack of the + and - signs for battery1 is a bug in circuitikz; I'll add some remark about this to my answer in some minutes. Commented Mar 13, 2014 at 0:18
  • Perfect, thanks again! Really appreciate it :) And yes, I think it's a bug because his changelog files/update summaries never mention a need to remove these signs from batteries; I'll probably contact him/her tonight to see what's going on.
    – Alborz
    Commented Mar 13, 2014 at 0:50
  • @GonzaloMedina, some news about a possible fix in the plus/minus bug? I didn't see in the package documentation.
    – Sigur
    Commented Sep 18, 2017 at 23:27
6

Just an update... (after 7 years)

\documentclass{article}
\usepackage[americanvoltages,fulldiodes,siunitx]{circuitikz}
\begin{document}
\begin{circuitikz}
\draw (0,0) to [battery1=$\,$] (3,0);
\end{circuitikz}
\end{document}

does the job.

I just typ \backslash , to have the the signs

enter image description here

1
  • 1
    Note, to get the +/- signs, you need the americanvoltages option passed to circuitikz.
    – Paul Wintz
    Commented Jan 28 at 9:31
2

Here are two possibilities. The first using subscripts, the second using stacks.

\documentclass{article}
\usepackage[americanvoltages,fulldiodes,siunitx]{circuitikz}
\usepackage{stackengine}
\parskip 1em
\begin{document}
\begin{circuitikz}
    \draw (0,0) to [battery1=$_-$\SI{9}{V}$_+$] (3,0)
                to [pC,v=$v_C$] (6,0);
\end{circuitikz}

\begin{circuitikz}
    \draw (0,0) to [battery1=\smash{\stackunder[6pt]{\SI{9}{V}}{$_-$~~~$_+$}}] (3,0)
                to [pC,v=$v_C$] (6,0);
\end{circuitikz}
\end{document}

enter image description here

1
  • Thank you; unfortunately I'm not familiar with stacks and I wouldn't know how to use it properly for a vertical battery, for example. I looked at stackengine's documentation (briefly, I'll admit) and it seemed a bit much for me to determine which one exactly was right to integrate with circuitikz :(
    – Alborz
    Commented Mar 12, 2014 at 22:14
1

Another possible solution with TiKz draw command:

\documentclass{article}

\usepackage[americanvoltages,fulldiodes,siunitx]{circuitikz}

\begin{document}
\begin{circuitikz}

  \draw (0,0) to [battery1=\SI{9}{V}] (3,0)
                to [pC,v=$v_C$] (6,0);

              \draw (1,0.3) node{$-$};
              \draw (2,0.3) node{$+$};

\end{circuitikz}
\end{document}

enter image description here

1
  • Hi, this would be perfect if only the positions of the +/- signs relative to the battery remained the same when I scale up and down; instead, they remain at their exact same coordinates. Is there any way for this to work?
    – Alborz
    Commented Mar 12, 2014 at 22:11

You must log in to answer this question.

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