Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to create a pyramid hierarchy in tikz with smooth sides (unlike this step-pyramid) and place text in each level. Shown below is how I am currently doing this, is there a better way?

\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}

enter image description here

share|improve this question
    
Not a duplicate (but a TikZ answer on a PSTricks question): Draw a triangle with horizontal lines with PSTricks –  Qrrbrbirlbel Apr 24 '13 at 16:54
add comment

2 Answers

up vote 5 down vote accepted

Here is a simplification using the tikz intersections library to cut down on brute-force calculation:

\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}

Notice the combination of level numbers and labels as in Gonzalo's comment.

sample code output

Here is another solution using the tikz calculations library:

\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};
}

The coordinate ($(A)!\y/7!(C)$) is \y/7 of the way between the coordinates A and C. The advantage here is total DRY: Change any of the coordinates A, B, or C, and the pyramid will transform accordingly.

\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}

sample code output

share|improve this answer
add comment

With PSTricks.

Single:

enter image description here

\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}

Multiple:

enter image description here

\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}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.