3

I have provided a code for a simple textbox in LaTeX below. I have also attached a screenshot of the output, next to a similar text box compiled in Microsoft Word. I would like the LaTeX textbox to be formatted in the way as the MS Word one. In particular, the vertical centering is the issue. The MS Word textbox looks like the LaTeX one only when the letter g (or any other letter with a descender) is amended with \smash{}. If \smash{} is NOT used, then the descender adds more vertical space below the text. Could someone please provide a LaTeX code that automatically smashes these descenders (or something achieving a similar effect) so that the text in the LaTeX box matches the vertical centering in the MS Word one? I'd really appreciate it.

Note that at some point I would like to add equations to these text boxes, most probably using the align environment. It is important that a full solution to this problem does not introduce any additional vertical space around this align environment, as is the case for the existing proposed answer to this question.

\documentclass[12pt]{article}
\usepackage{lipsum} % for generating dummy text
\usepackage{tcolorbox} % for creating colored boxes

\begin{document}

\begin{tcolorbox}[colback=white,colframe=black]
\LARGE  abcdefg
\end{tcolorbox}

\end{document}

enter image description here

2
  • Anything more constructive to add?
    – wrb98
    Feb 9 at 19:57
  • 1
    @wrb98 It is usually considered rather rude to modify a question in a way which invalidates an existing answer. You should ask a new question with your new requirements. Feb 9 at 20:04

1 Answer 1

6

You could use \strut to ensure that your text always has the maximum height and then reduce the top and bottom margins as you want:

\documentclass{article}
\usepackage{tcolorbox} 

\newtcolorbox{test}{
  colback=white,
  colframe=black,
  bottom=-3pt,
  top=0pt,
  fontupper=\LARGE,
  after upper={\strut},
  before upper={\strut}
}

\begin{document}

\begin{test}
ace 
\end{test}

\begin{test}
abcdefg 
\end{test}

\end{document}

enter image description here

If for whatever strange reasons the presence of ascenders should influence the box height, but the presence of descenders should not influence the box height, then you could do something like

\documentclass{article}
\usepackage{tcolorbox} 

\newtcolorbox{test}{
  colback=white,
  colframe=black,
  bottom=0pt,
  top=3pt,
  fontupper=\LARGE,
  after upper={\vphantom{g}},
}

\begin{document}

\begin{test}
ace 
\end{test}

\begin{test}
abcdefg 
\end{test}

\end{document}
7
  • 1
    You missed setting the font to Calibri ... =) Feb 9 at 16:30
  • 3
    @JasperHabicht I'd rather get tortured :P Feb 9 at 16:30
  • This seems to do the trick. However, at some point I may wish to use equations in this new environment via \begin{align*} ... \end{align*}. If I were to put such an equation after the 'abcdefg' example text, then this creates unnecessary and unwanted vertical space around the align* environment. How can this be removed to give a result that is more consistent with the spacing around the ordinary text in your example?
    – wrb98
    Feb 9 at 19:39
  • @wrb98 If you have a new question, please ask a new question Feb 9 at 19:41
  • 1
    @wrb98 Why should the absence of ascenders influence the top spacing, but the presence of descenders should not change the bottom spacing? This makes no sense at all. Feb 9 at 20:10

You must log in to answer this question.

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