6

I'm curious how does \vrule determine the height of the current box?

\documentclass{article}
\usepackage{amsmath}

\[
\Big(
    ~
    \vrule width.7pt
    ~
\Big)
\]

enter image description here

this image was created by stitching together different ones after compiling with each of \Bigg, \Big and \big delimiters.

How can I retentive the height of the current box as a dimension to use for my needs?

In sake of simplicity, lets consider the following: make the same rule with \rule[]{}{} command that has height as a mandatory argument. And I want it to produce the same rule as \vrule width.7pt by setting some kind of arguments that contain dimension of the current box

\rule[<current depth>]{.7pt}{<current height>}

But the actual need for that is to draw a curved line using tikz that would have a height of the current box.

enter image description here

this one has been created simply by eyeballing

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}

\[
\Big(
    \,
    \tikz[overlay]\draw (0,-9pt) to[in=10] (5pt, 16.5pt);
    \hspace{2ex}
\Big)
\]

Note:

The main requirement is to avoid saving a chunk of code with the box beforehand. The goal -- make it inline as \vrule does.

5
  • For one thing, there is not just one, but many temporary boxes, given by numbers, starting with 0. A given macro or primitive will typically choose one of the temporary boxes for use. Commented Jul 17, 2022 at 14:38
  • @StevenB.Segletes what are those? Can I access them to make one of described examples in the post possible?
    – antshar
    Commented Jul 17, 2022 at 14:42
  • For example, \setbox0=\hbox{...} or \setbox0=\vbox{...} to set contents. Then, \ht0, \dp0, and \wd0 can retrieve height, depth, and width. \box0 will destructively typeset the box, whereas \copy0 will non-destructively set it. Register numbers range 0-255. Even numbers are local box registers, odd numbers are global box registers. If I recall, 255 is reserved for special use. Commented Jul 17, 2022 at 15:00
  • @StevenB.Segletes Yes, \box255 is a \vbox where the TeX page builder stores the page body once the best page break has been found, ready for use by the \output routine (cf. TeXbook p. 125, plus the chapter about \output routines).
    – frougon
    Commented Jul 17, 2022 at 16:57
  • @StevenB.Segletes but only box registers less than 10 should be used without allocation so only 0,2,4,6,8 for local assignments or 1,3,5,7,9 for global Commented Jul 17, 2022 at 20:59

2 Answers 2

10

It's built in TeX. The command \vrule is a primitive that accepts dimensions specified via keywords

\vrule width <len> height <len> depth <len>

The keywords can appear in any order and even multiple times, the last specification wins.

The command \vrule has another property: it's a horizontal command, meaning that as soon as TeX scans it, it will switch to horizontal mode (making a paragraph) if it was in vertical mode; used in math mode it doesn't switch, but just typesets a rule.

In either case (horizontal or math mode) the \vrule will end up inside a horizontal box (maybe a line of a paragraph) and this is important to answer your question. If the dimensions of a \vrule are underspecified, some defaults are applied:

  • the default for width is 0.4pt
  • the default for height is the height of the enclosing \hbox
  • the default for depth is the depth of the enclosing \hbox

This explains why

$\Bigl(\vrule\Bigr)$

will typeset a rule 0.4pt wide (thick, if you prefer) and as high and deep (above and below the baseline) as the formula, in this case the parentheses. You'd get the same dimensions with

$\Big|\vrule$

These default values are not available, because they're determined at typesetting time, when the user has lost any control on the process.

For your problem, you can measure a box:

\sbox0{Some text you want to measure height and depth of}

will provide the height as \ht0 and the depth as \dp0.

You can also allocate your own box register (it's better if the usage is not occasional): in the preamble you state

\newsavebox{\mybox}

(or whatever unused name you deem fit) and after doing

\sbox\mybox{Some text you want to measure height and depth of}

you can use \ht\mybox and \dp\mybox in any context where lengths are expected, for instance a \dimexpr.

2
  • In the "Note" paragraph I mentioned that saving box by copying the content beforehand isn't the way I'm looking for. Maybe there is some kind of a workaround that could grab all the content of the current box and save it automatically for further usage?
    – antshar
    Commented Jul 17, 2022 at 16:03
  • @antshar No, sorry.
    – egreg
    Commented Jul 17, 2022 at 16:04
3

egreg gave excellent explanations regarding \vrule; I won't attempt to duplicate this effort.

With the technique showed here, it is possible to obtain the depth, height and therefore also total height of the part of the formula you are interested in without any additional boxing, but this requires two compilation runs. I propose to do this using the tikzmark TikZ library. (Actually, I don't know how this library is implementated; maybe there is some boxing in this approach, but if so, it is completely automated.)

Beware however that the \tikzmarknode appears as a \mathchoice item in the math list; therefore, the spacing with respect to surrounding math items may be affected. However, you could wrap it inside \mathopen{...} and make it followed by \mathclose{} for instance, in order to adjust the spacing behavior according to the node contents.

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{calc, tikzmark}

\begin{document}

\[
  \tikzmarknode{paren}{\Bigl( \mkern 18mu \Bigr)}
  %
  \begin{tikzpicture}[remember picture, overlay]
    \fill[red] (paren.north) circle[radius=1pt];
    \fill[blue] (paren.south) circle[radius=1pt];

    \draw (paren.south) to[in=10] (paren.north);

    \path let \p{north} = (paren.north),
              \p{base} = (paren.base),
              \p{south} = (paren.south),
              \n{totalheight} = {\y{north} - \y{south}},
              \n{depth} = {\y{base} - \y{south}} in
      \pgfextra{%
        \xdef\parenTotalHeight{\n{totalheight}}%
        \xdef\parenDepth{\n{depth}}%
      };
  \end{tikzpicture}
  %
  \typeout{The total height of the parenthesis is \parenTotalHeight.}% 18.40015pt
  \typeout{The depth of the parenthesis is \parenDepth.}             % 6.70009pt
  %
  \textcolor{green}{%
    \raisebox{-\parenDepth}{%
      \rule{0.4pt}{\parenTotalHeight}%
    }%
  }
\]

\end{document}

enter image description here

Note: since the coordinates are read from the .aux file, it is quite possible to draw the green rule before the parentheses—even use the coordinates immediately after \begin{document}.

3
  • It's not an inline approach, you have to surround the whole formula to take into account the whole height, so basically, it doesn't differ much from saving the box beforehand. However I still appreciate you spending time on an attempt, upvote.
    – antshar
    Commented Jul 18, 2022 at 8:11
  • The main advantage IMHO is ease of use because 1) all anchors of a TikZ rectangle node are readily available and 2) the info is read from the .aux file, therefore available anywhere in the document. What you are asking for is strictly impossible given how TeX works: AFAIUI, you want to have a length accessible by macro code, however such code lies in the input stream and the length you want is undetermined until the current horizontal box (possibly math as egreg explained) has been finished. IOW, it depends on yet unread contents of the input stream when your code would be executed.
    – frougon
    Commented Jul 18, 2022 at 14:19
  • Since TeX consumes the input stream without ever going back, the requirements are unsatisfiable, as far as I can tell.
    – frougon
    Commented Jul 18, 2022 at 14:19

You must log in to answer this question.

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