# Set \strut height

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.

-

For example : #1 the height #2 the depth

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


example :

\documentclass{scrartcl}

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


-
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.

-
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.:

\colorbox{newredred}{{\color{white}{\ttfamily{\small\strut}fun}}}


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

-

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:

\documentclass{article}
\usepackage{xcolor}

\newcommand\MyColorBox[1]{%
\setlength\fboxsep{2pt}% change according to your needs
\colorbox{red}{\textcolor{white}{\ttfamily\vphantom{fg}#1}}
}

\newcommand\MyColorBoxT[1]{%
\setlength\fboxsep{0pt}% change according to your needs
\colorbox{red}{\textcolor{white}{\strut\ttfamily#1}}
}

\begin{document}

\MyColorBox{fun}\MyColorBox{gun}

\MyColorBoxT{fun}\MyColorBoxT{gun}

\end{document}

-
+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.

-

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

-
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