# Drawing shapes in a tikz matrix

I want to draw or fill a triangle between three nodes in a matrix. The following does not work, so it seems my basic understanding of how matrix nodes work is wrong:

\begin{tikzpicture}
\matrix[matrix of math nodes, name=m] {
A & B \\
C & D \\};
\draw (m-2-1) -- (m-1-1) -- (m-2-2) -- cycle;
\fill (m-1-1) -- (m-1-2) -- (m-2-2) --cycle;
\end{tikzpicture}


Instead of a triangle, only one line (the first) is drawn and the shape is not filled at all.

I have so far failed to find an answer online, although this seems pretty basic. That's why I think I am missing something important... Why is the triangle not drawn completely / the shape not filled?

• You might also try (m-2-1.center) -- (m-1-1.center) ... – John Kormylo Aug 17 '17 at 13:44
• (m-2-1)--(m-1-1) draws a line from m-2-1.center to m-1-1.center but stopping at node borders. Then your path is made with unconnected fragments which form a non closed path that cannot be filled. – Ignasi Aug 17 '17 at 14:24

I think it's because the node are contiguous (if you add nodes={draw} to your code you can see it).

And I think the fill doesn't work because the line is not closed. If you add some space between the nodes and use the coordinates to draw the lines, it works:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix[matrix of math nodes, %nodes={draw},
row sep=10pt,
column sep=10pt] (m) {%
A & B \\
C & D \\};
\draw[fill] (m-2-1.north east) -- (m-1-1.south east) -- (m-2-2.north west) -- cycle;
\end{tikzpicture}
\end{document}


(Even if I don't know if this is exactly what you're looking for).