灵感来自Clemens Niederberger 关于可能的新软件包的帖子,我曾尝试使用 PSTricks 创建类似的图形。
代码
\documentclass{article}
\usepackage{chemmacros}
\usepackage{pstricks-add}
\pagestyle{empty}
\begin{document}
\begin{figure}[htbp]
% Parameters
\def\basinwidth{15 }
\def\basinheight{10 }
\def\waterheight{8}
\def\cathode{\ch{Zn}}
\def\cathodeColour{gray!30}
\def\anode{\ch{Cu}}
\def\anodeColour{red!50}
\centering
\psset{unit=0.7\psunit}
\begin{pspicture}(15,11.5)
% Basin
\psline(0,\basinheight)(0,\waterheight)
\pscustom[fillstyle=solid,fillcolor=cyan!70]{%
\psline(0,\waterheight)(\basinwidth,\waterheight)
\psline(0,\waterheight)(0,1)
\psarc(1,1){1}{180}{270}
\psline(1,0)(!\basinwidth 1 sub 0)
\psarc(!\basinwidth 1 sub 1){1}{270}{360}
\psline(\basinwidth,1)(\basinwidth,\waterheight)
\closepath
}
\psline(\basinwidth,\waterheight)(\basinwidth,\basinheight)
% Membrane
\psline[linestyle=dashed]%
(!\basinwidth 2 div 0)%
(!\basinwidth 2 div \waterheight)
% Cathode
\pspolygon[fillstyle=solid,fillcolor=\cathodeColour]%
(!\basinwidth 4 div 1 sub 1)%
(!\basinwidth 4 div 1 sub \basinheight 1 sub)%
(!\basinwidth 4 div 1 add \basinheight 1 sub)%
(!\basinwidth 4 div 1 add 1)
\rput(!\basinwidth 4 div \basinheight 2 div){\cathode}
% Anode
\pspolygon[fillstyle=solid,fillcolor=\anodeColour]%
(!3 \basinwidth mul 4 div 1 sub 1)%
(!3 \basinwidth mul 4 div 1 sub \basinheight 1 sub)%
(!3 \basinwidth mul 4 div 1 add \basinheight 1 sub)%
(!3 \basinwidth mul 4 div 1 add 1)
\rput(!3 \basinwidth mul 4 div \basinheight 2 div){\anode}
% Wires
\rput(!\basinwidth 4 div 1 add \basinheight){$+$}
\psline(!\basinwidth 4 div \basinheight 1 sub)%
(!\basinwidth 4 div \basinheight)
\psarc(!\basinwidth 4 div 1 add \basinheight){1}{90}{180}
\psline(!\basinwidth 4 div 1 add \basinheight 1 add)%
(!\basinwidth 1 sub 2 div \basinheight 1 add)
\pscircle(!\basinwidth 2 div \basinheight 1 add){0.5}
\rput(!\basinwidth 2 div \basinheight 1 add){$U$}
\psline(!3 \basinwidth mul 4 div 1 sub \basinheight 1 add)%
(!\basinwidth 1 add 2 div \basinheight 1 add)
\psarc(!3 \basinwidth mul 4 div 1 sub \basinheight){1}{0}{90}
\psline(!3 \basinwidth mul 4 div \basinheight 1 sub)%
(!3 \basinwidth mul 4 div \basinheight)
\rput(!3 \basinwidth mul 4 div 1 sub \basinheight){$-$}
% Electron movement
\rput(!3 \basinwidth mul 1 add 8 div \basinheight 3 2 div add)%
{\ch{->[ e- ]}}
\rput(!5 \basinwidth mul 2 sub 8 div \basinheight 3 2 div add)%
{\ch{->[ e- ]}}
\end{pspicture}
\end{figure}
\end{document}
代码应该用 进行编译xelatex
。
问题
是否可以定义一个类似以下参数的新对象?
\cell[
cathode=Zn,
cathodeColour=grey!30,
anode=Cu,
anodeColour=red!50
]{"basinwidth"}{"basinheight"}{"waterheight"}
如果可能的话,我该怎么做?
附加问题
我看过了chemmacros 手册为了绘制上面带有电子的箭头,但我无法让它工作\ch{ ->[\el] }
。如果有人能告诉我如何绘制它,那就太好了。(解决了)
答案1
我对 PSTricks 没有任何经验,所以我不知道是否有比我建议的方法更好的方法。在我看来,这是一项简单的任务:将整个定义包装在一个带有可选参数的宏中。PSTricks 加载xkeyval
因此,您可以使用这个包来定义键,然后可以通过可选参数进行设置:
% default definitions:
\def\cathode{\ch{Zn}}
\def\cathodeColour{gray!30}
\def\anode{\ch{Cu}}
\def\anodeColour{red!50}
\makeatletter
% keys for the optional argument:
\define@key{cell}{cathode}{\def\cathode{\ch{#1}}}
\define@key{cell}{cathodeColour}{\def\cathodeColour{#1}}
\define@key{cell}{anode}{\def\anode{\ch{#1}}}
\define@key{cell}{anodeColour}{\def\anodeColour{#1}}
\makeatother
% \cell[<options>]{<basinwidth>}{<basinheight>}{<waterheight>}
\newcommand\cell[4][]{%
\begingroup
% set the keys:
\setkeys{cell}{#1}%
\def\basinwidth{#2 }%
\def\basinheight{#3 }%
\def\waterheight{#4 }%
% the rest of the definition:
...
\endgroup
分组是为了将键的设置保持在本地。但是,由于它们将在 a 中使用,pspicture
因此可能不需要。如果您想访问单元格的坐标,最好还是省略它。(正如我所说:我没有使用 PSTricks 的经验,所以我没有测试过这个……)
以下是基于示例代码的完整示例:
\documentclass{article}
\usepackage{chemmacros}
\usepackage{pstricks-add}
\pagestyle{empty}
\def\cathode{\ch{Zn}}
\def\cathodeColour{gray!30}
\def\anode{\ch{Cu}}
\def\anodeColour{red!50}
\makeatletter
\define@key{cell}{cathode}{\def\cathode{\ch{#1}}}
\define@key{cell}{cathodeColour}{\def\cathodeColour{#1}}
\define@key{cell}{anode}{\def\anode{\ch{#1}}}
\define@key{cell}{anodeColour}{\def\anodeColour{#1}}
\makeatother
\newcommand\cell[4][]{%
\begingroup
\setkeys{cell}{#1}%
\def\basinwidth{#2 }%
\def\basinheight{#3 }%
\def\waterheight{#4 }%
% Basin
\psline(0,\basinheight)(0,\waterheight)
\pscustom[fillstyle=solid,fillcolor=cyan!70]{%
\psline(0,\waterheight)(\basinwidth,\waterheight)
\psline(0,\waterheight)(0,1)
\psarc(1,1){1}{180}{270}
\psline(1,0)(!\basinwidth 1 sub 0)
\psarc(!\basinwidth 1 sub 1){1}{270}{360}
\psline(\basinwidth,1)(\basinwidth,\waterheight)
\closepath
}
\psline(\basinwidth,\waterheight)(\basinwidth,\basinheight)
% Membrane
\psline[linestyle=dashed]%
(!\basinwidth 2 div 0)%
(!\basinwidth 2 div \waterheight)
% Cathode
\pspolygon[fillstyle=solid,fillcolor=\cathodeColour]%
(!\basinwidth 4 div 1 sub 1)%
(!\basinwidth 4 div 1 sub \basinheight 1 sub)%
(!\basinwidth 4 div 1 add \basinheight 1 sub)%
(!\basinwidth 4 div 1 add 1)
\rput(!\basinwidth 4 div \basinheight 2 div){\cathode}
% Anode
\pspolygon[fillstyle=solid,fillcolor=\anodeColour]%
(!3 \basinwidth mul 4 div 1 sub 1)%
(!3 \basinwidth mul 4 div 1 sub \basinheight 1 sub)%
(!3 \basinwidth mul 4 div 1 add \basinheight 1 sub)%
(!3 \basinwidth mul 4 div 1 add 1)
\rput(!3 \basinwidth mul 4 div \basinheight 2 div){\anode}
% Wires
\rput(!\basinwidth 4 div 1 add \basinheight){$+$}
\psline(!\basinwidth 4 div \basinheight 1 sub)%
(!\basinwidth 4 div \basinheight)
\psarc(!\basinwidth 4 div 1 add \basinheight){1}{90}{180}
\psline(!\basinwidth 4 div 1 add \basinheight 1 add)%
(!\basinwidth 1 sub 2 div \basinheight 1 add)
\pscircle(!\basinwidth 2 div \basinheight 1 add){0.5}
\rput(!\basinwidth 2 div \basinheight 1 add){$U$}
\psline(!3 \basinwidth mul 4 div 1 sub \basinheight 1 add)%
(!\basinwidth 1 add 2 div \basinheight 1 add)
\psarc(!3 \basinwidth mul 4 div 1 sub \basinheight){1}{0}{90}
\psline(!3 \basinwidth mul 4 div \basinheight 1 sub)%
(!3 \basinwidth mul 4 div \basinheight)
\rput(!3 \basinwidth mul 4 div 1 sub \basinheight){$-$}
% Electron movement
\rput(!3 \basinwidth mul 1 add 8 div \basinheight 3 2 div add)%
{\ch{->[ e- ]}}
\rput(!5 \basinwidth mul 2 sub 8 div \basinheight 3 2 div add)%
{\ch{->[ e- ]}}
\endgroup
}
\begin{document}
\begin{figure}[htbp]
\centering
\psset{unit=0.7\psunit}
\begin{pspicture}(15,11.5)
\cell{15}{10}{8}
\end{pspicture}
\end{figure}
\begin{figure}[htbp]
\centering
\psset{unit=0.7\psunit}
\begin{pspicture}(15,11.5)
\cell[
cathode=Cu,
cathodeColour=red!50,
anode=Ag,
anodeColour=gray!10
]{15}{10}{8}
\end{pspicture}
\end{figure}
\end{document}