# TikZ: Controlling new circuit symbols questions

I have created a new circuit symbol for battery source (var battery). There are two problems and one question:

1. I would like for minus to be always horizontal.
2. I would like to control how plus and minus are oriented (e.g. plus up or plus down).
3. Is it possible to change graphical representation of + and - with text symbols?

Regards, Marko

\documentclass[12pt,a4paper]{article}
\input{glava}
\usepackage{tikz}

\begin{document}

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

\tikzset{circuit declare symbol = var battery}
\tikzset{set var battery graphic = var battery IEC graphic}
\tikzset{var battery IEC graphic/.style=
{transform shape, circuit symbol lines, circuit symbol size = width
2.5 height 2.5,
shape=generic circle IEC, /pgf/generic circle IEC/before
background=
{\pgfpathcircle{\pgfpoint{0.45pt}{0pt}}{0.1pt}
\pgfpathcircle{\pgfpoint{-0.45pt}{0pt}}{0.1pt}
\pgfpathmoveto{\pgfpoint{-1.0pt}{0pt}}
\pgfpathlineto{\pgfpoint{-0.6pt}{0pt}}
\pgfpathmoveto{\pgfpoint{1.0pt}{0pt}}
\pgfpathlineto{\pgfpoint{0.6pt}{0pt}}
% PLUS SYMBOL
\pgfpathmoveto{\pgfpoint{-0.45pt}{0.25pt}}
\pgfpathlineto{\pgfpoint{-0.45pt}{0.65pt}}
\pgfpathmoveto{\pgfpoint{-0.25pt}{0.45pt}}
\pgfpathlineto{\pgfpoint{-0.65pt}{0.45pt}}
% MINUS SYMBOL
\pgfpathmoveto{\pgfpoint{0.25pt}{0.45pt}}
\pgfpathlineto{\pgfpoint{0.65pt}{0.45pt}}
\pgfusepathqstroke}}}

\begin{tikzpicture}[circuit ee IEC,semithick,x=1cm,y=1cm]
\draw (0,0) to [capacitor] (3,0)
to [resistor] (3,2)
to [var battery] (0,2)
to [var battery={info'={$U_3$}}] (0,0);
\end{tikzpicture}

\end{document}

-
@Marko: Welcome to tex.sx! A tip: If you indent lines by 4 spaces, then they are marked as a code sample. You can also highlight the code and click the "code" button (with "101010" on it). – lockstep Feb 14 '11 at 21:40
@lockstep, a tip: It's better to wait a few minutes with such edits :-) (5 minutes grace period!) – Hendrik Vogt Feb 14 '11 at 21:44
@Hendrik: You're right, and I will restrain myself from now on. (It was a really big would-be code sample, though. :-)) – lockstep Feb 14 '11 at 21:48
@lockstep: I do appreciate your edits, so please refrain from restraining for more than 5 minutes :-) – Hendrik Vogt Feb 14 '11 at 21:53
@lockstep: circuitikz is a separate package, so the tag technically doesn't apply here. Maybe we should create a general tag for typesetting circuits? – Caramdir Feb 15 '11 at 1:28

Here's one solution. It uses \pgftransformresetnontranslation to make sure the minus is typeset the right way round, and then sets a new scaling factor as a multiple of \tikzcircuitssizeunit, which makes sure the symbol scales according to the <size> circuit symbols style.

\documentclass[12pt,a4paper]{article}
%\input{glava}
\usepackage{tikz}

\begin{document}

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

\newif\ifreversepolarity
\tikzoption{reversepolarity}[true]{\reversepolaritytrue}

\tikzset{circuit declare symbol = var battery}
\tikzset{set var battery graphic = var battery IEC graphic}
\tikzset{var battery IEC graphic/.style=
{transform shape, circuit symbol lines, circuit symbol size = width
2.5 height 2.5,
shape=generic circle IEC, /pgf/generic circle IEC/before
background=
{
\pgfpathcircle{\pgfpoint{0.45pt}{0pt}}{0.1pt}
\pgfpathcircle{\pgfpoint{-0.45pt}{0pt}}{0.1pt}
\pgfpathmoveto{\pgfpoint{-1.0pt}{0pt}}
\pgfpathlineto{\pgfpoint{-0.6pt}{0pt}}
\pgfpathmoveto{\pgfpoint{1.0pt}{0pt}}
\pgfpathlineto{\pgfpoint{0.6pt}{0pt}}
\pgfusepathqstroke
% PLUS AND MINUS SYMBOL
\pgfgettransform\savedtransform
\pgftransformshift{\pgfpoint{0.45pt}{0.45pt}}
\pgftransformresetnontranslations
\pgftransformscale{0.075\tikzcircuitssizeunit}
\pgftext{\bf{\ifreversepolarity$-$\else$+$\fi}}
\pgfsettransform\savedtransform
\pgftransformshift{\pgfpoint{-0.45pt}{0.45pt}}
\pgftransformresetnontranslations
\pgftransformscale{0.075\tikzcircuitssizeunit}
\pgftext{\bf{\ifreversepolarity$+$\else$-$\fi}}
\pgfsettransform\savedtransform
}}}

\begin{tikzpicture}[circuit ee IEC,semithick,x=1cm,y=1cm]
\draw (0,0) to [capacitor] (3,0)
to [resistor] (3,2)
to [var battery,reversepolarity,tiny circuit symbols] (0,2)
to [var battery={info'={$U_3$}},huge circuit symbols] (0,0);
\end{tikzpicture}

\end{document}


-

Thank you very much! We are coming closer to the final solution!

One possible solution would be to return back to drawing plus and minus symbols. This is rather straightforward. However, I have noticed that I need to include \pgftransformscale{8}. This is worrying, as I do not know if this has any specific consequences on appearance and symbol scaling...

\documentclass[12pt,a4paper]{article}
\usepackage{tikz}

\begin{document}
\newif\ifreversepolarity
\tikzoption{reversepolarity}[true]{\reversepolaritytrue}

\tikzset{circuit declare symbol = var battery}
\tikzset{set var battery graphic = var battery IEC graphic}
\tikzset{var battery IEC graphic/.style=
{transform shape, circuit symbol lines, circuit symbol size = width
2.5 height 2.5,
shape=generic circle IEC, /pgf/generic circle IEC/before
background=
{
\pgfpathcircle{\pgfpoint{0.45pt}{0pt}}{0.1pt}
\pgfpathcircle{\pgfpoint{-0.45pt}{0pt}}{0.1pt}
\pgfpathmoveto{\pgfpoint{-1.0pt}{0pt}}
\pgfpathlineto{\pgfpoint{-0.55pt}{0pt}}
\pgfpathmoveto{\pgfpoint{1.0pt}{0pt}}
\pgfpathlineto{\pgfpoint{0.55pt}{0pt}}
\pgfusepathqstroke
% PLUS AND MINUS SYMBOL
\pgfgettransform\savedtransform
\pgftransformshift{\pgfpoint{0.45pt}{0.45pt}}
\pgftransformresetnontranslations
\pgftransformscale{8}
\ifreversepolarity\pgfpathmoveto{\pgfpoint{0pt}{-0.2pt}}
\pgfpathlineto{\pgfpoint{0pt}{0.2pt}}\fi
\pgfpathmoveto{\pgfpoint{-0.2pt}{0pt}}
\pgfpathlineto{\pgfpoint{0.2pt}{0pt}}
\pgfusepathqstroke
%     \pgftransformscale{0.5}
%     \pgftext{\bf{\ifreversepolarity$-$\else$+$\fi}}
\pgfsettransform\savedtransform
\pgftransformshift{\pgfpoint{-0.45pt}{0.45pt}}
\pgftransformresetnontranslations
\pgftransformscale{8}
\ifreversepolarity\else\pgfpathmoveto{\pgfpoint{0pt}{-0.2pt}}
\pgfpathlineto{\pgfpoint{0pt}{0.2pt}}\fi
\pgfpathmoveto{\pgfpoint{-0.2pt}{0pt}}
\pgfpathlineto{\pgfpoint{0.2pt}{0pt}}
\pgfusepathqstroke
%     \pgftransformscale{0.5}
%     \pgftext{\bf{\ifreversepolarity$+$\else$-$\fi}}
\pgfsettransform\savedtransform
}}}

\begin{tikzpicture}[circuit ee IEC,semithick,x=1cm,y=1cm]
\draw (0,0) to [capacitor] (3,0)
to [resistor] (3,2)
to [var battery={info={$U_1$}},reversepolarity] (0,2)
to [var battery={info'={$U_3$}}] (0,0)
to [var battery={info'={$U_2$}}] (-2,-2);
\end{tikzpicture}

\end{document}

-
 The problem with the scaling isn't solved by that approach, because the scaling factor is still reset using \pgftransformresetnontranslations, and then manually set to a fixed value using \pgftransformscale. I've looked into it again, the way to handle this is to use the macro \tikzcircuitssizeunit in the scaling factor, which reflects the symbol size as set by the style tiny circuit symbols, small circuit symbols, etc. I've updated my answer accordingly. A similar approach will also work with the "hand-drawn" plus and minus symbols. – Jake Feb 16 '11 at 8:49 @Jake Now I got what you mean. Yes, you can get matrix components from \pgfgettransformentries{\ta}{\tb}{\tc}{\td}{\te}{\tf}. I did some calculations and scaling in x dimension is sqrt(a^2+c^2) and scaling in y dimension is sqrt(b^2+d^2). But I do not have any idea how to do those calculations in TikZ. – Pygmalion Feb 16 '11 at 10:01 You can do those calculations using \pgfmathsetmacro\scalex{veclen(\ta,\tc)} and \pgfmathsetmacro\scaley{veclen(\tb,\td)}, which calculates the values using the pgf math parser and stores them into a macro of your choice (like \scalex). There's no need to do this here, however, because we already have the reference length \tikzcircuitssizeunit that we can use to determine the correct scaling factor. \tikzcircuitssizeunit changes when the symbol size is changed using, say, tiny circuit symbols in the tikzpicture. – Jake Feb 16 '11 at 10:13 O thanks a lot! I've written my comment before seeing yours. So this \tikzcircuitssizeunit is fullproof solution? Thanks again. – Pygmalion Feb 16 '11 at 10:37 @Jake I have tested it on another example and in fact \tikzcircuitssizeunit method makes scaling about 10% smaller than original. – Pygmalion Feb 16 '11 at 10:46

For anybody interested in that topic. I have used proposed solution for another of my problems: ac source symbol. Namely in the case of ac source symbol, SINE should not rotate together with the symbol. First suggestion was to drop "transform shape", which gave stupid results when label was written by calling "info". Instead I have used Jake's proposition with \tikzcircuitssizeunit, but I had to multiply it with 1.25 to retain original scaling (this is somehow strange). Anyway, I think that ac symbol could be included into circuit/tikz library, since it is a very common circuit symbol.

\documentclass[12pt,a4paper]{article}
\usepackage{tikz}

\begin{document}

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

\tikzset{circuit declare symbol = ac source}
\tikzset{set ac source graphic = ac source IEC graphic}
\tikzset{ac source IEC graphic/.style={%
transform shape,
circuit symbol lines,
circuit symbol size = width 2.5 height 2.5,
shape = generic circle IEC,
/pgf/generic circle IEC/before background={%
\pgfgettransform\savedtransform
\pgftransformresetnontranslations
\pgftransformscale{1.25*\tikzcircuitssizeunit}
\pgfpathmoveto{\pgfpoint{-0.8pt}{0pt}}
\pgfpathsine{\pgfpoint{0.4pt}{0.4pt}}
\pgfpathcosine{\pgfpoint{0.4pt}{-0.4pt}}
\pgfpathsine{\pgfpoint{0.4pt}{-0.4pt}}
\pgfpathcosine{\pgfpoint{0.4pt}{0.4pt}}
\pgfusepathqstroke
\pgfsettransform\savedtransform}}}

\begin{tikzpicture}[circuit ee IEC]
\draw (0,0) to [ac source={info={$U_1$}}] (-3,0) to [ac source={info={$U_2$}}] (-3,2);
\end{tikzpicture}

\end{document}

-
 It's not really "strange" that you need to use \tikzcircuitssizeunit with a factor: \tikzcircuitssizeunit is not the scale factor, but merely a length that can be used as a unit length to define other lengths by. Take a look a the symbols defined in tikzlibrarycircuits.ee.code.tex: \tikzcircuitssizeunit is almost exclusively used with a factor. Note also that you don't need the * between the factor and \tikzcircuitssizeunit (the predefined symbols do not use it, either). – Jake Feb 16 '11 at 11:15 I wanted to include a picture but it does not accept EPS and I do not know how to produce anything else :-) – Pygmalion Feb 16 '11 at 11:17 @Marko: this seems to be an answer to a question you've not asked. The {TeX} model is to have each question in a separate post. You would be best to post a question on this topic, then answer it yourself. As some stage, one of the moderators will probably remove this post from public view – Joseph Wright♦ Feb 17 '11 at 21:26