LaTeX forum ⇒ Graphics, Figures & TablesHow to redefine the american voltage signs in CircuiTikz?

Information and discussion about graphics, figures & tables in LaTeX documents.
kgoodrick
Posts: 1
Joined: Wed Jun 13, 2018 8:45 pm

How to redefine the american voltage signs in CircuiTikz?

Postby kgoodrick » Tue Jun 19, 2018 10:11 pm

Hello All,

I posted this question on the Tex stack exchange a week ago, but unfortunately we haven't been able to come up with a solution yet. I was hoping another set of eyes on it might help find a solution.

Thanks for your help!

Question duplicated below:

I am trying to redefine the plus and minus signs in circuitikz voltage labels to have a user defined thickness. So far I have a solution that works, but it has a few quirks.

The main document:
  1. \documentclass{standalone}
  2.  
  3. \usepackage{pgfplots}
  4.  
  5. \usepackage[american]{circuitikz}
  6.  
  7. \usepgfplotslibrary{external}
  8. \tikzexternalize
  9.  
  10. \newcommand{\thickplusvm}{
  11. \begin{tikzpicture}[x=0.85ex, y=0.85ex, scale=0.5]
  12. \draw[line cap=round, line width=.95pt] (0,-1) -- (0,1);
  13. \draw[line cap=round, line width=.95pt] (1,0) -- (-1,0);
  14. \end{tikzpicture}
  15. }
  16.  
  17. \newcommand{\thickminusvm}{
  18. \begin{tikzpicture}[x=0.85ex, y=0.85ex, scale=0.5]
  19. \draw[line cap=round, line width=.95pt, opacity=0] (0,-1) -- (0,1);
  20. \draw[line cap=round, line width=.95pt] (1,0) -- (-1,0);
  21. \end{tikzpicture}
  22. }
  23.  
  24. \input{./voltagemarkers.tex}
  25.  
  26. \begin{document}
  27.  
  28. \tikzset{external/remake next}
  29. \tikzsetnextfilename{MWE}
  30. \begin{tikzpicture}
  31. \draw (0,0) to[L,v=$v$] ++(2,0);
  32. \draw (3,0) to[R, v^=$v$] ++(0,-2);
  33. \draw[very thin, color=red]
  34. (0,-0.2) -- ++(2,0)
  35. (3,0) -| ++(0.2015,-0.263) ++(-.25,0) -- ++(.5,0)
  36. (3,-2) -| ++(0.265,0.263) ++(-.25,0) -- ++(.5,0)
  37. (0.265,0.25) -- ++(0,-1)
  38. (1.8,0.25) -- ++(0,-1)
  39. ;
  40. \end{tikzpicture}
  41. \end{document}


and voltagemarkers.tex:

  1. \def\pgf@circ@drawvoltagegeneric{
  2. \pgfextra{
  3. \edef\pgf@temp{/tikz/circuitikz/bipoles/\pgfkeysvalueof{/tikz/circuitikz/bipole/kind}/voltage/straight label distance}
  4. \pgfkeysifdefined{\pgf@temp}
  5. {
  6. \edef\partheight{\ctikzvalof{bipoles/\pgfkeysvalueof{/tikz/circuitikz/bipole/kind}/voltage/straight label distance}}
  7. \edef\tmpdistfromline{(\partheight\pgf@circ@Rlen)}
  8. }
  9. {
  10. \pgfkeysifdefined{/tikz/circuitikz/bipoles/voltage/straight label distance}
  11. {
  12. \edef\partheight{\ctikzvalof{bipoles/voltage/straight label distance}}
  13. \edef\tmpdistfromline{(\partheight\pgf@circ@Rlen)}
  14. }
  15. {%calculate default value from part height
  16. \edef\pgf@temp{/tikz/circuitikz/bipoles/\pgfkeysvalueof{/tikz/circuitikz/bipole/kind}/height}
  17. \pgfkeysifdefined{\pgf@temp}
  18. {
  19. \edef\partheight{0.5*\ctikzvalof{bipoles/\pgfkeysvalueof{/tikz/circuitikz/bipole/kind}/height}}
  20. \edef\tmpdistfromline{(\partheight\pgf@circ@Rlen+0.2\pgf@circ@Rlen)}
  21. }
  22. {
  23. \edef\tmpdistfromline{(.5\pgf@circ@Rlen)} %fallback to fixed value
  24. }
  25. }
  26. }
  27. \ifnum \ctikzvalof{mirror value}=-1
  28. \ifpgf@circuit@bipole@inverted
  29. \ifpgf@circuit@bipole@voltage@straight
  30. \def\distfromline{\tmpdistfromline}
  31. \else
  32. \def\distfromline{\ctikzvalof{voltage/distance from line}\pgf@circ@Rlen}
  33. \fi
  34. \else
  35. \ifpgf@circuit@bipole@voltage@straight
  36. \def\distfromline{-\tmpdistfromline}
  37. \else
  38. \def\distfromline{-\ctikzvalof{voltage/distance from line}\pgf@circ@Rlen}
  39. \fi
  40. \fi
  41. \else
  42. \ifpgf@circuit@bipole@inverted
  43. \ifpgf@circuit@bipole@voltage@straight
  44. \def\distfromline{-\tmpdistfromline}
  45. \else
  46. \def\distfromline{-\ctikzvalof{voltage/distance from line}\pgf@circ@Rlen}
  47. \fi
  48. \else
  49. \ifpgf@circuit@bipole@voltage@straight
  50. \def\distfromline{\tmpdistfromline}
  51. \else
  52. \def\distfromline{\ctikzvalof{voltage/distance from line}\pgf@circ@Rlen}
  53. \fi
  54. \fi
  55. \fi
  56. \ifpgf@circuit@bipole@voltage@below
  57. \def\pgf@circ@voltage@angle{90}
  58. \else
  59. \def\pgf@circ@voltage@angle{-90}
  60. \fi
  61. \edef\pgf@temp{/tikz/circuitikz/bipoles/\pgfkeysvalueof{/tikz/circuitikz/bipole/kind}/voltage/distance from node}
  62. \pgfkeysifdefined{\pgf@temp}
  63. { \edef\distacefromnode{\ctikzvalof{bipoles/\pgfkeysvalueof{/tikz/circuitikz/bipole/kind}/voltage/distance from node}} }
  64. { \edef\distacefromnode{\ctikzvalof{voltage/distance from node}} }
  65. \edef\pgf@temp{/tikz/circuitikz/bipoles/\pgfkeysvalueof{/tikz/circuitikz/bipole/kind}/voltage/bump b}
  66. \pgfkeysifdefined{\pgf@temp}
  67. { \edef\bumpb{\ctikzvalof{bipoles/\pgfkeysvalueof{/tikz/circuitikz/bipole/kind}/voltage/bump b}} }
  68. { \edef\bumpb{\ctikzvalof{voltage/bump b}} }
  69. }
  70. % %\pgf@circ@Rlen/16 is equal to the length of the currarrow
  71. coordinate (pgfcirc@midtmp) at ($(\tikztostart) ! \pgf@circ@Rlen/16 ! (anchorstartnode)$) %absolute move, minimum space is length of arrowhead
  72. coordinate (pgfcirc@midtmp) at ($(pgfcirc@midtmp) ! \distacefromnode ! (anchorstartnode)$)
  73.  
  74. coordinate (pgfcirc@Vfrom) at ($(pgfcirc@midtmp) ! -\distfromline ! \pgf@circ@voltage@angle:(anchorstartnode)$)
  75. coordinate (pgfcirc@midtmp) at ($(\tikztotarget) ! \pgf@circ@Rlen/16 ! (anchorendnode)$)%absolute move, minimum space is length of arrowhead
  76. coordinate (pgfcirc@midtmp) at ($(pgfcirc@midtmp) ! \distacefromnode ! (anchorendnode)$)
  77.  
  78. coordinate (pgfcirc@Vto) at ($(pgfcirc@midtmp) ! \distfromline ! \pgf@circ@voltage@angle : (anchorendnode)$)
  79.  
  80. \ifpgf@circuit@bipole@voltage@below
  81. coordinate (pgfcirc@Vcont1) at ($(\ctikzvalof{bipole/name}.center) ! \bumpb ! (\ctikzvalof{bipole/name}.-110)$)
  82. coordinate (pgfcirc@Vcont2) at ($(\ctikzvalof{bipole/name}.center) ! \bumpb ! (\ctikzvalof{bipole/name}.-70)$)
  83. \else
  84. coordinate (pgfcirc@Vcont1) at ($(\ctikzvalof{bipole/name}.center) ! \bumpb ! (\ctikzvalof{bipole/name}.110)$)
  85. coordinate (pgfcirc@Vcont2) at ($(\ctikzvalof{bipole/name}.center) ! \bumpb ! (\ctikzvalof{bipole/name}.70)$)
  86. \fi
  87.  
  88. \ifpgf@circuit@europeanvoltage
  89. \ifpgf@circuit@bipole@voltage@straight
  90. \ifpgf@circuit@bipole@voltage@backward
  91. (pgfcirc@Vto) --(pgfcirc@Vfrom) node[currarrow, sloped, allow upside down, pos=1,anchor=tip] {}
  92. \else
  93. (pgfcirc@Vfrom) --(pgfcirc@Vto) node[currarrow, sloped, allow upside down, pos=1,anchor=tip] {}
  94. \fi
  95. \else
  96. \ifpgf@circuit@bipole@voltage@backward
  97. (pgfcirc@Vto) .. controls (pgfcirc@Vcont2) and (pgfcirc@Vcont1) ..
  98. node[currarrow, sloped, allow upside down, pos=1] {}
  99. (pgfcirc@Vfrom)
  100. \else
  101. (pgfcirc@Vfrom) .. controls (pgfcirc@Vcont1) and (pgfcirc@Vcont2) ..
  102. node[currarrow, sloped, allow upside down, pos=1] {}
  103. (pgfcirc@Vto)
  104. \fi
  105. \fi
  106. \else
  107. \ifpgf@circuit@bipole@voltage@backward
  108. \ifpgf@circ@oldvoltagedirection
  109. (pgfcirc@Vfrom) node[inner sep=0, anchor=\pgf@circ@bipole@voltage@label@anchor]{\thickplusvm}
  110. (pgfcirc@Vto) node[inner sep=0, anchor=\pgf@circ@bipole@voltage@label@anchor]{\thickminusvm}
  111. \else
  112. (pgfcirc@Vfrom) node[inner sep=0, anchor=\pgf@circ@bipole@voltage@label@anchor]{\thickminusvm}
  113. (pgfcirc@Vto) node[inner sep=0, anchor=\pgf@circ@bipole@voltage@label@anchor]{\thickplusvm}
  114. \fi
  115. \else
  116. \ifpgf@circ@oldvoltagedirection
  117. (pgfcirc@Vfrom) node[inner sep=0, anchor=\pgf@circ@bipole@voltage@label@anchor]{\thickminusvm}
  118. (pgfcirc@Vto) node[inner sep=0, anchor=\pgf@circ@bipole@voltage@label@anchor]{\thickplusvm}
  119. \else
  120. (pgfcirc@Vfrom) node[inner sep=0, anchor=\pgf@circ@bipole@voltage@label@anchor]{\thickplusvm}
  121. (pgfcirc@Vto) node[inner sep=0, anchor=\pgf@circ@bipole@voltage@label@anchor]{\thickminusvm}
  122. \fi
  123. \fi
  124. \fi
  125. }


voltagemarkers.tex is the same as the definition in the source code with $+$ and $-$ replaced by my macros from the main file.

There are two problems that I have with this solution. The first is that it requires externalization, and specifically it requires [cmd]usepgfplotslibrary{external}[/cmd] Either not externalizing or using [cmd]usetikzlibrary{external}[/cmd] instead both result in the new definitions being ignored. This is not necessarily a problem, but I would at least like to understand why this specific externalization is required.

The second problem is that I need to create an invisible vertical line in the minus symbol to keep the plus and minus inline. I tried using the text height and text depth tikz options described in the manual to keep them the same size, but this resulted in the minus sign being higher than the plus sign in the horizontal components. I also tried using the baseline option, but with it the vertical markers became misaligned.

Is there a better way to do this that does not require the phantom line in the minus sign or externalization? I know this is delving into the depths of circuitikz, but I would certainly appreciate any help you would be able to provide.


Tags:

Return to “Graphics, Figures & Tables”

Who is online

Users browsing this forum: No registered users and 12 guests