# How to center node in tikz matrix cell?

How do I center a node in the middle of a tikz matrix cell?

This MWE:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\tikzset{mynode/.style={rectangle,draw,minimum width=2cm,minimum height=1cm}}
\begin{tikzpicture}
\matrix[row sep=1cm]{
\node[mynode] {A}; \\
\node[mynode] {B}; \node[mynode,right=2cm] {C}; \\};
\end{tikzpicture}
\end{document}


produces

How do I get the A node to be between (but still above) the B and C nodes? I know how to do this without using a tikz matrix, but this is for a more complicated diagram where the matrix is the easiest way to layout the rest of it.

• Why do you need a matrix, here? It's easy to achieve what you need with simple nodes. Or is it a part of a larger project? Commented Feb 19, 2022 at 21:47
• Yes, as I wrote in my question, it's part of a larger project that requires a matrix.
– Alan
Commented Feb 19, 2022 at 21:59

One more solution, with use of matrix library:

\documentclass[tikz, border=3.141592mm]{standalone}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}
\matrix[matrix of nodes,
nodes={draw, minimum width=2cm, minimum height=1cm},
row sep=1cm]
{
\node {A};   \\
\node[left=5mm] {B}; \node[right=5mm] {C};\\
};
\end{tikzpicture}
\end{document}


\documentclass[tikz, border=1cm]{standalone}
\begin{document}
\tikzset{mynode/.style={rectangle, draw, minimum width=2cm, minimum height=1cm}}
\begin{tikzpicture}
\matrix[row sep=1cm]{
\node[mynode] {A}; \\
\node[mynode, left=0.5cm] {B}; \node[mynode, right=0.5cm] {C};\\
};
\end{tikzpicture}
\end{document}


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

\begin{document}
\tikzset{mynode/.style={rectangle,draw,minimum width=2cm,minimum height=1cm}}
\begin{tikzpicture}
\matrix[row sep=1cm]{
\node[mynode] (B) {B}; \node[mynode,right=2cm] (C) {C}; \\};
\node[mynode] at ($(B)!0.5!(C)+(0,2cm)$) {A};
\end{tikzpicture}
\end{document}

• Thanks, but is there any way to have node A be within the matrix itself? The actual picture has a big matrix of which A, B, and C are parts.
– Alan
Commented Feb 19, 2022 at 22:48