4

I would like to know better way of accomplishing the following figure than the code I used.

I'm used to for loops, but not to LaTeX loops!

enter image description here THE CODE

\documentclass[a4paper,11pt,x11names,svgnames]{article}
\usepackage[margin =1.5cm]{geometry}
\usepackage{pstricks-add}
\usepackage{auto-pst-pdf}
\usepackage{xcolor}
\usepackage{amsmath}

\def\rvect{\psline{->}(-0.5,0)(0,0)}
\def\lvect{\psline{->}(0.5,0)(0,0)}
\def\uvect{\psline{->}(0,-0.5)(0,0)}
\def\dvect{\psline{->}(0,0.5)(0,0)}

\begin{document}%

\begin{pspicture}(-5,-5)(5,5)
\pscircle[linestyle=dashed](0,0){4.0}
\multido{\i = 0 + 45}{8}{
         \psset{origin = {4 ; \i}}\psline(-0.1 ; \i)(0.1 ; \i)
        }
        %
        \rput(4.35;0){1}
        \rput(4.35;45){2}
        \rput(4.35;90){3}
        \rput(4.35;135){4}
        \rput(4.35;180){5}
        \rput(4.35;225){6}
        \rput(4.35;270){7}
        \rput(4.35;315){8}
        %
        \rput(4.5;22.5){\pscirclebox{1}}
        \rput(4.5;67.5){\pscirclebox{2}}
        \rput(4.5;112.5){\pscirclebox{3}}
        \rput(4.5;157.5){\pscirclebox{4}}
        \rput(4.5;202.5){\pscirclebox{5}}
        \rput(4.5;247.5){\pscirclebox{6}}
        \rput(4.5;292.5){\pscirclebox{7}}
        \rput(4.5;337.5){\pscirclebox{8}}
        %
        %
\multido{\n = -67.5 + 45.0}{4}{
         \rput(4.0;\n){\rvect}
         \rput(-4.0;\n){\lvect}
        }%
\multido{\n = 22.5 + 45.0}{4}{
         \rput(4;\n){\uvect}
         \rput(-4;\n){\dvect}
        }%
        \rput(3.3;28){$\rho_{x_{1}}$}
        \rput(3.65;14){$\rho_{y_{1}}$}
        \rput(-3.3;28){$\rho_{x_{5}}$}
        \rput(-3.6;14){$\rho_{y_{5}}$}
        \rput(3.7;78){$\rho_{x_{2}}$}
        \rput(3.4;62){$\rho_{y_{2}}$}
        \rput(-3.75;79){$\rho_{x_{6}}$}
        \rput(-3.35;66){$\rho_{y_{6}}$}
        %
        \rput(3.3;-28){$\rho_{x_{8}}$}
        \rput(3.65;-13.5){$\rho_{y_{8}}$}
        \rput(-3.3;-28){$\rho_{x_{4}}$}
        \rput(-3.6;-14){$\rho_{y_{4}}$}
        \rput(3.7;-78){$\rho_{x_{7}}$}
        \rput(3.4;-61){$\rho_{y_{7}}$}
        \rput(-3.75;-79){$\rho_{x_{3}}$}
        \rput(-3.3;-66){$\rho_{y_{3}}$}

\end{pspicture}

\end{document} 

3 Answers 3

5

Reducing the number of keystrokes just 4 fun and for socializing "Go Green".

\documentclass[pstricks,12pt]{standalone}
\usepackage{pst-eucl}

\begin{document}
\begin{pspicture}[npos=1.6](-6,-6)(6,6)
    \pscircle[linestyle=dashed]{5}
    \multido{\i=0+45,\n=22.5+45.0,\ic=1+1}{8}
    {
        \psline(4.8;\i)(5.2;\i)
        \uput{12pt}[\i](5;\i){\ic}
        \pnode(5;\n){P}
        \uput{8pt}[\n](P){\pscirclebox{\ic}}
        \rput(P){\pnode(!\n\space cos 0 lt {.7}{-.7} ifelse 0){x\ic}\pnode(!0 \n\space sin 0 lt {.7}{-.7} ifelse){y\ic}}
        \pcline{<-}(P)(x\ic)\ncput{$\rho_{x_{\ic}}$}
        \pcline{<-}(P)(y\ic)\ncput{$\rho_{y_{\ic}}$}
    }
\end{pspicture}
\end{document}

enter image description here

2
  • Can u plz explain the following lines. \Circlenode; \pnode; \pcline
    – ltxEnthu
    Sep 20, 2014 at 13:03
  • \Circlenode for creating a circular node enclosing the number in your case. \pnode for creating a point node enclosing nothing. A node can be considered as a named variable of a 2D point so you can reuse it many times in your code. \pcline for creating a line joining two nodes. Sep 20, 2014 at 13:21
9

Reducing the keystrokes even further (just for fun of course) with TikZ:

\documentclass[tikz,border=5]{standalone}
\begin{document}
\begin{tikzpicture}[>=stealth]
\draw [dashed] circle [radius=4];
\foreach \i [evaluate={\a=\i*45-45; \b=\a+22.5; \c=cos(\b)<0; \d=sin(\b)<0;}] in {1,...,8}{
  \draw (\a:3.875) -- (\a:4.125) node [at end, anchor=\a+180] {\i};
  \tikzset{shift=(\b:4)}
  \node [anchor=\b+180, circle, draw, outer sep=1ex] {\i};
  \draw [->] (\c*1.5-.75,0) -- (0,0) node [at start, anchor=180*\c] {$\rho_{x_{\i}}$};
  \draw [->] (0,\d*1.5-.75) -- (0,0) node [at start, anchor=180*\d+90] {$\rho_{y_{\i}}$};
}
\end{tikzpicture}
\end{document}

enter image description here

1
  • O O O!. Good competition. +1 for a befitting tikz version! :-)
    – user11232
    Sep 21, 2014 at 1:46
3

Another solution, redefining the \lvect, \rvect, \uvect, \dvect to accept optional arguments (the labels), in 3 \multidos and no PostScript code. There is some colour for arrows, that helped me debug my macros, which can be deleted:

\documentclass[a4paper,11pt,x11names,svgnames]{article}
\usepackage[margin =1.5cm]{geometry}
\usepackage{pstricks-add}
\usepackage{auto-pst-pdf}
\usepackage{xcolor}
\usepackage{amsmath}

\newcommand\rvect[1][]{\psline{->}(-0.5,0)(0,0)\uput{2pt}[l](-0.5,0){#1}}
\newcommand\lvect[1][]{\psline{->}(0.5,0)(0,0)\uput{2pt}[r](0.5,0){#1}}
\newcommand\uvect[1][]{\psline{->}(0,-0.5)(0,0)\uput{2pt}[d](0,-0.5){#1}}
\newcommand\dvect[1][]{\psline{->}(0,0.5)(0,0)\uput{2pt}[u](0,0.5){#1}}

\begin{document}%

\begin{pspicture}(-5,-5)(5,5)
\pscircle[linestyle=dashed](0,0){4.0}
\multido{\n = 0 + 45,\i =1+1}{8}{%
    \psset{origin = {4 ; \n}}
    \psline(-0.1 ; \n)(0.1 ; \n)\rput(4.35;\n){\i}
    }%
%
\multido{\n = 22.5 + 45.0, \i=1+1, \is=5+1}{2}{%
    \rput(4;\n){\psset{linecolor = green}\rvect[$ρ_{x_{\i}}$]\uvect[$ρ_{y_{\i}}$]}
    \rput(4.5;\n){\pscirclebox{\i}}
    \rput(-4;\n){\lvect[$ρ_{x_{\is}}$]\dvect[$ρ_{y_{\is}}$]}
    \rput(-4.5;\n){\pscirclebox{\is}}
    }%
%
\multido{\n = 112.5 + 45.0, \i=3+1, \is=7+1}{2}{%
    \rput(4; \n){\psset{linecolor = red}\lvect[$ρ_{x_{\i}}$]\uvect[$ρ_{y_{\i}}$]}
    \rput(4.5;\n){\pscirclebox{\i}}
    \rput(-4;\n){\psset{linecolor = blue}\rvect[$ρ_{x_{\is}}$]\dvect[$ρ_{y_{\is}}$]}
    \rput(-4.5;\n){\pscirclebox{\is}}
    }%
\end{pspicture}

\end{document} 

enter image description here

You must log in to answer this question.

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