Take the 2-minute tour ×
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.

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
add comment

5 Answers

up vote 7 down vote accepted

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}

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
add comment

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.

share|improve this answer
add comment

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
add comment

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}
share|improve this answer
1  
+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
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

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.