在法国,我们在偶极子上方或下方画一个箭头来表示其两极之间的电位差。我试图使用tikz
电路库(不是circuitz
)的思想来自动化这一过程。代码改编自代码direction info
。
我有以下代码。它可以工作,但还不够完美。
- 它允许颠倒标签。
- 有时标签会与箭头重叠。
- 我感觉它只是部分符合
tikz
处理样式和选项的方式。 - 它很冗长(未重构)。
我应该做什么以及如何改善这种情况?
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{circuits.ee.IEC}
\tikzset{%
voltage info/.is family,
voltage info/vertical offset/.initial = .5,
voltage info/arrow direction/.initial = ->,
voltage info/horizontal offset/.initial = 2,
voltage info/label position/.initial = above,
voltage info/label rotate/.initial = 0,
voltage info/label/.initial = {}
}
\tikzset{%
voltage arrow/.style = {%
/utils/exec = {%
\pgfsetarrowoptions{direction ee}{%
.4*\the\tikzcircuitssizeunit+.3*\the\pgflinewidth}},
> = direction ee},
voltage info sloped/.style = {%
append after command = {%
\bgroup
[voltage info/.cd,#1]
[current point is local=true]
[every voltage info/.try]
[shift={($(\tikzlastnode.north)+(0,\pgfkeysvalueof{/tikz/voltage info/vertical offset}\tikzcircuitssizeunit)$)}]
[voltage arrow,\pgfkeysvalueof{/tikz/voltage info/arrow direction}]
(-\pgfkeysvalueof{/tikz/voltage info/horizontal offset}\tikzcircuitssizeunit,0)
edge[line to]
node[\pgfkeysvalueof{/tikz/voltage info/label
position},transform shape,rotate = \pgfkeysvalueof{/tikz/voltage
info/label rotate}] {\pgfkeysvalueof{/tikz/voltage info/label}}
(\pgfkeysvalueof{/tikz/voltage info/horizontal offset}\tikzcircuitssizeunit,0)
\egroup
}
},
voltage info' sloped/.style = {%
append after command = {%
\bgroup
[voltage info/.cd,label position = below,#1]
[current point is local=true]
[every voltage info/.try]
[shift={($(\tikzlastnode.south)+(0,-\pgfkeysvalueof{/tikz/voltage info/vertical offset}\tikzcircuitssizeunit)$)}]
[voltage arrow,\pgfkeysvalueof{/tikz/voltage info/arrow direction}]
(-\pgfkeysvalueof{/tikz/voltage info/horizontal offset}\tikzcircuitssizeunit,0)
edge[line to]
node[\pgfkeysvalueof{/tikz/voltage info/label
position},transform shape,rotate = \pgfkeysvalueof{/tikz/voltage
info/label rotate}] {\pgfkeysvalueof{/tikz/voltage info/label}}
(\pgfkeysvalueof{/tikz/voltage info/horizontal offset}\tikzcircuitssizeunit,0)
\egroup
}
},
voltage info/.style = {%
append after command = {%
\bgroup
[voltage info/.cd,#1]
[current point is local=true]
[every voltage info/.try]
[shift={($(\tikzlastnode.north)+(0,\pgfkeysvalueof{/tikz/voltage info/vertical offset}\tikzcircuitssizeunit)$)}]
[voltage arrow,\pgfkeysvalueof{/tikz/voltage info/arrow direction}]
(-\pgfkeysvalueof{/tikz/voltage info/horizontal offset}\tikzcircuitssizeunit,0)
edge[line to]
node[\pgfkeysvalueof{/tikz/voltage info/label
position},rotate = \pgfkeysvalueof{/tikz/voltage
info/label rotate}] {\pgfkeysvalueof{/tikz/voltage info/label}}
(\pgfkeysvalueof{/tikz/voltage info/horizontal offset}\tikzcircuitssizeunit,0)
\egroup
}
},
voltage info'/.style = {%
append after command = {%
\bgroup
[voltage info/.cd,label position = below,#1]
[current point is local=true]
[every voltage info/.try]
[shift={($(\tikzlastnode.south)+(0,-\pgfkeysvalueof{/tikz/voltage info/vertical offset}\tikzcircuitssizeunit)$)}]
[voltage arrow,\pgfkeysvalueof{/tikz/voltage info/arrow direction}]
(-\pgfkeysvalueof{/tikz/voltage info/horizontal offset}\tikzcircuitssizeunit,0)
edge[line to]
node[\pgfkeysvalueof{/tikz/voltage info/label
position},rotate = \pgfkeysvalueof{/tikz/voltage
info/label rotate}] {\pgfkeysvalueof{/tikz/voltage info/label}}
(\pgfkeysvalueof{/tikz/voltage info/horizontal offset}\tikzcircuitssizeunit,0)
\egroup
}
}
}
\begin{document}
\begin{tikzpicture}[circuit ee IEC]
\draw (0,0) to[resistor = {%
voltage info = {label = $u_R$},
voltage info' = {label = $u_R$}}] (2,2);
\draw (2,0) to[resistor = {%
voltage info sloped = {label = $u_R$},
voltage info' sloped= {label = $u_R$}}] (4,2);
\begin{scope}[shift = {(4.5,2)}]
\draw (0,0)
to[resistor = {voltage info sloped = {label = $u_R$}}]
++ (2,0)
to[resistor = {voltage info sloped = {label = $u_R$}}]
++ (0,-2)
to[resistor = {voltage info sloped = {label = $u_R$}}]
++ (-2,0);
\end{scope}
\end{tikzpicture}
\end{document}
答案1
我相信我可以帮助您解决前两点。
- 它允许颠倒标签。
这是因为你sloped
设置了选项。如果你关闭它,那么你的右边的例子就变成了
这样就修复了标签颠倒的问题。但是然后...
- 有时标签会与箭头重叠。
可以通过auto
为voltage info
节点提供选项来解决此问题。这会导致...
唯一的问题是,你的左边的例子看起来仍然有点不对......
但可以使用auto,swap
节点选项来解决voltage info'
这个问题,将这些标签放在箭头的另一侧。
完整代码如下。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{circuits.ee.IEC}
\tikzset{%
voltage info/.is family,
voltage info/vertical offset/.initial = .5,
voltage info/arrow direction/.initial = ->,
voltage info/horizontal offset/.initial = 2,
voltage info/label position/.initial = above,
voltage info/label rotate/.initial = 0,
voltage info/label/.initial = {}
}
\tikzset{%
voltage arrow/.style = {%
/utils/exec = {%
\pgfsetarrowoptions{direction ee}{%
.4*\the\tikzcircuitssizeunit+.3*\the\pgflinewidth}},
> = direction ee},
voltage info sloped/.style = {%
append after command = {%
\bgroup
[voltage info/.cd,#1]
[current point is local=true]
[every voltage info/.try]
[shift={($(\tikzlastnode.north)+(0,\pgfkeysvalueof{/tikz/voltage info/vertical offset}\tikzcircuitssizeunit)$)}]
[voltage arrow,\pgfkeysvalueof{/tikz/voltage info/arrow direction}]
(-\pgfkeysvalueof{/tikz/voltage info/horizontal offset}\tikzcircuitssizeunit,0)
edge[line to]
node[\pgfkeysvalueof{/tikz/voltage info/label
position},transform shape,rotate = \pgfkeysvalueof{/tikz/voltage
info/label rotate}] {\pgfkeysvalueof{/tikz/voltage info/label}}
(\pgfkeysvalueof{/tikz/voltage info/horizontal offset}\tikzcircuitssizeunit,0)
\egroup
}
},
voltage info' sloped/.style = {%
append after command = {%
\bgroup
[voltage info/.cd,label position = below,#1]
[current point is local=true]
[every voltage info/.try]
[shift={($(\tikzlastnode.south)+(0,-\pgfkeysvalueof{/tikz/voltage info/vertical offset}\tikzcircuitssizeunit)$)}]
[voltage arrow,\pgfkeysvalueof{/tikz/voltage info/arrow direction}]
(-\pgfkeysvalueof{/tikz/voltage info/horizontal offset}\tikzcircuitssizeunit,0)
edge[line to]
node[\pgfkeysvalueof{/tikz/voltage info/label
position},transform shape,rotate = \pgfkeysvalueof{/tikz/voltage
info/label rotate}] {\pgfkeysvalueof{/tikz/voltage info/label}}
(\pgfkeysvalueof{/tikz/voltage info/horizontal offset}\tikzcircuitssizeunit,0)
\egroup
}
},
voltage info/.style = {%
append after command = {%
\bgroup
[voltage info/.cd,#1]
[current point is local=true]
[every voltage info/.try]
[shift={($(\tikzlastnode.north)+(0,\pgfkeysvalueof{/tikz/voltage info/vertical offset}\tikzcircuitssizeunit)$)}]
[voltage arrow,\pgfkeysvalueof{/tikz/voltage info/arrow direction}]
(-\pgfkeysvalueof{/tikz/voltage info/horizontal offset}\tikzcircuitssizeunit,0)
edge[line to]
node[\pgfkeysvalueof{/tikz/voltage info/label
position},auto,rotate = \pgfkeysvalueof{/tikz/voltage
info/label rotate}] {\pgfkeysvalueof{/tikz/voltage info/label}}
(\pgfkeysvalueof{/tikz/voltage info/horizontal offset}\tikzcircuitssizeunit,0)
\egroup
}
},
voltage info'/.style = {%
append after command = {%
\bgroup
[voltage info/.cd,label position = below,#1]
[current point is local=true]
[every voltage info/.try]
[shift={($(\tikzlastnode.south)+(0,-\pgfkeysvalueof{/tikz/voltage info/vertical offset}\tikzcircuitssizeunit)$)}]
[voltage arrow,\pgfkeysvalueof{/tikz/voltage info/arrow direction}]
(-\pgfkeysvalueof{/tikz/voltage info/horizontal offset}\tikzcircuitssizeunit,0)
edge[line to]
node[\pgfkeysvalueof{/tikz/voltage info/label
position},auto,swap,rotate = \pgfkeysvalueof{/tikz/voltage
info/label rotate}] {\pgfkeysvalueof{/tikz/voltage info/label}}
(\pgfkeysvalueof{/tikz/voltage info/horizontal offset}\tikzcircuitssizeunit,0)
\egroup
}
}
}
\begin{document}
\begin{tikzpicture}[circuit ee IEC]
\draw (0,0) to[resistor = {%
voltage info = {label = $u_R$},
voltage info' = {label = $u_R$}}] (2,2);
\draw (2,0) to[resistor = {%
voltage info sloped = {label = $u_R$},
voltage info' sloped= {label = $u_R$}}] (4,2);
\begin{scope}[shift = {(4.5,2)}]
\draw (0,0)
to[resistor = {voltage info = {label = $u_R$}}]
++ (2,0)
to[resistor = {voltage info = {label = $u_R$}}]
++ (0,-2)
to[resistor = {voltage info = {label = $u_R$}}]
++ (-2,0);
\end{scope}
\end{tikzpicture}
\end{document}
答案2
我已经制作了这个,但没有使用tikz
,PSTricks
特别是pst-circ
可以绘制电路图的包...我相信它更强大(和更方便),但这是我的观点......
我在下面做了一个 MWE。
labelangle=:U
它是确定组件和标签方向(以及名称)之间角度的线。:U
将以相同的意义表达,但您可以指定所需的任何角度。
\documentclass[a4paper, 11pt]{article}
\usepackage[frenchb]{babel}
\usepackage{pst-circ}
\begin{document}
\begin{pspicture}[showgrid=true](0, -1)(12, 4)
\pnode(0, 0){A}
\pnode(3, 3){B}
\pnode(4, 0){C}
\pnode(7, 3){D}
\pnode(8, 3){E}
\pnode(11, 3){F}
\pnode(11, 0){G}
\pnode(8, 0){H}
\resistor[labeloffset=0,
labelangle=:U,
tensionlabel=$u_R$,
tensionoffset=0.7,
tensionlabeloffset=1](A)(B){$R$}
\resistor[labeloffset=0,
tensionlabel=$u_R$,
tensionoffset=0.7,
tensionlabeloffset=1](C)(D){$R$}
\resistor[labeloffset=0,
tensionlabel=$u_R$,
tensionoffset=-0.7,
tensionlabeloffset=-1,
directconvention=false](F)(E){$R$}
\resistor[labeloffset=0,
%labelangle=:U, TO PUT THE R IN THE SAME SENSE AS THE RESISTOR
tensionlabel=$u_R$,
tensionoffset=-0.7,
tensionlabeloffset=-1,
directconvention=false](G)(F){$R$}
\resistor[labeloffset=0,
tensionlabel=$u_R$,
tensionoffset=-0.7,
tensionlabeloffset=-1,
directconvention=false](H)(G){$R$}
\end{pspicture}
\end{document}
输出如下: