[Tex/LaTex] TikZ: Symmetric matrix without upper diagonal elements

tikz-pgf

I want to make a symmetric matrix in TikZ and don't want to show the upper diagonal elements.

\documentclass[tikz]{standalone}
\usetikzlibrary{matrix, positioning, calc}
\usetikzlibrary{shapes.geometric, fit}

\begin{document}

\begin{tikzpicture}[baseline=(U.center)]
\matrix [matrix of math nodes, left delimiter=[ ,right delimiter={]},  nodes={outer sep=1pt}] (U) { 
 0  &   \textcolor{white}{9} & \textcolor{white}{3} & \textcolor{white}{6} & \textcolor{white}{11}   \\[0.1cm]
 9  &   0 & \textcolor{white}{11} & \textcolor{white}{11} & \textcolor{white}{11}   \\[0.1cm]
 3  &   7 & 0 & \textcolor{white}{11} &  \textcolor{white}{11}   \\[0.1cm]
 6  &   5 & 9 & 0 &   \textcolor{white}{11}   \\[0.1cm]
11 & 10 & 2 & 8 &   0   \\[0.1cm]
 };

\foreach \x in {1,...,5}{
 \node[black,  left = 3 mm of U-\x-1] (U-\x-6) {$\x$};
 };

\foreach \y in {1,...,5}{
 \node[black, above= 1 mm of U-1-\y] (U-6-\y)  {$\y$};
 };

\draw[red] ([shift={(4pt, 2pt)}]U-1-1.north west) -- ([shift={(5pt,2pt)}]U-5-5.south east);
\draw[red] ([shift={(-5pt, -2pt)}]U-1-1.north west) -- ([shift={(-4pt,-2pt)}]U-5-5.south east);

\draw[red] ([shift={(5pt, 2pt)}]U-1-1.north west) -- ([shift={(-5pt,-2pt)}]U-1-1.north west);
\draw[red] ([shift={(5pt, 2pt)}]U-5-5.south east) -- ([shift={(-5pt,-2pt)}]U-5-5.south east);

\node [draw, circle, red] at (U-5-3) {};

\node[black,  left = 6 mm of U] {$\mathbf{D}=\left\{ d_{ij}\right\}=$};

\end{tikzpicture}

\end{document}

enter image description here

I wonder how to get the blank upper diagonal elements without using the white text color of the elements. Thanks

Best Answer

You can use TikZ inside an equation and since your diagonal is full you don't need any nodes inside the upper diagonal entries. Often the delimiters come out too wide so a little nudge towards inside looks better (opinion!)

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\[
\mathbf{D}=\{ d_{ij} \}=
\begin{tikzpicture}[baseline=(U.center),
                    every left delimiter/.style={xshift=1mm},
                    every right delimiter/.style={xshift=-1mm}]

\matrix [matrix of math nodes,
         inner sep=0pt,
         left delimiter={[}, 
         right delimiter={]},
         nodes={inner sep=0,minimum size=6mm,anchor=center}
        ] (U) { 
 0&  &  &  &  \\
 9& 0&  &  &  \\
 3& 7& 0&  &  \\
 6& 5& 9& 0&  \\
11&10& 2& 8& 0\\
};
\foreach \x in {1,...,5}{\\
\node[left]  at (U-\x-\x.west -| U.west) {$\x$};
\node[above] at (U-\x-\x.north|- U.north) {$\x$};
}
\draw[red] (U-1-1.north) -- (U-1-1.west) -- (U-5-5.south) -- (U-5-5.east) -- cycle;
\node [draw, circle, red] at (U-5-3) {};
\end{tikzpicture}
\]
\end{document}

enter image description here