6

Consider the following example.

Code

\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}

Output

enter image description here

Question

I'm pretty sure the code can be improved/optimized. How do I do that?


Update 1

I have tried to simplify the creation of the sixteen \pnodes, but when they are replaced be the four outcommented lines in the code below, I cannot compile.

\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}

2 Answers 2

4

it makes no sense to me to use nodes where only coordinates are needed. Define a unit as multiple of 60 then you can simply use 1,2,3,... independent from your length/unit setting:

\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}

enter image description here

and the same with defining an element which is then moved and rotated:

\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}

and if you prefer the setting via a command then use

\def\laengde{60}
\psset{unit=\laengde pt}
3
  • Very nice, Herbert! Commented Apr 2, 2013 at 13:00
  • I was wondering what you edited in your post the last time? (I can't see any difference.) Commented Apr 2, 2013 at 16:00
  • there is now only one \multido in the second example.
    – user2478
    Commented Apr 2, 2013 at 16:21
3

Object Oriented PSTricks:

enter image description here

\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}

Animation:

enter image description here

\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}
2
  • Thank you! I try to achieve the same output as I posted but with the least amount of keystroke possible, i.e, optimize the code as much as possible. (I have accepted Herbert's answer, but I would very much like to see your suggestion, too.) Commented Apr 2, 2013 at 13:11
  • @SvendMortensen: |<*->|* is also important to consider. Commented Apr 2, 2013 at 15:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .