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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using quite a few \colorbox and \colorfbox in a document but they weren't sitting at the same heights (because some words contained g and this seemed to push the box down.

The solution I found (in another stackoverflow question actually) was to use \strut.

Only problem with that is that it defaulted to being very high.

I couldn't figure out how to shrink the height (I'm very new to Latex) and was wondering if there's a simple way to specify the height of the \strut command?

For example I have \colorbox{newredred}{{\color{white}{\strut\tt fun}}}.

Is what I'm asking possible? Please remember, I'm very new to LaTeX and don't really understand much about the renewing commands or setting custom commands.

share|improve this question
up vote 8 down vote accepted

For example : #1 the height #2 the depth

\def\mystrut(#1,#2){\vrule height #1pt depth #2pt width 0pt}   

example :


  \def\mystrut(#1,#2){\vrule height #1pt depth #2pt width 0pt}    
  \fbox{\mystrut(18,10) qk}  

enter image description here

share|improve this answer
Add \newcommand\mystrut{} before \def\... to avoid a problem if mystrut is already defined. – Alain Matthes Mar 30 '11 at 20:38

If the problem is exactly that some words contain g and some do not, then why not add \vphantom{g} to each box? This makes it as though each box contains g.

share|improve this answer
You guys are absolute legends. All these answers are perfect, this one specially! This will be very useful in future! – dieselpower44 Mar 30 '11 at 20:29
The choice depends of the context. My "hard" solution is only for some cases like tables or pictures but it's the way to add some height. If you need to get some space inside a text, I prefer to use the answers from Martin or Lev – Alain Matthes Mar 30 '11 at 20:35

The \strut command adds the maximal required height (.7\baselineskip) and depth (.3\baselineskip) of the current font size. Both together equal the current baselineskip, i.e. the distance between two lines. To explain this in short: There is an invisible line called the baseline on which all the letters sit, and some letters like "g" have a depth and go below that line. Two lines are separated between their baselines by the baselineskip. With a normal 10pt font the baselineskip is 12pt. Adding a \strut makes the whole box 12pt in size (inside the framebox + the frameboxsep).

You could insert a smaller \strut by changing the font size locally, e.g.:


However, it is no longer guaranteed that \strut will make all boxes have the same total height.

share|improve this answer

Besides Lev Bishop's suggestion (inluding aldo an f in the \vphantom to compensate the height), you could also control the separation between the text and the box using \fboxsep. The following code shows two new commands, both using a redefinition of \fboxsep; the first one uses the \vphantom approach and the second one, uses \strut:


  \setlength\fboxsep{2pt}% change according to your needs

  \setlength\fboxsep{0pt}% change according to your needs




share|improve this answer
+1 Adding \vphantom{fg} (or Ag) is a very good idea and some form of alternative \strut. I also thought to change \fboxsep, but the problem is that the horizontal distance is also changed. Ok, this could be adjusted with \,, spaces or \hspace. – Martin Scharrer Mar 30 '11 at 20:24

You could also use \mathstrut, which is slightly smaller, instead of \strut.

share|improve this answer

Dirty trick, yet a very simple way to set a \strut-like of minimal height : simply use a non-breaking space instead.

share|improve this answer
Usually a strut is meant to be something “vertical”, not horizontal. – egreg Sep 19 '15 at 22:45
@egreg : Really ? I'm honestly surprised, I've used hundreds of \strut, yet I can't remember having used it for a vertical purpose even once, my typical use being to force an \hfill. – Skippy le Grand Gourou Sep 19 '15 at 22:48
\leavevmode\null in plain TeX; \hspace*{\fill} in LaTeX. Assuming you do really want to “force an \hfill”. – egreg Sep 19 '15 at 22:50
I just looked at the translation for "strut", now it makes sense indeed. But then, for a vertical control, the lack of a height property is quite pitiful… – Skippy le Grand Gourou Sep 19 '15 at 22:56

Your Answer


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.