# aligned matrices in tikz

I'm quiet newbie in PGF/TikZ and I would like to draw matrices aligned with their first line aligned horizontally and vertically aligned under every term.

My first rough code attempt is:

\documentclass{standalone}
\usepackage[pdftex,active,tightpage]{preview}
\usepackage{tikz}
\usepackage{amsmath,amsfonts,amsthm,bm}
\PreviewEnvironment{tikzpicture}

\newcommand{\x}{\bm{\mathrm{x}}}
\newcommand{\n}{\mathbf{n}}
\newcommand{\y}{\mathbf{y}}
\newcommand{\D}{\mathbf{D}}
\newcommand{\A}{\mathbf{A}}
\renewcommand{\H}{\mathbf{H}}

\begin{document}
\begin{tikzpicture}[scale=1]
\begin{scope}[xshift=0]
\node (xhat) at (0, 0) {$\hat{\x}$};
\node at (1, -2pt) {$=$};
\node (D) at (2, 0) {$\D^H$};
\node (A) at (3, 0) {$\A^H$};
\node (H) at (4, 0) {$(\H$};
\node (x) at (5, 0) {$\x$};
\node at (6, 0) {$+$};
\node (HB) at (7, 0) {$\H_B$};
\node (XB) at (8, 0) {$\x_B$};
\node at (9, 0) {$+$};
\node (n) at (10, 0) {$\n)$};
\end{scope}
\begin{scope}[scale=1/8]
%           \draw[help lines]     (0,0) grid (33,-9);
\draw[thick,yscale=2,shift={([xshift=-12,yshift=-16]xhat)}] (0,0) grid (1,-1);
\draw[thick,xscale=4,yscale=2,shift={([xshift=-18,yshift=-16]D)}] (0,0) grid (1,-1);
\draw[thick,xscale=8,yscale=4,shift={([xshift=-18,yshift=-16]A)}] (0,0) grid (1,-1);
%       \draw[]                     (x) grid ++(1,-2); % x
\draw[thick,yscale=2,shift={([xshift=-12,yshift=-16]x)}] (0,0) grid (1,-1);

\end{scope}
\end{tikzpicture}
\end{document}

• Welcome to TeX.SX! Don't redefine short commands such as \H: you'll regret it when you have to cite a paper by Erdős and get a weird error. Also, \bm{\mathrm{x} is the same as \mathbf{x} (and the latter is much easier, besides being much more efficient). – egreg Sep 18 '18 at 9:17
• Are you looking for something like this? tex.stackexchange.com/questions/168035/… – Steven B. Segletes Sep 18 '18 at 10:02
• @StevenB.Segletes : yes ! but Zarko example is closer to what I want. I would like to add grid to these matrices and color certain cells to emphasize dimensions. – Reda Sep 18 '18 at 12:04

like this?

one of possibilities is use of thematrix library:

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

\usepackage{amssymb}

\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of math nodes,
nodes={inner xsep=0pt},
nodes in empty cells,
column sep=1pt,
row sep=0pt,
row 2/.style = {nodes={draw, minimum height=3ex}}
]
{
\hat{\mathbf{x}}
& = & \mathbf{D}^H & \mathbf{A}^H
& ( & \mathbf{H} & \mathbf{x}
& + & \mathbf{H}_B & \mathbf{x}_B
& + & \mathbf{n}
& )                                 \\
\hphantom{x}
& |[draw=none]| & \hphantom{D^H}  & \hphantom{A^H}
& |[draw=none]| & \hphantom{H}    & \hphantom{x}
& |[draw=none]| & \hphantom{H_B}  & \hphantom{x_B}
& |[draw=none]| & \hphantom{n}
& |[draw=none]|                    \\
};
\end{tikzpicture}
\end{document}
• thanks ! it is very neat. Is it possible to add grids to these matrices and color certain cells ? – Reda Sep 18 '18 at 12:05
• of course it is possible. however, you need to explain first how you like to have this. from your question i can't figured out, how this should look out. – Zarko Sep 18 '18 at 13:04

Here is an alternative to the answers by Steven B. Seglets and Zarko, which combines the flexibility of Steven's nice answer to use standard equations, including things like \left( and \right), with the flexibility of TikZ, which makes it possible to add any desired feature to the boxes like fills, shading and you know not.

\documentclass{article}
\usepackage{amsmath,amssymb,braket,tikz}
\usetikzlibrary{tikzmark,calc}
\begin{document}
\newcounter{mymark}
\newcommand{\Mark}[1][]{\stepcounter{mymark}\tikzmark{m-\themymark}}
$$\Mark \hat{\mathbf{x}} \Mark = \Mark \mathbf{D}^H \Mark \mathbf{A}^H \Mark \left( \Mark \mathbf{H}\, \Mark \mathbf{x} \Mark + \Mark \mathbf{H}_B \Mark \mathbf{x}_B \Mark + \Mark \mathbf{n} \Mark\right)$$
\begin{tikzpicture}[overlay,remember picture]
\foreach \X [evaluate=\X as \Y using {int(\X+1)}] in {1,3,4,6,7,9,10,12}
{\draw ([yshift=-3pt,xshift=1pt]pic cs:m-\X) rectangle
([yshift=-12pt,xshift=-1pt]pic cs:m-\Y); }
\end{tikzpicture}
\end{document}