This solution allows you to specify the number of columns and rows with its mandatory argument.
The environments are uppertriangle and lowertriangle.
Their optional argment will be forwarded to the internal \matrix.
The \\ – the \pgfmatrixendrow macro actually – will be patched in a way that you don't have to add empty cells at the start or the end of the row. Empty cells will be inserted on both ends automatically.
The matrix is setup in a way with execute at empty cell, execute at begin cell and execute at end cell that only the actual cells have a possible empty node in them. These use the macros that also matrix of nodes use so you should be able to use |[<options]| as usual in a node.
Don't use matrix of math nodes, though, this will be in conflict with triangle matrix/triangle setup. Just use
math nodes/.style={
nodes={
execute at begin node=$,%
execute at end node=$%
}
}
My ext.node-families library helps make all nodes in one column have the same width. There are simpler approach if you know the cell content beforehand. (Of course, we can also draw none of the nodes and loop over them afterwards and figure out where to draw the lines then.)
The style matrix like tabular sets up the nodes similar on how cells of a tabular or array would have been setup. Except that instead of \strut the height and depth of the node is set to the dimension of \strut but they don't expand if the contents are higher or deeper.
This highly depends on what the contents of your matrix will be.
If you can't predict it and it may be more complex you might need to set the families node family/text height and node family/text depth as well (as I did in a previous version of this answer).
The cell color = {<list of cells>}{<color>} sets all cells in its first argument to use <color> as their fill.
This key is a specialized version of the solutions provided by another answer of mine.
Remember that in a PGF/TikZ matrix, the & is made active and isn't the usual alignment character. If you use this as part of an argument of macro, you're going to need to use ampersand replacement.
Sneak peek
The output is the result of
\begin{uppertriangle}[cell color={1-2, 1-3, 2-3, 2-4, 4-4}{yellow!60}]{4}
& 1 & 1 \\
& 1 & 1 \\
& 1
\end{uppertriangle}
\begin{lowertriangle}{10}
\end{lowertriangle}
Output

Code
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,ext.node-families}
\makeatletter
\def\pgfutil@addto@macro#1#2{%
\expandafter\def\expandafter#1\expandafter{#1#2}}
\tikzset{
execute before new row/.style={% \preto
/utils/tempa/.initial/.expand once=\pgfmatrixendrow,
/utils/tempa/.prefix={#1}, /utils/tempa/.get=\pgfmatrixendrow},
execute after new row/.code={% \appto
\pgfutil@addto@macro\pgf@matrix@no@eom@found{#1}},
triangle matrix/triangle setup/.style={
execute before new row={
\let\tikz@temp\pgfutil@empty \c@pgf@counta=\pgfmatrixcurrentcolumn
\pgfutil@loop\ifnum\c@pgf@counta<\tikzmatrixrows\relax
\pgfutil@addto@macro\tikz@temp\pgfmatrixnextcell
\advance\c@pgf@counta1 \pgfutil@repeat\tikz@temp},
nodes in empty cells,
execute at empty cell={%
\ifnum\pgfmatrixcurrentrow#1\pgfmatrixcurrentcolumn\else
\expandafter\tikz@lib@matrix@empty@cell\fi},
execute at begin cell={%
\ifnum\pgfmatrixcurrentrow#1\pgfmatrixcurrentcolumn\else
\expandafter\tikz@lib@matrix@start@cell\fi},
execute at end cell={%
\ifnum\pgfmatrixcurrentrow#1\pgfmatrixcurrentcolumn\else
\expandafter\tikz@lib@matrix@end@cell\fi}},
triangle matrix/upper triangle/.style={
triangle matrix/triangle setup={>},
execute after new row={%
\let\tikz@temp\pgfutil@empty \c@pgf@counta=\pgfmatrixcurrentrow
\pgfutil@loop\ifnum\c@pgf@counta>1
\pgfutil@addto@macro\tikz@temp\pgfmatrixnextcell
\advance\c@pgf@counta-1 \pgfutil@repeat\tikz@temp}},
triangle matrix/lower triangle/.style={triangle matrix/triangle setup={<}},
triangle matrix/remaining rows/.initial={%
\let\tikz@temp\pgfutil@empty
\c@pgf@counta=\pgfmatrixcurrentrow\relax
\pgfutil@loop
\ifnum\c@pgf@counta<\tikzmatrixrows\relax
\pgfutil@addto@macro\tikz@temp\pgfmatrixendrow
\advance\c@pgf@counta1
\pgfutil@repeat
\tikz@temp}}
\makeatother
\tikzset{
matrix like tabular/.style={
/pgf/inner xsep=+\tabcolsep, /pgf/inner ysep=+0pt,
/tikz/text height=+.7\baselineskip,% .7 and .3 → basically \strutbox
/tikz/text depth=+.3\baselineskip},% or font=\strut
%
% specialized version of https://tex.stackexchange.com/a/660100
cell color/.style 2 args={
/utils/tempa/.style args={##1-##2}{row ##1 column ##2/.append style={nodes={fill=#2}}},
/utils/tempa/.list={#1}},
%
triangle matrix/picture/.style={
baseline=(current bounding box.center),% triangle matrix-1-1.base
/utils/exec=\pgfmathtruncatemacro\tikzmatrixrows{#1},
matrix like tabular},
triangle matrix/matrix/.style={
name=triangle matrix, every outer matrix/.append style={/pgf/inner sep=+0pt},
column sep=+-\pgflinewidth, row sep=+-\pgflinewidth,
nodes={draw, node family={
prefix/.append=\tikzmatrixname,
width=col\the\pgfmatrixcurrentcolumn}}}}
\newenvironment{uppertriangle}[2][]{%
\tikzpicture[triangle matrix/picture/.evaluated={int(#2)}, triangle matrix/upper triangle]
\matrix[triangle matrix/matrix,#1]\bgroup
}{%
\pgfkeysvalueof{/tikz/triangle matrix/remaining rows}%
\pgfmatrixendrow\egroup;
\endtikzpicture}
\newenvironment{lowertriangle}[2][]{%
\tikzpicture[triangle matrix/picture/.evaluated={int(#2)}, triangle matrix/lower triangle]
\matrix[triangle matrix/matrix,#1]\bgroup
}{%
\pgfkeysvalueof{/tikz/triangle matrix/remaining rows}%
\pgfmatrixendrow\egroup;
\endtikzpicture}
\begin{document}
\begin{uppertriangle}[cell color={1-2, 1-3, 2-3, 2-4, 4-4}{yellow!60}]{4}
& 1 & 1 \
& 1 & 1 \
& 1
\end{uppertriangle}
\begin{lowertriangle}{10}
\end{lowertriangle}
\end{document}
columns-width=auto. – F. Pantigny Oct 08 '22 at 18:00&even if the cell is empty? Just by using&and\\you already specify how many rows and cells you need. – Qrrbrbirlbel Oct 08 '22 at 20:11