10

I'm using Latex and CircuiTikz to draw circuits but I can't rotate de voltmeter without making a disaster. Any idea? Here is an example.

\begin{circuitikz}
\draw
(0,0) to[ammeter] (3,0)
      to (4,0)
      to[lamp] (4,-2)
      to (0,-2)
      to[sV] (0,0);
\draw (3,0) to[voltmeter] (3,-2);
\end{circuitikz}

enter image description here

  • How do I "compile" code to show the example circuit? – Agustin Mar 29 '13 at 13:48
  • Welcome to TeX.SX. As new user without image posting privileges simply include the image as normal and remove the ! in front of it to turn it into a link. A moderator or another user with edit privileges can then reinsert the ! to turn it into an image again. It is better to turn your code snippet into a (MWE)](meta.tex.stackexchange.com/q/228) starting with \documentclass ending with \end{document} and including the necessary packages. This will help who wants to answer you. :) – Claudio Fiandrino Mar 29 '13 at 14:02
  • 1
    This answer might be helpful. If you need the arrow too, uncomment all the lines. – Luigi Mar 29 '13 at 15:32
9

I see that in the last version of circuitikz, the definitions for ammeter and voltmeter have been changed, so if your version is 0.3.0 or newer, use the following code. If you don't need the diagonal arrow, comment the last two \pgfusepath{draw}. However I notice that now the circles don't touch the wires (I wonder why). If you want to correct this behaviour, replace the lines

\pgfpathcircle{\pgfpointorigin}{.9\pgf@circ@res@up}
<...>
\pgfpathlineto{\pgfpoint{-\pgf@circ@res@other}{\pgf@circ@res@up}}

with

\pgfpathcircle{\pgfpointorigin}{\pgf@circ@res@up}
<...>
\pgfpathlineto{\pgfpoint{-1.06\pgf@circ@res@other}{1.06\pgf@circ@res@up}}

but then you have to do the same in the ammeter definition. enter image description here

\documentclass{standalone}
\usepackage{circuitikz}
\makeatletter
\def\pgf@circ@myvoltmeter@path#1{\pgf@circ@bipole@path{myvoltmeter}{#1}}
\tikzset{myvoltmeter/.style = {\circuitikzbasekey, /tikz/to
                               path=\pgf@circ@myvoltmeter@path}}
\pgfcircdeclarebipole{}{\ctikzvalof{bipoles/voltmeter/height}}{myvoltmeter}{\ctikzvalof{bipoles/voltmeter/height}}{\ctikzvalof{bipoles/voltmeter/width}}{
    \def\pgf@circ@temp{right}
    \ifx\tikz@res@label@pos\pgf@circ@temp
        \pgf@circ@res@step=-1.2\pgf@circ@res@up
    \else
        \def\pgf@circ@temp{below}
        \ifx\tikz@res@label@pos\pgf@circ@temp
            \pgf@circ@res@step=-1.2\pgf@circ@res@up
        \else
            \pgf@circ@res@step=1.2\pgf@circ@res@up
        \fi
    \fi

    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@left}{\pgf@circ@res@zero}}       
    \pgfpointorigin \pgf@circ@res@other =  \pgf@x  \advance \pgf@circ@res@other by -\pgf@circ@res@up
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@other}{\pgf@circ@res@zero}}
    \pgfusepath{draw}

    \pgfsetlinewidth{\pgfkeysvalueof{/tikz/circuitikz/bipoles/thickness}\pgfstartlinewidth}

        \pgfscope
            \pgfpathcircle{\pgfpointorigin}{.9\pgf@circ@res@up} % change this if you want to touch the wires
            \pgfusepath{draw}       
        \endpgfscope    

    \pgfsetlinewidth{\pgfstartlinewidth}
    \pgftransformrotate{90}
    \pgfsetarrowsend{latex}
    \pgfpathmoveto{\pgfpoint{\pgf@circ@res@other}{\pgf@circ@res@down}}
    \pgfpathlineto{\pgfpoint{-\pgf@circ@res@other}{\pgf@circ@res@up}} % change this if you want to touch the wires
    \pgfusepath{draw} % comment this if you don't need the diagonal arrow
    \pgfsetarrowsend{}


    \pgfpathmoveto{\pgfpoint{-\pgf@circ@res@other}{\pgf@circ@res@zero}}
    \pgfpathlineto{\pgfpoint{\pgf@circ@res@right}{\pgf@circ@res@zero}}
    \pgfusepath{draw} % comment this if you don't need the diagonal arrow

    \pgfnode{circle}{center}{\textbf{V}}{}{}
}
\makeatother
\begin{document}
\begin{circuitikz}
\draw
(0,0) to[ammeter] (3,0)
      to (4,0)
      to[lamp] (4,-2)
      to (0,-2)
      to[sV] (0,0);
\draw (3,0) to[myvoltmeter] (3,-2);
\end{circuitikz}
\end{document}
  • WOW. Thats a lot of code just to rotate the voltmeter, but thanks it works. – Agustin Mar 30 '13 at 18:42
  • @Agustin it's not completely my own work! I simply defined a new component by copying the code for the original voltmeter from pgfcircbipoles.tex, and adding a row of code to rotate the label (and reusing an old answer of mine). – Luigi Mar 30 '13 at 19:10
9

There is a simpler scheme to achieve the goal. A proposed approach that defines a mymeter command taking 2 arguments \#1=name, \#2=angle of rotation. Basically it redefines the voltmeter by drawing a mymeter that has the rotation ability.

\newcommand{\mymeter}[2] 
{  % #1 = name , #2 = rotation angle
\begin{scope}[transform shape,rotate=#2]
\draw[thick] (#1)node(){$\mathbf V$} circle (11pt);
\draw[rotate=45,-latex] (#1)  +(-17pt,0) --+(17pt,0);
\end{scope}
}

enter image description here

Code

\documentclass[border=1cm,varwidth]{standalone}  
\usepackage{tikz,amsmath}
\usepackage[american,siunitx]{circuitikz}
\usetikzlibrary{arrows}

\newcommand{\mymeter}[2] 
{  % #1 = name , #2 = rotation angle
\begin{scope}[transform shape,rotate=#2]
\draw[thick] (#1)node(){$\mathbf V$} circle (11pt);
\draw[rotate=45,-latex] (#1)  +(-17pt,0) --+(17pt,0);
\end{scope}
}

\begin{document}

OP's circuit:

\medskip

\begin{circuitikz}
\draw
(0,0) to[ammeter] (3,0)
      to (4,0)
      to[lamp] (4,-2)
      to (0,-2)
      to[sV] (0,0);
\draw (3,0) to[voltmeter] (3,-2);
\end{circuitikz}

\medskip

Result of proposed approach:

\medskip

\begin{circuitikz}
\draw
(0,0) to[ammeter] (3,0)
      to (4,0)
      to[lamp] (4,-2)
      to (0,-2)
      to[sV] (0,0);
\draw (3,0) to[voltmeter,color=white,name=M](3,-2);
\mymeter{M}{0}  % try 90, 0, -90
\end{circuitikz}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.