# Including +/- signs to battery in Circuitikz

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:

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:

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.

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).

-

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}


## 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


\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.

-
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 Mar 12 at 23:02
@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. –  Gonzalo Medina Mar 13 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 Mar 13 at 0:50

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}


-
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 Mar 12 at 22:14

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}


-
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 Mar 12 at 22:11