當圖形元素某些可重現時,在LaTeX中合理使用循環和判斷選擇語句進行繪圖,會非常省事. 下面給個的例子:

\documentclass{ctexbook}
\usepackage{tikz,ifthen}
\usetikzlibrary{calc}
\begin{document}
% %------------------------------------------------------
由 $4$ 人可構成$2^6 = 64$張人際關系圖, 其中的 $8$ 張圖:

\newcounter{maincounter}
\newcounter{secondcounter}
\setcounter{maincounter}{0}%設置計數器的值
% %------------------------------------------------------
 \begin{tikzpicture}[line width = 1.2pt]
 \pgfmathsetmacro{\h}{1.4} %兩點的水平間距
 \pgfmathsetmacro{\v}{1.4} %兩點的垂直間距
% %------------------------------------------------------
\foreach \r/\c in {0/0,2/0,4/0,6/0,0/2,2/2,4/2,6/2} {
 \stepcounter{maincounter} %增加計數器的值
 \setcounter{secondcounter}{0}%設置計數器的值
 %定義原點坐標
 \coordinate (P0) at (\r,\c);

 \path (P0) node[circle,fill=red,inner sep=2pt](a){$a$};
 \path (a)+(\h,0 ) node[circle,fill=red,inner sep=2pt](b){$b$};
 \path (b)+(0,\v ) node[circle,fill=red,inner sep=2pt](c){$c$};
 \path (a)+(0,\v ) node[circle,fill=red,inner sep=2pt](d){$d$};
 \path ($ (a)!0.5! (b) $)+(0,-0.25) node (T){ $\left( \themaincounter \right) $};
 %畫虛線
 \foreach \from/\to in {a/b,b/c,c/d,d/a,a/c,b/d}
 {
 \stepcounter{secondcounter} %增加計數器的值
 \ifthenelse{\value{maincounter} < 8}{
 \ifthenelse{\value{secondcounter} < \value{maincounter}}
 {\draw[blue,very thick] (\from)--(\to) ; } {\draw[dashed,thin] (\from)--(\to) ;}
 }{\draw[dashed,thin] (\from)--(\to) ;}
 %------------------------------
 }
 %------------------------------
 \ifthenelse{\value{maincounter} = 8}{\draw[green,very thick] (a)--(b)--(c)--(a) ; }{}
}
\end{tikzpicture}

 \end{document}
tikz20150726152312選自:http://blog.csdn.net/rumswell/article/details/38122193