25

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?

3
  • @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? Dec 21, 2010 at 19:44
  • @Hendrik Tags trigger related keyword questions. I thought if there were more examples it would be useful. Dec 21, 2010 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, 2010 at 20:41

3 Answers 3

22

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.

4
  • Is it possible to redefine the \strut to be as short as possible based on the current font size? Dec 22, 2010 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. Dec 22, 2010 at 4:27
  • but it is not the shortest \strut I think. Thank you anyway. Dec 22, 2010 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, 2011 at 21:01
15

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

alt text


\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.
5
  • 1
    @How about a letter with and accent? Dec 22, 2010 at 4:30
  • @Yiannis, I have not tried it. I will do soon. Go to have lunch now. Thank you. Dec 22, 2010 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. Dec 22, 2010 at 7:54
  • 1
    @Hendrik. Yes. I think so. Dec 22, 2010 at 7:55
  • You saved my day, thank you so much! Feb 25, 2021 at 12:41
6
\documentclass[a5paper,12pt]{article}

\usepackage{soul,xcolor}
\sethlcolor{red}
\begin{document}

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

\end{document}

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.