我想使用 Tikz 创建类似这样的绘图:
我找到了一个很好的 tikz 示例,它创建了类似的东西:
\documentclass[border=10pt]{standalone}%
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \n in {0,...,4} {
\foreach \k in {0,...,\n} {
\node at (\k-\n/2,-\n) {${\n \choose \k}$};
}
}
\end{tikzpicture}
\end{document}
我以为通过访问列表上的标签来更改节点上的标签很简单,例如lst[\n]
,其中lst
包含单项式,但结果比我想象的要复杂得多。此外,我找不到以编程方式添加指数的方法。使用循环的 tikz 可以做到这一点吗,还是我必须创建一个临时解决方案?
答案1
扩展你的例子:
\documentclass[border=10pt]{standalone}%
\usepackage{ifthen}
\usepackage{tikz}
\usetikzlibrary{math}
\begin{document}
\newcommand{\exponent}[2]{\ifthenelse{#2=0}{}{\ifthenelse{#2=1}{$#1$}{$#1^{#2}$}}}
\begin{tikzpicture}
\foreach \x in {0,...,7} {
\foreach \n in {0,...,\x} {
\pgfmathparse{int(subtract(\x,\n))}
\edef\xexp{\pgfmathresult}
\edef\nexp{\n}
\node[inner sep = 8pt] (X\xexp-N\nexp) at (2*\n-\x,-\x) {\ifthenelse{\xexp=0 \AND \nexp=0}{$1$}{\exponent{\xi}{\xexp}\exponent{\eta}{\nexp}}};
\node[inner sep = 16pt] (dX\xexp-N\nexp) at (2*\n-\x,-\x) {}; % d: dummy
}
}
\draw[blue, thick, dashed] (X0-N0.north) -- (X1-N0.west) -- (X1-N1.south) -- (X0-N1.east) node[inner sep = 0] (S1) {} -- cycle;
\draw[red, thick, dashed] (dX0-N0.north) -- (dX4-N0.west) -- (dX4-N1.south) -- (dX3-N1.south) --
(dX1-N3.south) -- (dX1-N4.south) -- (dX0-N4.east) node[inner sep = 0] (S4) {} -- cycle;
\node[right = 0.5cm, above = 0.5cm] (S1t) at (S1.west) {$S_1(\Omega_{\rm st}^{(q)})$};
\draw[blue] (S1t) edge[thick, out=270, in=90, ->] (S1);
\node[right = 0.5cm, above = 0.5cm] (S4t) at (S4.west) {$S_4(\Omega_{\rm st}^{(q)})$};
\draw[red] (S4t) edge[thick, out=270, in=90, ->] (S4);
\end{tikzpicture}
\end{document}