4

I am trying to create a simple number pyramid like this one using a tabularx table with six columns and `\multicolumn' commands.

I tried

\begin{tabularx}{0.5\linewidth}{XXXXXX}
        \cline{3-4}
        \multicolumn{2}{c}{}&\multicolumn{2}{|c|}{}&\multicolumn{2}{c}{}\\
        \cline{2-5}
        \multicolumn{1}{c}{}&\multicolumn{2}{|c}{}&\multicolumn{2}{|c|}{}&\multicolumn{1}{c}{}\\
        \hline
        \multicolumn{2}{|c|}{$\frac{3}{2}$}&\multicolumn{2}{c}{$\frac{1}{3}$}&\multicolumn{2}{|c|}{$\frac{1}{2}$}\\
        \hline
 \end{tabularx}

.. which gives me a very distorted table.

However if I add a simple empty line to the table with

&&&&&&

everything looks perfect. My only issue now is that the table is too long, because of that extra empty line.

Is there a way to get a pretty pyramid without adding that extra line?

6

With some low level programming:

\documentclass{article}
\usepackage{amsmath}
\usepackage{varwidth}

\newcommand{\block}[1]{%
  \begingroup
  \setlength{\fboxsep}{0pt}%
  \vrule width0pt height \blockdim
  \ooalign{%
    \framebox[\blockdim]{\rule{0pt}{\blockdim}}\cr
    \hidewidth\raisebox{0.5\dimexpr\blockdim-\height}{\raisebox{\depth}{#1}}\hidewidth\cr
  }%
  \endgroup
}
\newcommand{\joinblocks}{\unskip\kern-\fboxrule\ignorespaces}
\newenvironment{blocks}[1][1cm]
 {\begin{varwidth}{\textwidth}\setlength{\blockdim}{#1}\makeblocks}
 {\end{varwidth}}
\newcommand{\makeblocks}{%
  \begingroup\lccode`~=`&\lowercase{\endgroup\let~}\joinblocks
  \catcode`\&=\active
  \baselineskip=0pt
  \lineskiplimit=\maxdimen
  \lineskip=0pt
  \centering
}
\newlength{\blockdim}

\begin{document}

A pyramid
\begin{blocks}
  \block{1}\\
  \block{2} & \block{3} \\
  \block{4} & \block{5} & \block{$\dfrac{36}{6}$}
\end{blocks}
in context

\bigskip

A pyramid
\begin{blocks}[1.5cm]
  \block{1}\\
  \block{2} & \block{3} \\
  \block{4} & \block{5} & \block{$\dfrac{36}{6}$}
\end{blocks}
in context

\end{document}

enter image description here

5

drawing this number pyramid is much simpler than write it as table. especially if you like to use tabularx table environment. by use of the tikz package you can obtain:

enter image description here

\documentclass[tikz, border=3mm]{standalone}
\usetikzlibrary{positioning}

\begin{document}
    \begin{tikzpicture}[
    node distance = 0pt,
every node/.style = {draw, minimum size=7mm, inner sep=0pt, outer sep=0pt}
                        ]
\node (n1)  {};
%
\node (n11) [below left=of n1.south]    {};
\node (n12) [right=of n11]              {};
%
\node (n21) [below left=of n11.south]   {$\frac{1}{3}$};
\node (n22) [right=of n21]              {$\frac{1}{3}$};
\node (n23) [right=of n22]              {$\frac{1}{2}$};
    \end{tikzpicture}
\end{document}

addendum: with use of the chains library it is possible to achieve a little bit shorter code:

\documentclass[tikz, border=3mm]{standalone}
\usetikzlibrary{chains, positioning}

\begin{document}
    \begin{tikzpicture}[
    node distance = 0pt,
      start chain = going right,
every node/.style = {draw, minimum size=7mm, inner sep=0pt, outer sep=0pt,
                     on chain}
                        ]
\node (n1)  {};
%
\node (n11) [below left=of n1.south]    {};
\node       {};
%
\node (n21) [below left=of n11.south]   {$\frac{1}{3}$};
\node       {$\frac{1}{3}$};
\node       {$\frac{1}{2}$};
    \end{tikzpicture}
\end{document}

result is the same as before.

2

This is really just for fun. Draw the pyramid via a recursion defined using tikzmath. If you want to increase \nmax even further, you need to make the list \LstNum longer. Of course, you can put in this list whatever numbers you like.

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{math}
\begin{document}
\def\LstNum{{1,2,4,8,16}}
\begin{tikzpicture}[scale=0.6,evaluate={
        function pyramid(\n, \q) {
    if \n == 1 then {
      return chi(\q);
    } else {
      return int(pyramid(\n-1, \q) + pyramid(\n-1, \q+1));
    };
  };},
declare function={chi(\i)=\LstNum[int(\i-1)];}]
\begin{scope}
\def\nmax{3}
\foreach \Z [evaluate=\Z as \Xmax using {int(1+\nmax-\Z)}]in {1,...,\nmax}
{\foreach \X in {1,...,\Xmax}
\pgfmathsetmacro{\myp}{pyramid(\Z,\X)}
\node[draw,minimum size=0.6cm] at (\X+\Z/2,\Z) {\myp};}
\node at (2.5,-0.6) {$n_\mathrm{max}=3$};
\end{scope}
\begin{scope}[xshift=4cm]
\def\nmax{4}
\foreach \Z [evaluate=\Z as \Xmax using {int(1+\nmax-\Z)}]in {1,...,\nmax}
{\foreach \X in {1,...,\Xmax}
\pgfmathsetmacro{\myp}{pyramid(\Z,\X)}
\node[draw,minimum size=0.6cm] at (\X+\Z/2,\Z) {\myp};}
\node at (3,-0.6) {$n_\mathrm{max}=4$};
\end{scope}
\begin{scope}[xshift=9cm]
\def\nmax{5}
\foreach \Z [evaluate=\Z as \Xmax using {int(1+\nmax-\Z)}]in {1,...,\nmax}
{\foreach \X in {1,...,\Xmax}
\pgfmathsetmacro{\myp}{pyramid(\Z,\X)}
\node[draw,minimum size=0.6cm] at (\X+\Z/2,\Z) {\myp};}
\node at (3.5,-0.6) {$n_\mathrm{max}=5$};
\end{scope}
\end{tikzpicture}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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