我想定义一个命令来绘制下图所示类型的模式,但分支数量不确定。输入始终为正整数。
右侧分支的编号是使用前两个参数根据左侧相应编号获得的,具体函数为:#1/{2*#2*\entry},其中 \entry 是左侧的变量编号。图中的示意图应可通过运行以下命令获得:st{252}{1}{1,2,3,6,7,9}
Latex编辑器返回的错误如下:“扫描 @argdef 的使用情况时文件结束。”
这是我一直在测试的代码:
% #1 = v, #2 = lambda_i, #3 = variable argument {a_1, a_2, ...}
\newcommand{\st}[3]{%
\newcounter{ni} % crea un contador
\setcounter{ni}{2} % inicializa el contador ni en 2
\draw (0 , 0) -- (0, 8);
\draw [fill=gray!50] (0 , 0) circle(1.6);
\draw (0, 0) node {{\large \textbf{2}}};
\foreach \superscript/\entry in {#3} {%
\draw (-\entry , 2*\entry) -- (\entry, 2*\entry);
\draw [fill=black] (0, 2*\entry) circle(.2);
\draw [fill=white] (\entry , 2*\entry) circle(1);
\draw [fill=white] (-\entry, 2*\entry) circle(1);
\draw (-ni, 2*ni) node {\entry};
\draw (ni, 2*ni) node {#1/{2*#2*\entry}};
\addtocounter{ni}{1} % incremento el contador ni en 1
}%
{
很抱歉,我忘记了 Tikz 的页眉和页脚,但我收到的错误仍然相同。
这是新代码:
% #1 = v, #2 = lambda_i, #3 = variable argument {a_1, a_2, ...}
\newcommand{\st}[3]{%
\newcounter{\ni} % this creates a counter
\setcounter{\ni}{2} % this initializes the \ni counter to 2
\begin{tikzpicture}[scale=.5, line width=1pt]
\draw (0 , 0) -- (0, 8);
\draw [fill=gray!50] (0 , 0) circle(1.6);
\draw (0, 0) node {{\large \textbf{2}}};
\foreach \superscript/\entry in {#3} {%
\draw (-\entry , 2*\entry) -- (\entry, 2*\entry);
\draw [fill=black] (0, 2*\entry) circle(.2);
\draw [fill=white] (\entry , 2*\entry) circle(1);
\draw [fill=white] (-\entry, 2*\entry) circle(1);
\draw (-\ni, 2*\ni) node {\entry};
\draw (\ni, 2*\ni) node {#1/{2*#2*\entry}};
\end{tikzpicture}
\addtocounter{\ni}{1} % increments the counter \ni by 1
}%
}
我附上的这个图是使用以下代码得到的,作为示例:
\begin{center}
\begin{tikzpicture}[scale=.5, line width=1pt]
\def\x{10};
\draw (0 , 0) -- (0, 14);
\draw [fill=gray!50] (0 , 0) circle(1.6);
\draw (0, 0) node {{\large \textbf{1}}};
\foreach \x in {2,...,7}
{
\draw (-\x , 2*\x) -- (\x, 2*\x);
\draw [fill=black] (0, 2*\x) circle(.2);
\draw [fill=white] (\x , 2*\x) circle(1);
\draw [fill=white] (-\x, 2*\x) circle(1);
}
\draw (-2, 4) node {9};
\draw (2, 4) node {14};
\draw (-3, 6) node {7};
\draw (3, 6) node {18};
\draw (-4, 8) node {6};
\draw (4, 8) node {21};
\draw (-5, 10) node {3};
\draw (5, 10) node {42};
\draw (-6, 12) node {2};
\draw (6, 12) node {63};
\draw (-7, 14) node {1};
\draw (7, 14) node {126};
\end{tikzpicture}
\end{center}
答案1
您有几个语法错误。特别是,\foreach \superscript/\entry
要求列表采用以下格式a/b,c/d
等等。
并且最好按照列表从下往上进行。
\documentclass{article}
\usepackage{tikz}
\newcommand{\st}[3]{%
\begin{tikzpicture}[scale=.5, line width=1pt]
\draw (0 , 0) -- (0, 2*dim{{#3}}+2);
\draw [fill=gray!50] (0 , 0) circle(1.6);
\draw (0, 0) node {{\large \textbf{#2}}};
\foreach \superscript [count=\ni from 2] in {#3} {%
\draw (-\ni, 2*\ni) -- (\ni, 2*\ni);
\draw [fill=black] (0, 2*\ni) circle(.2);
\draw [fill=white] (\ni , 2*\ni) circle(1);
\draw [fill=white] (-\ni, 2*\ni) circle(1);
\draw (-\ni, 2*\ni) node {\superscript};
\draw (\ni, 2*\ni) node {\the\numexpr#1/(2*#2)/\superscript};}
\end{tikzpicture}
}
\begin{document}
\st{252}{1}{9,7,6,3,2,1}
\end{document}