16

How to get a box including some text in text mode, which depth 0pt?

Martin Scharrer
  • 262,582

3 Answers3

29

You can use \raisebox to modify the official height and depth of some text:

\raisebox{0pt}[\height][0pt]{<text>}

This raises the text by 0pt (so not at all), keeps the official height identical to the original height and sets the depth to 0pt.

Martin Scharrer
  • 262,582
  • @M.S.That is exactly what I want! Thank you. – Stufazi Hoqckt Apr 25 '11 at 08:54
  • 3
    Just a note to clarify that the first optional parameter specifies the height (that is, x-height), and the second the depth (distance to lowest descender). And \height is a command to get the natural height of <text>. – Micah Walter Jun 08 '15 at 16:11
13

\smash{great} is a box with zero height and no depth

\documentclass{article}
\usepackage{amsmath}
\newsavebox\SBox
\begin{document}
\sbox\SBox{great}%
\the\wd\SBox\\
\the\ht\SBox\\
\the\dp\SBox

\sbox\SBox{\smash{great}}%
\the\wd\SBox\\
\the\ht\SBox\\
\the\dp\SBox

\sbox\SBox{\smash[b]{great}}%% Needs amsmath
\the\wd\SBox\\
\the\ht\SBox\\
\the\dp\SBox

\end{document}

enter image description here

  • @Herbert But if I use \smash{a} in text mode, the letter a turned a math italic shape. – Stufazi Hoqckt Apr 25 '11 at 08:53
  • no, that is not possible, only inside math you'll get an italic a. –  Apr 25 '11 at 09:18
  • 3
    @Herbert: With amsmath you can also say \smash[b]{g} to get zero depth and unmodified height (there's also \smash[t]{...}. – egreg Apr 25 '11 at 10:03
  • @Herbert Ehh, I just put the \smash in \DeclareMathOperator, that is, \DeclareMathOperator*{\Sup}{su\smash{p}} and use $\Sup$. While \DeclareMathOperator*{\Sup}{su\raisebox[0pt]{\height][0pt]{p}} is good. – Stufazi Hoqckt Apr 25 '11 at 11:33
  • @egred: I know ... there is also a [bt]. But this is not the problem here ... –  Apr 25 '11 at 13:20
  • @Stufazi Hoqckt: \DeclareMathOperator*{\Sup}{su\smash{p}} this is set in math mode and the one with \raisebox is set in text mode! With \textrm{\smash{p}} you also have it in text mode. –  Apr 25 '11 at 13:23
  • @Herbert Another solution! This is useful to me, thanks! – Stufazi Hoqckt Apr 25 '11 at 23:48
3

I don't know exactly what you want to do but if you want a special box with special dim then you can try :

\newbox\mybox
\setbox\mybox=\hbox{}
\ht\mybox=10mm \dp\mybox=0pt \wd\mybox=20mm

But If you put some text in your initial \hbox, you can have some strange results ! When you make a box with some contents, height, depth and width depend of the context.

Alain Matthes
  • 95,075