如何使用 foreach 命令在 TikZ 中创建点三角形?
我需要这个。
答案1
您可以使用该选项[evaluate = <variable> as <macro> using <expression>]
根据计数器计算新值,然后可在内部计数器中使用该值:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach [evaluate = \y as \n using \y*2-1] \y in {1,...,5} {
\foreach \x in {1,...,\n} {
\fill [blue] (\x-\n/2,-\y) circle [radius=3pt];
}
}
\end{tikzpicture}
\end{document}
答案2
尊重 OP 问题(带有 Tikz 的点三角形)的最简单的解决方案似乎是:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \y in {0,...,4}
\foreach \x in {-\y,...,\y}
\fill [blue] (\x,-\y) circle [radius=0.2];
\end{tikzpicture}
\end{document}
答案3
方法 1(循环)
用户定义常量:
\FPset{H}{5}% Height
\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pstricks-add}
\usepackage[nomessages]{fp}
% user defined constants
\FPset{H}{5}% Height
% internal used constants
\FPeval{Size}{H-1}
\begin{document}
\begin{pspicture}(-\Size,-\Size)(\Size,0)
\multido{\iY=0+1}{\H}{%
\FPeval{N}{round(2*\iY{}+1:0)}%
\multirput(-\iY,-\iY)(1,0){\N}{\pscircle*[linecolor=blue](0,0){5pt}}}
\end{pspicture}
\end{document}
方法 2(使用 Ti 循环钾Z 为每个)
\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pstricks-add,tikz}
\usepackage[nomessages]{fp}
% user defined constants
\FPset{H}{5}% Height
% internal used constants
\FPeval{Size}{H-1}
\begin{document}
\begin{pspicture}(-\Size,-\Size)(\Size,0)
\foreach \y in {0,...,\H}
\foreach \x in {-\y,...,\y}
{\pscircle*[linecolor=blue](\x,-\y){5pt}}
\end{pspicture}
\end{document}
方法 3(剪辑和循环)
用户定义数据:
\FPset{N}{6}
\documentclass[border=-0.5cm,pstricks]{standalone}%a negative length is set to border to trim the unwanted white spaces!
\usepackage{pstricks-add,fp}
% user defined data
\FPset{N}{6}
% internal used data
\FPeval{H}{round(N-1:0)}
\FPeval{W}{round(2*H+1:0)}
\pstVerb{/Left {\N\space 0.25 2 sqrt mul \H\space add neg} def}
\begin{document}
\begin{pspicture}(-\N,-\N)(\N,1)
\psclip{\pspolygon[linestyle=none](0,0.5)(!Left)(!Left 2 1 roll neg exch)}
\multirput(0,0)(0,-1){\N}{\multirput(-\H,0)(1,0){\W}{\pscircle*(0,0){5pt}}}
\endpsclip
\end{pspicture}
\end{document}
答案4
杰克的回答非常简洁,我很高兴了解评估,但你不需要它。下面使用更基本的代码来实现这个技巧:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\foreach \y in {0,...,4} {%
\foreach \x in {0,...,\y} {%
\fill [blue] (\x,-\y) circle [radius=3pt];
\fill [blue] (-\x,-\y) circle [radius=3pt];
}
}
\end{tikzpicture}
\end{document}