当图形元素某些可重现时,在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

点赞(2)

评论列表 共有 0 条评论

暂无评论
立即
投稿

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部