# 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 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 at 22:48
\leavevmode\null in plain TeX; \hspace*{\fill} in LaTeX. Assuming you do really want to “force an \hfill”. – egreg Sep 19 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 at 22:56
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.922095775604248, "perplexity": 2335.4237628183137}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2015-48/segments/1448398453553.36/warc/CC-MAIN-20151124205413-00054-ip-10-71-132-137.ec2.internal.warc.gz"}