2

The context

I know that it is possible to enclose text in colored boxes by using \colorbox.

\documentclass{standalone}

\usepackage{xcolor}

\begin{document}
 \colorbox{red}{This} \colorbox{green}{is} \colorbox{red}{a} \colorbox{green}{paragraph}\colorbox{red}{.}
\end{document}

enter image description here

I also know that it is possible to make all colorboxes to have equal height by using \strut. I learned this from this answer at another question.

\documentclass{standalone}

\usepackage{xcolor}

\begin{document}
 \colorbox{red}{\strut This}\colorbox{green}{\strut is}\colorbox{red}{\strut a}\colorbox{green}{\strut paragraph}\colorbox{red}{\strut .}
\end{document}

enter image description here

I also know that it is possible to change the font size with \fontsize.

\documentclass{standalone}

\usepackage{fontspec}
\usepackage{xcolor}

\begin{document}
  \fontsize{20pt}{0pt}\selectfont
  This is a paragraph.
\end{document}

enter image description here

The problem

When I change the font size with \fontsize and use \strut, \strut doesn't have the same effect that it has when the font size hasn't been changed. Please see code and screenshot below.

\documentclass{standalone}

\usepackage{fontspec}
\usepackage{xcolor}

\begin{document}
  \fontsize{20pt}{3pt}\selectfont
  \colorbox{red}{\strut This}\colorbox{green}{\strut is}\colorbox{red}{\strut a}\colorbox{green}{\strut paragraph}\colorbox{red}{\strut .}
\end{document}

enter image description here

The question

How to change the font size and, at the same time, make all colorboxes have equal height?

1 Answer 1

7

enter image description here

\fontsize{20pt}{3pt} specifies letters 20pt high on lines that are 3pt apart, which is doomed to fail.

Use \fontsize{20pt}{23pt} to get a 23pt strut rather than a 3pt one.

\documentclass{standalone}

\usepackage{fontspec}
\usepackage{xcolor}

\begin{document}
  \fontsize{20pt}{23pt}\selectfont
  \colorbox{red}{\strut This}\colorbox{green}{\strut is}\colorbox{red}{\strut a}\colorbox{green}{\strut paragraph}\colorbox{red}{\strut .}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .