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:



            \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)$};       
%           \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);


enter image description here

  • 1
    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?

enter image description here

one of possibilities is use of thematrix library:

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


  \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}}
    & = & \mathbf{D}^H & \mathbf{A}^H
    & ( & \mathbf{H} & \mathbf{x}
    & + & \mathbf{H}_B & \mathbf{x}_B
    & + & \mathbf{n}
    & )                                 \\
    & |[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]|                    \\
  • 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
  • Tried to modify your code to obtain something like \renewcommand\nu{2} \newcommand\nr{8} \newcommand\nrf{4} \renewcommand\a{1/8} \draw[scale=\a] (0,0) grid (\nr,-\nrf) imgur.com/a/Z5DKFnP – Reda Sep 18 '18 at 13:22

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.

\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); }

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.