我想在 tikz 中创建一个金字塔层次结构,其侧面光滑(不同于这个阶梯金字塔) 并在每个级别中放置文本。下面显示的是我目前的做法,有没有更好的方法?
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{figure}[!h]
\centering
\begin{tikzpicture}[scale=0.73]
\def \h {9};
\def \f {.7};
\foreach \y in {0,1,2,3,4,5,6} {
\def \w { \h*\f-\y*\f };
\def \v { \y*\f-\h*\f };
\draw (\v,\y) -- (\w,\y);
}
\draw (-\h*\f,0) -- (0,\h);
\draw (\h*\f,0) -- (0,\h);
\node at (0,0) [above] {G};
\node at (0,1) [above] {F};
\node at (0,2) [above] {E};
\node at (0,3) [above] {D};
\node at (0,4) [above] {C};
\node at (0,5) [above] {B};
\node at (0,6) [above] {A};
\end{tikzpicture}
\end{figure}
\end{document}
答案1
这里使用 tikz 交叉库进行了简化,以减少强力计算:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (-5,0) {};
\coordinate (B) at ( 5,0) {};
\coordinate (C) at (0,7) {};
\draw[name path=AC] (A) -- (C);
\draw[name path=BC] (B) -- (C);
\foreach \y/\A in {0/G,1/F,2/E,3/D,4/C,5/B,6/A} {
\path[name path=horiz] (A|-0,\y) -- (B|-0,\y);
\draw[name intersections={of=AC and horiz,by=P},
name intersections={of=BC and horiz,by=Q}] (P) -- (Q)
node[midway,above] {\A};
}
\end{tikzpicture}
\end{document}
请注意 Gonzalo 评论中提到的级别编号和标签的组合。
以下是使用 tikz 计算库的另一种解决方案:
\foreach \y/\A in {0/G,1/F,2/E,3/D,4/C,5/B,6/A} {
\draw ($(A)!\y/7!(C)$) -- ($(B)!\y/7!(C)$)
node[midway,above] {\A};
}
坐标($(A)!\y/7!(C)$)
位于和\y/7
之间。这里的优点是完全 DRY:更改 、 或 中的任何一个坐标,金字塔就会相应地变换。A
C
A
B
C
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (-5,0) {};
\coordinate (B) at ( 5,1) {};
\coordinate (C) at (0,3) {};
\draw (A) -- (C);
\draw (B) -- (C);
\foreach \y/\A in {0/G,1/F,2/E,3/D,4/C,5/B,6/A} {
\draw ($(A)!\y/7!(C)$) -- ($(B)!\y/7!(C)$) node[midway,above] {\A};
}
\end{tikzpicture}
\end{document}
答案2
使用 PSTricks。
单身的:
\documentclass[pstricks,border=1pt]{standalone}
\usepackage{multido}
\SpecialCoor
\def\Pyramid#1{%
\begin{pspicture}(#1,-#1)
\multido{\i=1+1}{#1}{%
\pstriangle(!#1 2 div \i\space neg)(\i,\i)
\uput[90](!#1 2 div \i\space neg){\char\numexpr\i+64}}
\end{pspicture}}
\begin{document}
\Pyramid{6}
\end{document}
多种的:
\documentclass[multi,border=1pt]{standalone}
\usepackage{pstricks}
\usepackage{multido}
\SpecialCoor
\def\Pyramid#1{%
\begin{pspicture}(#1,-#1)
\multido{\i=1+1}{#1}{%
\pstriangle(!#1 2 div \i\space neg)(\i,\i)
\uput[90](!#1 2 div \i\space neg){\char\numexpr\i+64}}
\end{pspicture}}
\begin{document}
\multido{\in=1+1}{9}{\Pyramid{\in}\ }
\end{document}