TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What's the easiest way of making diagrams depicting matrix dimensions such as the following?

enter image description here

I can do it with simple subscripts, but I like the visual effect that these diagrams give.

share|improve this question
up vote 8 down vote accepted

Here I do it with stacks, setting the indices below the boxes. The primary macro is

\matbox{rows}{columns}{row index}{column index}{matrix name}.

A helper macro is \raiserows, which is like a \raisebox, except that the "length" is specified in rows, not actual length. Note that in the MWE, because X has 7 rows, while P^T only has 2 rows, P^T must be lifted (7-2)/2 = 2.5 rows.

I introduce the factor \matscale to scale your box sizes. By default, each row and column of the box size will occupy a square of size \baselineskip on a side. The factor \matscale (which defaults to 1) will scale that value.

EDITED to set array indices in \scriptstyle

  \stackunder{\dimbox{#1}{#2}{$\mathbf{#5}$}}{\scriptstyle(#3\times #4)}%
\parskip 1em
\matbox{7}{4}{I}{J}{X} = 
\matbox{7}{2}{I}{R}{T} \raiserows{2.5}{\matbox{2}{4}{R}{J}{P^T}} +

enter image description here

Just to clarify, in case it wasn't clear. One can use actual (very large) matrix dimensions, as long as one scales suitably small. For example, the following works::

\matbox{300}{75}{I}{J}{X} = 
\matbox{300}{25}{I}{R}{T} \raiserows{137.5}{\matbox{25}{75}{R}{J}{P^T}} +
share|improve this answer
Awesome. I've never used 'stackengine' before. Thank you! – Memming Mar 27 '14 at 16:07
I didn't have 'stackengine' package installed by default on macTeX, so I needed sudo tlmgr install stackengine – Memming Mar 27 '14 at 17:42
@Memming The package is almost a year old, but that still might not make it to some older installations. Thanks for figuring it out, since I couldn't help you with mactex. – Steven B. Segletes Mar 27 '14 at 18:00
@Memming if you used MacTeX to install tex on you Mac, it comes with Tex Live Utility which is a great tool to update packages via a graphic user interface. – jfbu Mar 27 '14 at 20:47

This is one possible solution via amsmath package.

enter image description here


\usepackage[margin=1cm,paper size={20cm,5cm}]{geometry}

  &    & \\
  & X &\\
  &     &
\mkern-10mu}_{I \times J}=
\mkern-10mu}_{I \times R}
  &    & \\
  & \raisebox{10pt}{$P^T$} &\\
\end{pmatrix}\mkern-10mu}_{R \times J}+
  &    & \\
  & E &\\
  &    &
\mkern-10mu}_{I \times J}

share|improve this answer

Here is a way to do it "manually."

\newcommand*{\clap}[1]{\hbox to 0pt{\hss#1\hss}}
\newcommand*{\subdims}[3]{\clap{\raisebox{#1}[0pt][0pt]{$\scriptstyle(#2 \times #3)$}}}

\framebox[2.5cm]{\clap{\raisebox{0pt}[1.5cm][1.5cm]{$\mat X$}}\subdims{-2.5cm} I J} =
\framebox[1.5cm]{\clap{\raisebox{0pt}[1.5cm][1.5cm]{$\mat T$}}\subdims{-2.5cm} I R} \ 
\framebox[2.5cm]{\clap{\raisebox{5mm}[1.5cm]{$\mat P^T$}}     \subdims{-1cm} R J} + 
\framebox[2.5cm]{\clap{\raisebox{0pt}[1.5cm][1.5cm]{$\mat E$}}\subdims{-2.5cm} I J}



Matrix equation

Obviously this doesn't compare to Mr. Segletes's powerful and general answer! It does use more basic commands, however, which could have the benefits that it's quicker to compile on a slow machine, and it will work even with a smaller installation that's lacking in packages. Or perhaps you just like getting your hands dirty. The drawback, of course, is that you have to determine all the widths, heights, and depths yourself.

Note: if you use mathtools or any other package that provides a \clap command, it will conflict with the definition. In this case, just omit this definition and use the \clap provided.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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