考虑以下示例。
代码
\documentclass{article}
\usepackage{pstricks-add}
\begin{document}
\def\laengde{60 }
\psset{unit=0.04}
\begin{pspicture}(260,260)
\pnode(0,0){P1}
\pnode(!0 \laengde){P2}
\pnode(!0 2 \laengde mul){P3}
\pnode(!0 3 \laengde mul){P4}
\pnode(!0 4 \laengde mul){P5}
\pnode(!\laengde 4 \laengde mul){P6}
\pnode(!2 \laengde mul 4 \laengde mul){P7}
\pnode(!3 \laengde mul 4 \laengde mul){P8}
\pnode(!4 \laengde mul 4 \laengde mul){P9}
\pnode(!4 \laengde mul 3 \laengde mul){P10}
\pnode(!4 \laengde mul 2 \laengde mul){P11}
\pnode(!4 \laengde mul \laengde){P12}
\pnode(!4 \laengde mul 0){P13}
\pnode(!3 \laengde mul 0){P14}
\pnode(!2 \laengde mul 0){P15}
\pnode(!\laengde 0){P16}
\psline(P1)(P2)
\psarc(P3){\laengde}{270}{90}
\psline(P4)(P5)(P6)
\psarc(P7){\laengde}{180}{0}
\psline(P8)(P9)(P10)
\psarc(P11){\laengde}{90}{270}
\psline(P12)(P13)(P14)
\psarc(P15){\laengde}{0}{180}
\psline(P16)(P1)
\pspolygon(P3)(P7)(P11)(P15)
\multido{\iA=5+1,\iB=6+1}{8}{%
\pcline[offset=12pt]{|<->|}(P\iA)(P\iB)
\ncput*[nrot=:U]{\laengde}
}
\end{pspicture}
\end{document}
输出
问题
我确信代码可以改进/优化。我该怎么做?
更新 1
我试图简化这十六个的创建\pnodes
,但是当它们被下面代码中的四行注释掉的行替换时,我无法编译。
\documentclass{article}
\usepackage{pstricks-add}
\begin{document}
\def\laengde{60 }
\psset{unit=0.04}
\begin{pspicture}(260,260)
%\multido{\iA=1+1}{4}{\pnode(!0 \iA \laengde mul){P\iA}}
\pnode(!0 1 \laengde mul){P1}
\pnode(!0 2 \laengde mul){P2}
\pnode(!0 3 \laengde mul){P3}
\pnode(!0 4 \laengde mul){P4}
%\multido{\iB=1+1,\iC=5+1}{4}{\pnode(!\iB \laengde mul 4 \laengde mul){P\iC}}
\pnode(!1 \laengde mul 4 \laengde mul){P5}
\pnode(!2 \laengde mul 4 \laengde mul){P6}
\pnode(!3 \laengde mul 4 \laengde mul){P7}
\pnode(!4 \laengde mul 4 \laengde mul){P8}
%\multido{\iD=0+-1,\iE=9+1}{4}{\pnode(!4 \laengde mul \iD \laengde mul){P\iE}}
\pnode(!4 \laengde mul 3 \laengde mul){P9}
\pnode(!4 \laengde mul 2 \laengde mul){P10}
\pnode(!4 \laengde mul 1 \laengde mul){P11}
\pnode(!4 \laengde mul 0 \laengde mul){P12}
%\multido{\iF=0+-1,\iG=9+1}{4}{\pnode(!\iF \laengde mul 0){P\iG}}
\pnode(!3 \laengde mul 0){P13}
\pnode(!2 \laengde mul 0){P14}
\pnode(!1 \laengde mul 0){P15}
\pnode(!0 \laengde mul 0){P16}
\psline(P16)(P1)
\psarc(P2){\laengde}{270}{90}
\psline(P3)(P4)(P5)
\psarc(P6){\laengde}{180}{0}
\psline(P7)(P8)(P9)
\psarc(P10){\laengde}{90}{270}
\psline(P11)(P12)(P13)
\psarc(P14){\laengde}{0}{180}
\psline(P15)(P16)
\pspolygon(P2)(P6)(P10)(P14)
\multido{\iH=4+1,\iI=5+1}{8}{%
\pcline[offset=12pt]{|<->|}(P\iH)(P\iI)
\ncput*[nrot=:U]{\laengde}
}
\end{pspicture}
\end{document}
答案1
对我来说,在只需要坐标的地方使用节点毫无意义。将单位定义为 60 的倍数,那么您可以简单地使用 1、2、3……,而不受长度/单位设置的影响:
\documentclass{article}
\usepackage{pstricks-add}
\begin{document}
\psset{unit=60pt}
\begin{pspicture}(-2,-2)(2,2)
\pspolygon(-2,0)(0,2)(2,0)(0,-2)
\psline(-2,1)(-2,2)(-1,2)
\psarc(0,2){1}{180}{0}
\psline(1,2)(2,2)(2,1)
\psarc(2,0){1}{90}{270}
\psline(2,-1)(2,-2)(1,-2)
\psarc(0,-2){1}{0}{180}
\psline(-1,-2)(-2,-2)(-2,-1)
\psarc(-2,0){1}{270}{90}
\multido{\iA=-2+1}{4}{%
\pcline[offset=12pt]{|<->|}(\iA,2)(!\iA\space 1 add 2) \ncput*[nrot=:U]{60}
\pcline[offset=-12pt]{|<->|}(2,\iA)(!2 \iA\space 1 add)\ncput*[nrot=:U]{60}}
\end{pspicture}
\end{document}
定义一个元素然后移动并旋转也是一样:
\documentclass{article}
\usepackage{pstricks-add}
\def\Element{%
\psline(-2,2)(-1,2)\psarc(0,2){1}{180}{0}\psline(1,2)(2,2)
\psline(-1,1)(0,2)(1,1)}
\begin{document}
\psset{unit=60pt}
\begin{pspicture}(-2,-2)(2,2)
\multido{\iA=-90+90,\iB=-2+1}{4}{%
\rput{\iA}(0,0){\Element}
\pcline[offset=12pt]{|<->|}(\iB,2)(!\iB\space 1 add 2) \ncput*[nrot=:U]{60}
\pcline[offset=-12pt]{|<->|}(2,\iB)(!2 \iB\space 1 add)\ncput*[nrot=:U]{60}}
\end{pspicture}
\end{document}
如果你更喜欢通过命令进行设置,那么使用
\def\laengde{60}
\psset{unit=\laengde pt}
答案2
面向对象的PSTricks:
\documentclass[pstricks,border={12pt 12pt 24pt 24pt}]{standalone}
\usepackage{pst-node}
\usepackage{multido}
\def\Atom
{
\psarcn(0,0){2}{0}{-90}
\psline(2,0)(4,0)
\psline(2,-2)
}
\def\Molecule(#1)#2%!
{
\rput{#2}(#1)
{
\Atom
\psscalebox{-1 1}{\Atom}%!
}
}
\def\Label
{
\pcline[offset=12pt]{|<*->|*}(0,0)(2,0)
\ncput*{60}
}
\begin{document}
\begin{pspicture}(-4,-4)(4,4)
\multido{\i=90+90}{4}{\Molecule(4;\i){!\i\space 90 sub}}
\multirput(-4,4)(2,0){4}{\Label}
\multirput{-90}(4,4)(0,-2){4}{\Label}
\end{pspicture}
\end{document}
动画片:
\documentclass{beamer}
\usepackage{pst-node}
\usepackage{multido}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{pspicture}
\renewcommand{\PreviewBbAdjust}{-12pt -12pt 24pt 24pt}
\def\Atom
{%!
\only<+->{\psarcn(0,0){2}{0}{-90}}%!
\only<+->{\psline(2,0)(4,0)}%!
\only<+->{\psline(2,-2)}%!
}
\def\Molecule(#1)#2%!
{
\rput{#2}(#1)
{
\Atom
\psscalebox{-1 1}{\Atom}%!
}
}
\def\Label
{%!
\only<+->{\pcline[offset=12pt]{|<*->|*}(0,0)(2,0)}%!
\only<+->{\ncput*{60}}%!
}
\begin{document}
\begin{frame}
\begin{pspicture}(-4,-4)(4,4)
\multido{\i=90+90}{4}{\Molecule(4;\i){!\i\space 90 sub}}
\multido{\i=-4+2}{4}{\rput(\i,4){\Label}}
\multido{\i=4+-2}{4}{\rput{-90}(4,\i){\Label}}
\end{pspicture}
\end{frame}
\end{document}