Matrix to triangle

matricestikz-pgf

Is there a simple way to represent a triangular matrix as a triangle, like Pascal's triangle?

I've seen various solutions that display Pascal's triangle, but I think that there should be a straight-forward command to display any general triangular matrix in a similar way from a LaTeX or from a SageMath input matrix.

As an example was requested, let me paste here dalibor.zeleny's solution from one of the links:

\begin{tikzpicture}
\foreach \n in {0,...,4} {
  \foreach \k in {0,...,\n} {
    \node at (\k-\n/2,-\n) {${\n \choose \k}$};
  }
}
\end{tikzpicture}

This produces: enter image description here

Best Answer

As noted in the comments, there are loads of solutions for Pascal's triangles on this site already. However, I understand your question rather that you want to have something where you can input an arbitrary list of entries and these will then arrange according to a triangular matrix automatically.

The following would do just this (still based in TikZ):

\documentclass{article}
\usepackage{tikz}

\newcounter{tmline}
\newcounter{tmposn}
\newcommand{\tmatrix}[2][]{%
    \begin{tikzpicture}[#1]
        \setcounter{tmline}{1}
        \setcounter{tmposn}{0}
        \foreach \k in {#2} {
            \stepcounter{tmposn}
            \node[anchor=base] at (\thetmposn-\thetmline/2,-\thetmline) {${\k}$};
            \ifnum\thetmposn=\thetmline\stepcounter{tmline}\setcounter{tmposn}{0}\fi
        }
    \end{tikzpicture}%
}

\begin{document}

    \tmatrix{1,2,3,4,5,6}

    \bigskip

    \tmatrix[y=.75cm, x=1.25cm]{1,2,3,4,5,6,a,b,2x+4y,\sqrt{2},\frac{3}{4},\sum^n_{i=0}{x^i},\textrm{out},\textrm{of},\textrm{ideas}}

\end{document}

enter image description here

Related Question