Take the 2-minute tour ×
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.

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}
\mybox{1}{b}\\[-0.65pt]
\mybox{1}{c}
\end{array}
\end{array}$$

\end{document}

I expected the total height of the two boxes on the right to be the same as the height of the left box, namely 2cm. However, there seem to be a hidden height which is added. How can I control this?


Update

Thanks for the quick and detailed replies, it helped a lot!

However, there is still another problem:

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


\newcommand{\myarray}[4][t]{
    \renewcommand{\arraycolsep}{-#3pt}
    \begin{array}[#1]{#2}#4\end{array}}

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

\begin{document}

\[\left(
\myarray{ll}{0.1}{

\myarray{ll}{0.15}{
   \mybox{yellow}{4}{A}&\mybox{green}{4}{B}\\[-5.15pt]
 & \mybox{blue}{4}{C}
}
& \mybox{red}{8}{D}
}
\right)\]

\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.

share|improve this question
    
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
show 2 more comments

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}

enter image description here

share|improve this answer
add comment

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}

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

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.