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
  • 1
    @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.

| improve this answer | |
  • 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
  • 1
    @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
  • 1
    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 :)

alt text




%Define a reference depth. 
%You can choose either relative or absolute.
\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.




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


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


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



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.
| improve this answer | |
  • 1
    @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
  • 2
    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
  • 1
    @Hendrik. Yes. I think so. – xport Dec 22 '10 at 7:55


\hl{This is} \hl{\strut a great sentence.}

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.