3

Here is a simple equation $$ \int_a^b f(x) \, dx $$ how do I calculate the box height? How do I round to the nearest multiple of 12pt ?

1 Answer 1

5

You could use the \settoheight and \settodepth macros, which take two arguments, to measure the height above and depth below the baseline. For Computer Modern fonts and a basic document font size of 10pt, the total heights of the integral formulas work out to be 25.1pt and 14.0pt for display style and text style, respectively.

To force the heights of the formulas to either 24pt (in the display style case) or 12pt (in the text style case), you could use a \resizebox* directive. Note: don't use \resizebox, use \resizebox*.

enter image description here

\documentclass[10pt]{article}
\newlength\bxheight
\newlength\bxdepth
\newlength\bxtot
\newcommand\myint{\int_a^b f(x)\,dx} % store the formula (it gets used a lot)
\usepackage{graphicx} % for \resizebox macro
\usepackage{amsmath}  % for \text macro
\usepackage{booktabs}
\begin{document}

\settoheight{\bxheight}{$\displaystyle\myint$}
\settodepth{\bxdepth}{$\displaystyle\myint$}
\setlength\bxtot{\dimexpr\bxheight+\bxdepth\relax}
$\displaystyle\myint$ has height \the\bxheight, depth \the\bxdepth, and total height \the\bxtot.

\medskip
\settoheight{\bxheight}{$\myint$}
\settodepth{\bxdepth}{$\myint$}
\setlength\bxtot{\dimexpr\bxheight+\bxdepth\relax}
$\myint$ has height \the\bxheight, depth \the\bxdepth, and total height \the\bxtot.

\[
\begin{array}{@{}lcc@{}}
& \text{natural size} & \text{rounded to nearest } \\
& & \text{multiple of 12pt} \\
\midrule
\text{display style} & \displaystyle\myint & \resizebox*{!}{24pt}{$\displaystyle\myint$} \\[3ex]
\text{text style}    & \myint              & \resizebox*{!}{12pt}{$\myint$}
\end{array}
\]
\end{document}
4
  • I wonder if it is possible to typeset the equation only once in an \hbox and use height of the box \the\ht to measure the height of equation (instead of using \settoheight)? When I try that, I get \hbox height & depth is 0pt.
    – codepoet
    Commented Apr 26, 2020 at 9:15
  • @reportaman - I'm not sure I understand the gist (or purpose) of your comment. For sure, the instruction \newcommand\myint{\int_a^b f(x)\,dx} ends up (eventually...) placing \int_a^b f(x)\,dx in an \hbox. Executing the three instructions \settoheight{\bxheight}{$\myint$} \settodepth{\bxdepth}{$\myint$} \setlength\bxtot{\dimexpr\bxheight+\bxdepth\relax} as a single instruction, say via \def\docalcs{\settoheight{\bxheight}{$\myint$} \settodepth{\bxdepth}{$\myint$} \setlength\bxtot{\dimexpr\bxheight+\bxdepth\relax}}, is straightforward. Please let me know if I missed something.
    – Mico
    Commented Apr 26, 2020 at 9:25
  • I was looking it from the point of efficiency. If a math equation is put in a \hbox first, and tex builds that \hbox (upon executing \setbox) & if we can query its height & depth then the equation need not be "built" three times by tex... I used the term typeset instead of built.
    – codepoet
    Commented Apr 26, 2020 at 9:32
  • @reportaman - I definitely wrote this answer as a one-off project. Economy (terseness?) was not an objective. Feel free to post a new answer that creates the same output but consumes fewer computing cycles.
    – Mico
    Commented Apr 26, 2020 at 10:29

You must log in to answer this question.

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