4

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

4
  • Are, please, you able to get a code? If your answer it is yes, could you add here? Thank you for your collaboration.
    – Sebastiano
    Nov 30 '21 at 12:33
  • So, what you mean is that you would like to have an input such as a \\ a & b \\ a & b & c \\ etc. and this should render as a triangular matrix? Nov 30 '21 at 13:16
  • Here is another solution that you might find useful, but only for smaller triangles: tex.stackexchange.com/q/142132/224487 . For larger triangles, I think the scripts provided in dalibor.zeleny's question would be more appropriate. Link to their question: tex.stackexchange.com/q/17522/224487 Nov 30 '21 at 13:38
  • How would such a matrix look like when exported from SageMath? Just like a comma separated list, or differently? Nov 30 '21 at 13:55
7

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

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.