我正在尝试编写一个宏来绘制一些图形,但由于每个节点的内容都是特定的,因此必须以某种方式将其作为列表传递给宏。
下面是我所期望的临时代码的一个例子(参见 tikzpicture 开头的部分)并查看相应的宏(但它不包含用于传递应在每个节点中打印的信息列表的第二个参数)。
\documentclass{book}
\usepackage{calc}
\usepackage[tikz]{bclogo}
\begin{document}
\newcounter{local}
\def\graphcircuit#1{
\setcounter{local}{#1 - 1}
\begin{scope}
\foreach \i in {1,...,#1}
\node [circle, draw] (n-\i) at (\i*360/#1-360/#1:#1*0.2) {\scriptsize$\i$};
\foreach \i [evaluate={\j=int(\i+1);}] in {1,...,\value{local}}
\draw[>=stealth,semithick,->] (n-\i) to[bend angle=18,bend right] (n-\j);
\draw[>=stealth,semithick,->] (n-#1) to[bend angle=18,bend right] (n-1);
\end{scope}
}
\begin{tikzpicture}
\foreach \i/\v in {1/{1:5},2/{7:2},3/{3:0},4/{4:0},5/{9:1}}
\node [circle, draw] (n-\i) at (\i*360/5-360/5:5*0.2) {\scriptsize$\v$};
\foreach \i [evaluate={\j=int(\i+1);}] in {1,...,4}
\draw[>=stealth,semithick,->] (n-\i) to[bend angle=18,bend right] (n-\j);
\draw[>=stealth,semithick,->] (n-5) to[bend angle=18,bend right] (n-1);
\end{tikzpicture}
\end{document}
答案1
如果我理解你的问题,这里有一个宏,可以根据你的一组节点值绘制你的圆形图。
\documentclass[varwidth,border=50]{standalone}
\usepackage{tikz}
\newcommand{\graphcircuit}[1]{
\foreach[count=\n] \v in {#1}; % count the number of elements
\pgfmathsetmacro{\r}{\n*0.2} % set the node distance from (0,0)
\pgfmathsetmacro{\b}{90/\n} % evaluate the bend angle
\foreach[count=\i, evaluate=\i as \a using (\i-1)*360/\n] \v in {#1}
\node [circle, draw, font=\scriptsize] (n-\i) at (\a:\r) {$\v$};
\foreach \i [remember=\i as \j (initially \n)] in {1,...,\n}
\draw[semithick,-stealth] (n-\j) to[bend right=\b] (n-\i);
}
\begin{document}
\begin{tikzpicture}
\graphcircuit{1:5,7:2,3:0,4:0,9:1}
\scoped[every node/.style={fill=yellow}]
\graphcircuit{a,...,l};
\end{tikzpicture}
\end{document}
注意:我对你的 tikz 代码做了一些调整。
编辑:如果您接受使用 LuaLaTeX,您可以执行以下操作来绘制圆形图:
\documentclass[varwidth,border=50]{standalone}
\usepackage{tikz}
\usetikzlibrary{graphs,graphdrawing}
\usegdlibrary{circular}
\begin{document}
\tikz \graph [simple necklace layout, node distance=0cm, nodes={circle,draw}]
{ 1:5->7:2->3:0->4:0->9:1->1:5};
\end{document}