22

I am just starting to use latex, and with all the internet help i figured out how to do many things myself, but now i am stuck. How can I write what you all can see in the picture? This is what i want to get

Thanks for the help!!!

2

2 Answers 2

35

At its simplest, this might be the easiest way.

\documentclass{article}
\usepackage{amsmath} 
\begin{document}
\begin{equation}
\underset{n\times 1}{\mathrm{Y}} =  \underset{n\times p}{X} \times 
\underset{p\times 1}{\theta} + \underset{n\times 1}{\varepsilon}
\end{equation}
\end{document}

enter image description here

As egreg points out, putting the matrix dimensions in \scriptscriptstyle may give a better appearance. That can be added to each underset of the above solution (or, as he notes, by creating a helper macro), or one could try this alternate stackengine approach, which gives added control over the depth of the underset.

\documentclass{article}
\usepackage{stackengine} 
\stackMath
\begin{document}
\begin{equation}
\def\sss{\scriptscriptstyle}
\setstackgap{L}{8pt}
\def\stacktype{L}
\stackunder{\mathrm{Y}}{\sss n\times 1} =  \stackunder{X}{\sss n\times p} \times 
\stackunder{\theta}{\sss p\times 1} + \stackunder{\varepsilon}{\sss n\times 1}
\end{equation}
\end{document}

enter image description here

If one wants a more visual interpretation of this result, I would note this related question, Matrix decomposition dimensions diagram

\documentclass{article}
\usepackage{stackengine}
\stackMath
\newlength\matfield
\newlength\tmplength
\def\matscale{1.}
\newcommand\dimbox[3]{%
  \setlength\matfield{\matscale\baselineskip}%
  \setbox0=\hbox{\vphantom{X}\smash{#3}}%
  \setlength{\tmplength}{#1\matfield-\ht0-\dp0}%
  \fboxrule=1pt\fboxsep=-\fboxrule\relax%
  \fbox{\makebox[#2\matfield]{\addstackgap[.5\tmplength]{\box0}}}%
}
\newcommand\raiserows[2]{%
   \setlength\matfield{\matscale\baselineskip}%
   \raisebox{#1\matfield}{#2}%
}
\newcommand\matbox[5]{
  \stackunder{\dimbox{#1}{#2}{$#5$}}{\scriptstyle(#3\times #4)}%
}
\parskip 1em
\begin{document}
$\renewcommand\matscale{.6}
\matbox{7}{2}{n}{1}{\mathrm{Y}} = 
\matbox{7}{4}{n}{p}{X} \raiserows{1.5}{\matbox{4}{2}{p}{1}{\theta}} +
\matbox{7}{2}{n}{1}{\varepsilon}$
\end{document}

enter image description here

6
  • 2
    No stackengine solution? ;-)
    – user31729
    Commented Jun 3, 2015 at 16:08
  • 3
    @ChristianHupfer Damned if I do...damned if I don't :^) I might add it as an alternative. Commented Jun 3, 2015 at 16:10
  • It was my first idea, but you're definitely quicker than me, so go ahead ;-)
    – user31729
    Commented Jun 3, 2015 at 16:10
  • Possibly \underset{\scriptscriptstyle n\times p}{X} gives less prominence to the dimensions.
    – egreg
    Commented Jun 3, 2015 at 16:12
  • 3
    \newcommand{\matrixdim}[2]{\underset{\scriptscriptstyle#2}{#1}} and \matrixdim{X}{n\times 1} is easy (and preferable anyway, even if we don't add the style selection).
    – egreg
    Commented Jun 3, 2015 at 16:15
3

I add my little answer in the form of a tabular (much more dirty than the math ones, but may be useful if a lot of complexity is added to the expression)

\documentclass{article}

\begin{document}
\begin{tabular}{ccccccc}
Y & $=$ & $X$ & $\times$ & $\theta$ & $+$ & $\epsilon$ \\
$n\times 1$ & & $n\times p $ & & $p\times 1$ & & $n\times 1$
\end{tabular}
\end{document}

1
  • 1
    This is a workable solution, thanks for contributing :-) I'll note to you and others though that if you're a stickler for typography, the spacing is waaay off for mathematics. By the way, you might be able to use a matrix or array environment to avoid going in and out of math mode all the time :-) Commented Jun 3, 2015 at 20:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .