# Exact height of \hbox

I have problems controlling the exact height of an \hbox; what is wrong with this example?

\documentclass{article}

\newcommand{\mybox}[2]{
\fbox{ \vbox to #1cm{
\vfil\hbox to 1cm {\hfill #2 \hfill}
\vfil}}}

\begin{document}

\renewcommand{\arraycolsep}{-0.1pt}
\begin{array}{cc}
\mybox{2}{a} &
\begin{array}[b]{c}

\end{document}


The left and right brackets are way to large; does anyone see what goes wrong?

And, second, is there a more elegant way of setting arraycolsep and the newline

\$-5.15pt]  automatically to the correct value? I want the boxes to be as close as possible, vertically and horizontally. - 2\frulewidth+2\frulesep – Danie Els Jun 10 '11 at 1:12 Welcome to tex.sx! A tip: If you indent lines by 4 spaces, then they are marked as a code sample. You can also highlight the code and click the "code" button (with "{}" on it), and you can use backticks  to mark your inline code as I did in my edit. – Gonzalo Medina Jun 10 '11 at 1:30 Sorry it is 2\fboxrule+2\fboxsep – Danie Els Jun 10 '11 at 1:31 @Heiko: Your answer post created a new unregistered account (with the same name and icon). I merged both together now and moved your post to the question. Please consider to register your account to avoid such issues. Thanks. – Martin Scharrer Jun 10 '11 at 7:29 I'd like to thank everyone for these most helpful suggestions! I'm still not used to this editor here, but at least now I'm registered ;) – Heiko Jun 10 '11 at 9:20 ## 2 Answers You are not taking into account \fboxsep (the separation between the framed box and its contents; default value: 3pt) and the "thickness" of the rules used (\fboxrule: 0.4pt, by default). If you set \fboxsep to 0pt, then the box on the right will have a total height of 2cm+0.4pt while the two stacked boxes on the left will have a total height of 1cm+1cm+(4*0.2pt)=2cm+0.8pt (if the middle horizontal rules don't overlap, but are immediately next to each other) or 1cm+1cm+(3*0.2pt)=2cm+0.6pt (if the middle horizontal rules overlap). EDIT: by the way, instead of using ... for displayed math, you should use \[...$.

EDIT2: although I think that sometimes TikZ is overused, in this particular case I think that it would be sensible to give it a try (it facilitates the exact placement of the colored boxes):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\def\MyUnit{ex}
\begin{tikzpicture}[x=1\MyUnit,y=1\MyUnit]

\def\MyBox#1#2#3#4{\node[draw,fill=#1,minimum width=#2\MyUnit,minimum height=#2\MyUnit,align=center] at (#4) {#3};}

\matrix [matrix of nodes,left delimiter=(,right delimiter=)]
{
\MyBox{yellow}{4}{A}{0,0}
\MyBox{green}{4}{B}{4,0}
\MyBox{blue}{4}{C}{4,-4}
\MyBox{red}{8}{D}{10,-2}\\
};

\end{tikzpicture}

\end{document}


-

Do not use the Tex primatives \vbox and \hbox in directly in latex, because they are not color "save". Use the latex equavalents \parbox, \makebox, and minipage.

The code below gives you a ruled box with required outer dimension (including the rule width and separation)

EDIT:

Rather use the picture environment to add your boxes, then you have more control

\documentclass{article}
\usepackage{color}
\usepackage{calc}
\newlength\mylenA
\newlength\mylenB

\newcommand\mybox[4][t]{%
\setlength\mylenA{#3\unitlength-2\fboxrule-2\fboxsep}%
\setlength\mylenB{#3\unitlength-2\fboxrule-2\fboxsep}%
\fcolorbox{black}{#2}{\parbox[#1][\mylenA][c]{\mylenB}{\centering #4}}}

\begin{document}

\setlength\unitlength{1ex}
$\left( \begin{picture}(16,5) \put(0, 2){\mybox[c]{yellow}{4}{A}} \put(4, 2){\mybox[c]{green}{4}{B}} \put(4,-2){\mybox[c]{blue}{4}{C}} \put(8, 0){\mybox[c]{red}{8}{D}} \end{picture} \right)$
\end{document}
`

-