# A box for math with same height (and width) as some other math expression (especially for matrix elements/blocks)

This is my MWE:

\documentclass[a4paper,12pt]{article}
\usepackage[T1]{fontenc}

\usepackage{mathtools}
\usepackage{physics}
\usepackage{easybmat}

\begin{document}
First attempt:
\begin{align*}
&\left[\begin{BMAT}{c1c}{c1c}
a& \begin{matrix} b_1& b_2 \end{matrix} \\
\begin{matrix} c_1  \\ c_2 \end{matrix}&     D
\end{BMAT}\right]
\left[\begin{BMAT}{c1c}{c1c}
a&  \mathmakebox[\widthof{$\begin{matrix} b_1& b_2 \end{matrix}$}]{\vb*b}\\
\begin{matrix} c_1  \\ c_2 \end{matrix}&    D
\end{BMAT}\right]\\
&\left[\begin{BMAT}{c1c}{c1c}
a&  \mathmakebox[\widthof{$\begin{matrix} b_1& b_2 \end{matrix}$}]{\vb*b}\\
\begin{matrix} c_1  \\ c_2 \end{matrix}&    D
\end{BMAT}\right]
\end{align*}

Second attempt:
\begin{align*}
&\left[\begin{BMAT}{c1c}{c1c}
a& \begin{matrix} b_1& b_2 \end{matrix} \\
\begin{matrix} c_1  \\ c_2 \end{matrix}&     D
\end{BMAT}\right]
\left[\begin{BMAT}{c1c}{c1c}
a& \begin{matrix}
\mathmakebox[\widthof{$\begin{matrix} b_1& b_2 \end{matrix}$}]{\vb*b}
\end{matrix}\\
\begin{matrix} c_1  \\ c_2 \end{matrix}&    D
\end{BMAT}\right]\\
&\left[\begin{BMAT}{c1c}{c1c}
a& \begin{matrix}
\mathmakebox[\widthof{$\begin{matrix} b_1& b_2 \end{matrix}$}]{\vb*b}
\end{matrix}\\
\begin{matrix} c_1  \\ c_2 \end{matrix}&    D
\end{BMAT}\right]
\end{align*}
\end{document}


Here the mathtools package should be necessary for the command \mathmakebox; I'm using the easybmat package because it's a great tool for handling blocks in matrices, which is necessary for my purposes; the physics package here is used just for quick vector notation, but of course it's not necessary. One should get this result:

I often need to divide a matrix into several sectors, or merge several blocks into some bigger blocks, and so on… In doing this I often would like corresponding objects to have the very same visual dimensions.
In the MWE above, I tried to show how it is possible to make a "multi-column" matrix element of the same width of some consecutive elements in the same row with \mathmakebox and \widthof. [*]
In the "first attempt" it is shown that this trick works perfectly with horizontal spacing, but not with vertical spacing; in the "second attempt" a workaround for the vertical spacing is shown.
My questions are:

1. Is there any cleaner solutions than the one I used above (in the MWE)?
2. (fundamental) Is there a similar procedure to have a "multirow" matrix element of the same height of some consecutive elements within the same column?, in the MWE, just think of replacing the column (c1,c2) with a vector c, analogously to what has been done with (b1,b2) and b; I ask this especially because \heightof in \mathmakebox doesn't do the trick. [**]
3. (more general) Is there a way to make a box, intended to be filled with math, with the same dimensions (the height and width) of some other math expression?, eventually is there a way to use two different expressions, one for width and one for height?

[*] I have to add, here, that I usually avoid merging columns and/or rows when managing matrices; I find nesting matrices a simpler solution; of course, I may very well be wrong on this!, anyway this question may eventually make sense also for other purposes, unrelated to matrices.
[**] I may have a workaround also for this problem, by using boxes with null width in which putting phantom expressions; I refrained from posting it: it is so inelegant that, had I posted, I'd have been banned for ever.

• What package does \vb come from, or is it local? Sep 16 '20 at 23:32
• It is from the package physics; I guess that "vb" stands for "vector bold", the starred version is for italic while the non-starred version is for normal. Anyway, I see from your answer that you already figured out… Sep 17 '20 at 2:10

You can avoid duplicate work by using a savebox.

\documentclass{article}
\usepackage{mathtools}
\usepackage{easybmat}

\newsavebox{\tempbox}% \sbox0 doesn't work

\begin{document}

\savebox\tempbox{$\begin{matrix} b_1 & b_2 \end{matrix}$}% must be outside align environment
\begin{align*}
&\left[\begin{BMAT}{c1c}{c1c}
a& \usebox\tempbox \\
\begin{matrix} c_1  \\ c_2 \end{matrix}&     D
\end{BMAT}\right]
\left[\begin{BMAT}{c1c}{c1c}
a&  \mathmakebox[\wd\tempbox]{\boldsymbol{b}}\\
\begin{matrix} c_1  \\ c_2 \end{matrix}&    D
\end{BMAT}\right]\\
&\left[\begin{BMAT}{c1c}{c1c}
a&  \mathmakebox[\wd\tempbox]{\boldsymbol{b}}\\
\begin{matrix} c_1  \\ c_2 \end{matrix}&    D
\end{BMAT}\right]
\end{align*}

\end{document}

• So, please correct me if I'm wrong: this is an answer to (1), and you're telling me that there's no need to copy and past a long code too many times, it's cleaner to "store it" apart; was this your point? Sep 17 '20 at 2:16
• @atlantropa The way I’d summarize it is: if you store the result, you can take its height. You can use that height for the other visible content you want to vertically align. Then you can also use the stored result instead of retyping it. It’s even possible that retyping the code would have side effects, like incrementing a counter twice. Sep 17 '20 at 3:32
• Basically, yes. Also, the width, height and depth of a savebox are precomputed, and the code has been reformatted into something faster. \unhbox cannot recover the original code. Sep 17 '20 at 14:52
• Thanks a lot to both of you, but still I don't get one crucial point: how do I assign the height to the box I'm creating?; when I use \heightof inside \mathmakebox I'm not able to get a box with the height I wanted to assign… PS: is \wd more or less the same as \widthof? Sep 17 '20 at 15:43
• @atlantropa The direct way is to assign it, as in \ht0=30pt makes temp box 0 30 pt high, without affecting the content of the box (meaning, the content could extend past the box top). But in the current context, where you likely want a box taller than the content you would otherwise place in it, you could add \vphantom{\copy0} to your content and it would be made large enough to include the original vertical dimensions of temp box 0. This would be the vertical equivalent of the horizontal \mathmakebox[\wd0]{<content>}. Sep 17 '20 at 18:48