使用 PSTricks 构建流程图

使用 PSTricks 构建流程图

我正在构建几个类似的流程图。它们表示分割三角形的过程(算法)。现在,我使用以下代码:

\newcommand{\thetriangle}{
 \pspolygon[fillstyle=solid,fillcolor=green!20](0,0)(1.5,0)(0,1.5)
}
\newcommand{\godown}[1] {
  \psline{->}(0.5,-0.1)(0.5,-0.4)
  \rput[Br](0.48,-0.25){#1}
}
\newcommand{\goright}[1] {
  \psline{->}(1.1,-0.1)(1.5,-0.4)
  \rput[Bl](1.5,-0.25){#1}
}
\newcommand{\steptitle}[1] {
  \rput[Br](-0.2,0.5){\emph{\textcolor{ForestGreen}{#1}}}
}
\begin{pspicture}(4,8)
\rput(0,8) {
 \steptitle{Step 1}
 \thetriangle
 \rput(0.5,0.5){Divide}
 \rput(0.5,0.3){to n}
 \godown{}
}
\rput(0,6) {
 \thetriangle
 \steptitle{Step 2}
 \psline[linestyle=dashed,linecolor=black](0,0)(0.75,0.75) 
 \rput{270}(0.2,0.75){k agents}
 \rput(0.75,0.2){n-k agents}
 \godown{$k=n$}
 \goright{$1\leq k\leq n-1$}
}
\rput(2,4) {
 \thetriangle
\psline[linestyle=solid,linecolor=black](0,0)(0.75,0.75) \rput{270}(0.2,0.75){Divide to k}
 \rput(0.75,0.2){Divide to n-k}
}
\rput(0,4) {
 \thetriangle
 \steptitle{Step 3}
 \psline[linestyle=dotted,linecolor=blue](0,0.1)(0.7,0.8)
\psline[linestyle=dotted,linecolor=blue](0,0.2)(0.65,0.85)
 \psline[linestyle=dotted,linecolor=blue](0,0.3)(0.6,0.9)
 \godown{select eastern}
}
\rput(0,2) {
 \thetriangle
 \psline[linestyle=solid,linecolor=blue](0,0.1)(0.7,0.8)
 \rput(0.25,1){Divide}
 \rput(0.25,0.8){to}
 \rput(0.25,0.6){$n-1$}
 \rput(0.7,0.2){Give to $1$}
}
\end{pspicture}

但这很不方便,因为我必须明确地将每个步骤放在其确切的位置(例如“\rput(0,6)”)。这使得添加/删除步骤、更改步骤之间的顺序等变得很困难。

有没有更方便的方法用几个块来创建一张图片?

更新:根据 Herbert 的回答,我将“pspicture”环境替换为“psmatrix”环境。基于赫伯特的另一个回答,我添加了“makebox”标签来创建可变宽度的列。结果如下:

\begin{psmatrix}[colsep=0,rowsep=2]
    \makebox[0cm]{} & \makebox[3cm]{} & \makebox[4cm]{} \\
    \steptitle{Step 1} & [name=Step1] 
       \thetriangle
       \rput(0.5,0.5){\shortstack[l]{Divide\\to n}}
       \godown{}
       \\
    \steptitle{Step 2} & [name=Step2] 
       \thetriangle
       \psline[linestyle=dashed,linecolor=black](0,0)(0.75,0.75) 
       \rput{270}(0.2,0.75){k agents}
       \rput(0.75,0.2){n-k agents}
       \godown{$k=n$}
       \goright{$1\leq k\leq n-1$} \\
    \steptitle{Step 3} & [name=Step3a]
       \thetriangle
       \psline[linestyle=dotted,linecolor=blue](0,0.1)(0.7,0.8)
       \psline[linestyle=dotted,linecolor=blue](0,0.2)(0.65,0.85)
       \psline[linestyle=dotted,linecolor=blue](0,0.3)(0.6,0.9)
       \godown{select eastern} & 
     [name=Step3b] 
       \thetriangle
       \psline[linestyle=solid,linecolor=black](0,0)(0.75,0.75)
       \rput{270}(0.2,0.75){Divide to k}
       \rput(0.75,0.2){Divide to n-k} \\
     \steptitle{} & [name=Step4] 
       \thetriangle
       \psline[linestyle=solid,linecolor=blue](0,0.1)(0.7,0.8)
       \rput(0.25,0.8){\shortstack[l]{Divide\\to\\$n-1$}}
       \rput(0.7,0.2){Give to $1$}
\end{psmatrix} 

答案1

\documentclass{article}
\usepackage[dvipsnames]{pstricks}
\usepackage{pst-node}
\pagestyle{empty}
\begin{document}

\newcommand\TTR[2][]{\pspolygon[fillstyle=solid,fillcolor=green!20](0,0)(2,0)(0,2)%
  \ifx\relax#1\relax\else\psline[linestyle=#1](0,0)(1,1)\fi%
  \rput[lb](0.1,0.01){#2}}

\footnotesize
\begin{psmatrix}[colsep=3cm,rowsep=2.75cm]
    [name=A] \TTR{\shortstack{Devide\\[-3pt]to n}} \\
    [name=B] \TTR[dashed]{\shortstack[l]{\psrotateleft{k agents}\\[-3pt] n-k agents}}\\
    [name=C1] \TTR[dotted]{\shortstack[l]{select\\[-3pt] eastern}} & 
        [name=C2] \TTR[solid]{\shortstack[l]{\psrotateleft{Devide to k}\\[-3pt] Devide to n-k}} \\
    [name=D] \TTR[solid]{\shortstack[l]{\psrotateleft{Devide to n-1}\\[-3pt] Devide to 1}}
\end{psmatrix}
\uput{15pt}[180]([offset=8mm]A){\emph{\textcolor{ForestGreen}{Step 1}}}
\uput{15pt}[180]([offset=8mm]B){\emph{\textcolor{ForestGreen}{Step 2}}}
\uput{15pt}[180]([offset=8mm]C1){\emph{\textcolor{ForestGreen}{Step 3}}}
\ncline[offset=1cm,nodesepA=2mm,nodesepB=13mm]{->}{A}{B}
\ncline[offset=1cm,nodesepA=2mm,nodesepB=13mm]{->}{B}{C1}
\ncline[offset=1cm,nodesepA=10mm,nodesepB=13mm]{->}{B}{C2}
\ncline[offset=1cm,nodesepA=2mm,nodesepB=13mm]{->}{C1}{D}

\end{document}

在此处输入图片描述

相关内容