5

I'm new to lualatex, which looks phenomenal. I wonder what the easiest way is to access the dimensions of a box from within lualatex. I intend to use it in more complicated situations than this (as part of lua chunks) so I'm not looking for a hack within latex that feeds the dimensions to the lua chunk. I'm aware of the limitations of \directlua. Is this something that should be done using the node library? How?

\documentclass{minimal}

\usepackage{luacode}


\newcommand{\measureme}[1]{%
\directlua{-- what should go here to get the size of the box?}%
}

\begin{document}

The size is \measureme{this box}.


\end{document}
  • 1
    You mean the analog of \settowidth{\alength}{this box}? – egreg Jul 12 '14 at 11:42
  • Thank you. That would be useful, also, but I'm looking for something more general than that, say something analogous to \settoboxwidth of the settobox package. – JPi Jul 12 '14 at 12:02
  • I want to assign the sizes of boxes to variables in lua so I can compute their optimal placement. – JPi Jul 12 '14 at 12:15
  • 2
    see tex.stackexchange.com/a/65601/2891, \gensavebox command in particular, it does something similar to what you want – michal.h21 Jul 12 '14 at 13:06
  • 3
    @JPi “Is there a way of doing this for boxes generated in lua” -- Each hlist and vlist node has width, height and depth attributes. See the manual, section 8.1.2. – Philipp Gesang Jul 12 '14 at 14:16
6
\documentclass{article}

\makeatletter
\let\pc\@percentchar
\makeatother
\newbox\zzz


\newcommand{\measureme}[1]{%
\sbox\zzz{#1}%
\typeout{from TeX ht:\the\ht\zzz, 
                  dp:\the\dp\zzz,
                  wd:\the\wd\zzz}%
\directlua{
local n = tex.getbox('zzz')
print(string.format("from Lua ht:\pc fpt, dp:\pc fpt, wd:\pc fpt",
  n.height/65536,
  n.depth/65536,
  n.width/65536 ))
}}

\begin{document}

The size is \measureme{this box}.


\end{document}

demonstrates accessing box dimensions from tex and Lua, it logs:

from TeX ht:6.94444pt, dp:0.0pt, wd:35.33342pt
from Lua ht:6.944443pt, dp:0.000000pt, wd:35.333420pt

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.