我有一张曲板电容器的图纸:
\documentclass[]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}[
baseline=(current bounding box.center),
capacitor/.style = {
line width=2mm,
color=black,
},
cable/.style = {
very thick,
color=black,
-o,
},
]
%
\def\upper{(1,4) to[out=-30,in=210] (9,4)}
\def\lower{(1,1) -- (9,1)}
\draw [thick, gray] \lower;
\draw [thick, gray, name path=membrane] \upper;
% invisible paths for intersections
\path [name path=verticalout] (7,0) -- (7,5);
% upper plate of the capacitor on the bended membrane
\begin{scope}
\clip (2.45,-1) rectangle (7.55,5.5);
\draw [capacitor] \upper;
\draw [capacitor] \lower;
\foreach \i in {2,3,...,8} {
\path[yshift=-4mm] (1,4) to[out=-30,in=210] node[red, pos=0.\i]{$+$} (9,4);
\path[yshift=4mm] (1,1) -- node[blue, pos=0.\i]{$-$} (9,1);
}
\end{scope}
% cables out
\draw [cable, name intersections={of=membrane and verticalout}]
(intersection-1) |- (11,5);
\draw [cable] (7,1) |- (11,-0.5);
\end{tikzpicture}
\end{document}
虽然我对最终的图形很满意,但我认为foreach
我用来放置红色加号和蓝色减号的部分相当不雅:
\upper
我不能在那里使用宏\lower
(我必须拆分路径构建表达式);\upper
如果我改变或\lower
路径,我不确定垂直对齐是否能得到保证我尝试使用文本装饰来实现这一点,但在三个方面失败了:
我必须使用类似的字符串,
$+ + + +$
因为我找不到如何“重复”单个字符符号沿着路径旋转
我放弃尝试将上方的“+”与下方的“-”对齐......
是否有一种通用的方法可以将一系列重复的符号沿着路径放置?
答案1
最终我采用了如下代码:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usetikzlibrary{intersections}
\usetikzlibrary{patterns}
\usetikzlibrary{shadows}
\begin{document}
\begin{tikzpicture}[
baseline=(current bounding box.center),
scale=0.5,
every node/.style={transform shape}, %% to apply scale to nodes
capacitor/.style = {
line width=2mm,
color=black,
},
cable/.style = {
very thick,
color=black,
-o,
},
]
% internal chamber
\path [fill=gray!20] (1,4) to[out=-30,in=210] (9,4) --
node [left, pos=0.7]{\Large $P_0=1$~atm}
(9,1) -- (1, 1) -- cycle;
% structure
\draw [pattern=north east lines, pattern color=gray]
(0,0) -- (0,4) -- (1,4) -- (1,1) --
(9,1) -- (9,4) -- (10,4) -- (10,0) -- cycle;
\draw [thick, name path=membrane] (1,4) to[out=-30,in=210] (9,4);
% invisible paths for intersections
\path [name path=vertical1] (2,0) -- (2,5);
\path [name path=vertical2] (7,0) -- (7,5);
% big arrow with $P$ into it
\path [fill=gray!40, drop shadow ] (4, 6) -- (4,4) -- (3.5, 4) --
(5,3) -- (6.5, 4) -- (6,4) -- (6, 6);
\path (5,4) node {\Huge $P$};
\path (2,5.5) node [align=left] (mtext)
{\Large Elastic \\ \Large membrane};
\draw [name intersections={of=membrane and vertical1}]
(mtext.south) edge[bend left, ->, shorten >=2pt] (intersection-1);
% upper plate of the capacitor on the bended membrane
\begin{scope}
\clip (2.45,-1) rectangle (7.55,5.5);
\draw [capacitor] (1,4) to[out=-30,in=210] (9,4);
%% pluses and minuses
\foreach \i in {2,3,...,8} {
\path[yshift=-4mm]
(1,4) to[out=-30,in=210] node[red, pos=0.\i]{$+$} (9,4);
\path[yshift=4mm]
(1,1) -- node[blue, pos=0.\i]{$-$} (9,1);
}
\end{scope}
% lower plate of the capacitor
\draw [capacitor] (2.5,1) -- (7.5,1);
\draw [cable, name intersections={of=membrane and vertical2}]
(intersection-1) |- (11,5);
\draw [cable] (7,1) |- (11,-0.5);
\end{tikzpicture}
\end{document}