# Height of colorbox

I'm trying to "highlight" two parts of a sentence by using a \colorbox like so:

\colorbox{red}{This is} \colorbox{blue}{a great sentence.}


The problem is that in the second part the height colorbox is a bit bigger, because it contains a g which extends below the baseline. As a result, the two colorboxes do not align at the bottom which looks suboptimal.

How would I make the two boxes line up?

-
@Yiannis: I'm not sure, but I find this a strange tag for the question. Are you going to add this tag to every solution where the \strut was the main point? –  Hendrik Vogt Dec 21 '10 at 19:44
@Hendrik Tags trigger related keyword questions. I thought if there were more examples it would be useful. –  Yiannis Lazarides Dec 21 '10 at 20:09
@Yiannis: If we had tag support for answers, the tag would be fine there. However, \strut is just one possible solution of many, though probably the easiest. I prefer tags which characterize the question (size, alignment) instead of keywords of each good answer (\strut, \rule, \vphantom, \parbox, ...). It could be discussed further on meta: Adding tags to a question in accordance with a provided answer. –  Stefan Kottwitz Dec 21 '10 at 20:41

You can use a \strut

{\colorbox{red}{\strut This is} \colorbox{blue}{\strut a great sentence.}}


LaTeX/TEX define \strut to be an invisible box of width zero that extends just enough above and below the baseline.

-
Is it possible to redefine the \strut to be as short as possible based on the current font size? –  xport Dec 22 '10 at 4:16
@xport It is possible. A \strut is simply a rule of zero width for example in plain TeX it is \vrule height8.5pt depth3.5pt width0pt. –  Yiannis Lazarides Dec 22 '10 at 4:27
but it is not the shortest \strut I think. Thank you anyway. –  xport Dec 22 '10 at 4:29
I was looking just for this thing!! Great! As a bonus point: "A strut is a structural component designed to resist longitudinal compression. " –  M'vy Jul 20 '11 at 21:01

Let's compare and decide which one do you prefer :)

\documentclass{article}

\usepackage[paperwidth=10cm,paperheight=10cm]{geometry}
\usepackage{graphicx}
\usepackage{xcolor}

\usepackage{calc}

%Define a reference depth.
%You can choose either relative or absolute.
%--------------------------
\newlength{\DepthReference}
\settodepth{\DepthReference}{g}%relative to a depth of a letter.
%\setlength{\DepthReference}{6pt}%absolute value.

%Define a reference Height.
%You can choose either relative or absolute.
%--------------------------
\newlength{\HeightReference}
\settoheight{\HeightReference}{T}
%\setlength{\HeightReference}{6pt}

%--------------------------
\newlength{\Width}%

\newcommand{\MyColorBox}[2][red]%
{%
\settowidth{\Width}{#2}%
%\setlength{\fboxsep}{0pt}%
\colorbox{#1}%
{%
\raisebox{-\DepthReference}%
{%
\parbox[b][\HeightReference+\DepthReference][c]{\Width}{\centering#2}%
}%
}%
}

\setlength{\fboxsep}{1pt}
\begin{document}

\noindent @Thomas \hrulefill\colorbox{red}{This is} \colorbox{blue}{a great sentence.}\hrulefill

\vspace{5mm}

\noindent @Yiannis \hrulefill{\colorbox{red}{\strut This is} \colorbox{blue}{\strut a great sentence.}}\hrulefill

\vspace{5mm}

\noindent @xport \hrulefill\MyColorBox{This is} \MyColorBox[blue]{a great sentence.}\hrulefill

\end{document}


EDIT 1:

My idea above can be explained as follows:

1. I choose the tallest letter as a reference for common height.
2. I choose the deepest letter as a reference for common depth.
3. Then a make a parbox with constant height based on the sum of height and depth mentioned in step 1 and 2.
4. Move the parbox down as far as the depth mentioned in step 2.
-
@How about a letter with and accent? –  Yiannis Lazarides Dec 22 '10 at 4:30
@Yiannis, I have not tried it. I will do soon. Go to have lunch now. Thank you. –  xport Dec 22 '10 at 4:31
I don't think it's a good idea to move the boxes up and down. The baselines should stay aligned in good typography. –  Hendrik Vogt Dec 22 '10 at 7:54
@Hendrik. Yes. I think so. –  xport Dec 22 '10 at 7:55
\documentclass[a5paper,12pt]{article}