# How to control the dimensions of \colorbox

I want a long narrow horizontal colored rectangle, something more like a thick line, but I cannot get the height below a certain amount. The best I can come up with is:

\documentclass[11pt]{book}
\usepackage{color}
\begin{document}
xxxxx\colorbox{red}{%
\phantom{------------------}
}%
xxxxx
\end{document}


where the number of - controls the length but I would like at most half the height of:

I have tried various solutions proposed for questions about height of boxes but was not able to find anything that did it. Did I miss anything?

• @Roboticist That question talks about increasing the height/depth. Here a decrease is requested. – Andrew Swann Jan 27 '17 at 7:51
• @AndrewSwann -- i;ve voted to reopen, and i have a working answer. – barbara beeton Jan 27 '17 at 14:27

The problem is that the - is sits an box with white space around it. You can see this by writing

\setlength{\fboxsep}{0pt}
\fbox{-}


which produces

indicating the box containing the dash sits on the baseline and rises to the height of a capital letter. (Actually --- is producing a different glyph, but the same considerations apply.) To get better control use a \rule. The syntax is

\rule[2pt]{3cm}{1.5ex}


to produce a rule of width 3cm, height 1.5ex all raised 2pt above the baseline. In your case to fit with the x's \rule{2cm}{1ex} should suffice, since 1ex is the nominal height of x in the font, but there are some optical effects from the serifs, so I suggest

\rule[-0.05ex]{2cm}{1.1ex}


As this is the actual shape you want to see you can enclose it in a simple \textcolor rather than having to make a phantom.

\documentclass[11pt]{book}

\usepackage{color}

\begin{document}

xxxxx\textcolor{red}{\rule[-0.05ex]{2cm}{1.1ex}}xxxxx

\end{document}

• the answer i had used \raisebox{.5ex}{\colorbox{red}{\hphantom ...} but your solution is better. the problem with the \hphantom, even with a rule, is that depth is added. (the only reason for this comment is to report the depth effect.) – barbara beeton Jan 27 '17 at 14:36
• @Roboticist As John Holt once put it in "How Children Fail", one ought to strive for being question oriented as opposed to answer oriented. Please think of the resulting chilling effect which is very real. – schremmer Jan 27 '17 at 15:58
• @Andrew Swann and barbara beeton I know, I know but I cannot help thanking you for averting my being chilled. – schremmer Jan 27 '17 at 15:59