想知道如何改进我的代码!

想知道如何改进我的代码!

我想知道比我使用的代码更好的方法来完成下图。

我习惯使用 for 循环,但不习惯使用 LaTeX 循环!

在此处输入图片描述 代码

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

答案1

减少击键次数只是为了乐趣和社交“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}

在此处输入图片描述

答案2

使用 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}

在此处输入图片描述

答案3

另一个解决方案是重新定义\lvect, \rvect, \uvect, \dvect以接受可选参数(标签),只需 3\multido秒,无需 PostScript 代码。箭头有一些颜色,可以帮助我调试宏,可以删除:

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

在此处输入图片描述

相关内容