Suppose I typeset some text using an environment that places it in a box with a width that I specify. (I don't really care if this is a minipage, tabular, or something else.) I want to write the coordinates of the box to a file. (I will probably use xetex, because this involves Greek text, and that seems too painful in the other tex implementations I've tried.) In pdftex and its extensions such as xetex, I can use \pdfsavepos at the beginning and end of the text, and use write18 to write the coordinates. But I think this won't really give me the coordinates of the upper-left and lower-right coordinates of the box. I imagine it will record the positions of a point on the baseline (?) just before the first character and just after the last character.

Is there a way to output the actual coordinates of the box, or do I have to somehow estimate this by doing arithmetic based on the width of the box, the ascender and descender height of the font, and possibly the box/environment's margins? If I have to do the arithmetic muself, is there some convenient, automated way to find out the relevant font dimensions for whatever font is being typeset?

  • write18 to write the coordinates? that's shell escape? you probably want to do arithmetic but that will not be an estimate you can get the exact measurements, you just need to save the reference point of the box, you know its height, width and depth Apr 6 '21 at 16:13
  • 1
    that is if you have the text in box \foo you have available \ht\foo \dp\foo and \wd\foo, you do not need any font information. Apr 6 '21 at 16:14
  • @DavidCarlisle: Thanks, your second comment seems to be sort of the answer I need, except that I don't understand it. I'm not clear what you mean by "box \foo". Normally boxes are anonymous data structures, aren't they? Are these things like \ht, \dp, and \wd documented somewhere? Do they tell me the box's position on the page, or only its dimensions? Getting a precise position (what you call a reference point) on the page is one of the things I'm I'm asking about.
    – user6853
    Apr 6 '21 at 17:10
  • no: tex has box registers made with \newbox (\newsavebox` in latex). I'll post an answer. "reference point" is the tex terminoligy in the texbook (and sources) not mine. Apr 6 '21 at 17:56

You can place content in a box register then access its width height and depth along with the position from \pdfsavepos (which takes multiple runs of latex)

enter image description here



  \ifcsname zz#1\endcsname\else
  \expandafter\gdef\csname zz#1\endcsname{{}{}{}{}{}}%
  \gdef\expandafter\string\csname zz#1\endcsname{%
  position (#1sp,#2sp)\par width #3, height #4, depth #5


A paragraph with no interest.

and here we use the box \fbox{\usewithpos{a}{\zzz}}


and a second box \fbox{\usewithpos{b}{\zzz}}.

The first box is:


The second box is:


  • Thanks for taking the time to write up an example. That's very helpful. Is there any documentation I can read about \wd, \ht, and \dp? It's not apparent to me what "depth" means in this context.
    – user6853
    Apr 6 '21 at 18:34
  • the texbook is the offical source but tex by topic a good feee alternative (texdoc texbytopic but I'm sure we have answers on site about how tex considers box dimensions I'll see if I can find a link @BenCrowell Apr 6 '21 at 18:36
  • 1
    @BenCrowell this is a good answer tex.stackexchange.com/a/40985/1090 Apr 6 '21 at 18:38
  • I see. Thanks, that's very helpful. It looks like there is a margin in the box that is not included in the measurements.
    – user6853
    Apr 6 '21 at 18:50
  • 1
    @BenCrowell yes the fbox was not part of the saved box (it could be) and adds fboxsep and the width fboxrule of the rule, the measurement is of the text placed with \usebox the fbox is just for show. Apr 6 '21 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy