使用 tabularx 创建数字金字塔

使用 tabularx 创建数字金字塔

我正在尝试创建一个简单的数字金字塔,例如这个使用具有六列的 tabularx 表和“\multicolumn”命令。

我试过

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

..这给了我一个非常扭曲的表格。

但是如果我在表中添加一个简单的空行

&&&&&&

一切看起来都很完美。我现在唯一的问题是表格太长了,因为有一行额外的空行。

有没有办法不添加额外的线条就能得到漂亮的金字塔?

答案1

通过一些低级编程:

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

在此处输入图片描述

答案2

绘制这个数字金字塔比将其写成表格要简单得多。特别是如果你喜欢使用tabularx表格环境。通过使用该tikz包你可以获得:

在此处输入图片描述

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

附录: 使用这个chains库可以实现更短的代码:

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

结果和以前一样。

答案3

这真的只是为了好玩。通过使用 定义的递归绘制金字塔tikzmath。如果你想\nmax进一步增加,你需要让列表\LstNum更长。当然,你可以在这个列表中输入你喜欢的任何数字。

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

在此处输入图片描述

相关内容