# LuaLaTeX: Calculate length in Lua

I apologize if this a rookie question, but I find it extremely hard to find a solution to my Lua problem using online searches and browsing reference manuals.

I would like to calculate a length: Divide \linewidth by \baselineskip, take the floor, and finally multiply with \baselineskip. The result should be returned in pt.

I already fail with getting \baselineskip into Lua such that I can do a calculation with it:

\documentclass{article}
\begin{document}
\begin{enumerate}
\item Linewidth works this way: \directlua{tex.sprint(tex.dimen['linewidth'] / 65536)tex.sprint('pt')}
\item and this way: \directlua{tex.sprint(tex.dimen.linewidth / 65536)tex.sprint('pt')}
\item \directlua{tex.sprint(tex.dimen['baselineskip'])}
\item \directlua{tex.print(tex.skip['baselineskip'].width)}
\item \directlua{tex.print(tex.skip['baselineskip'].stretch)}
\item \directlua{tex.sprint(tex.baselineskip)}
\end{enumerate}
\end{document}


So the questions are: How do I get \baselineskip into Lua, and how can I calculate and output the length I described above.

If someone happens to know a "real" beginner's guide to LuaLaTeX that also discusses such problems, I would appreciate a hint. Thanks!

## Solution

With the help of the answers below, the length above can be calculated as:

\directlua{tex.sprint(math.floor(tex.dimen.linewidth / tex.baselineskip.width)
* tex.baselineskip.width  / tex.sp('1pt') ..'pt')}

• @HenriMenke This causes an error: [\directlua]:1: incorrect dimen name stack traceback: [C]: in function 'getdimen' [\directlua]:1: in main chunk. l.9 ...lua{tex.sprint(tex.getdimen("baselineskip"))}. – FlorianL Apr 27 '17 at 19:37

You can use tex.baselineskip in Lua to get the baselineskip but a skip is a Lua userdata object (as it has stretch and shrink components as well as a natural length) so you can not directly use it in inline arithmetic Lua expressions with /

\documentclass{article}

\begin{document}

\directlua{
print('baseline is '  ..tex.baselineskip.width)
}
\end{document}


produces terminal output:

baseline is 786432

• Is there a way to convert it to a number that one can use in a calculation? Or does this mean it is not possible? – FlorianL Apr 27 '17 at 21:09
• @FlorianL - Just write tex.baselineskip.width/65536 if you want the length expressed in pt units instead of sp units. – Mico Apr 27 '17 at 21:10
• @FlorianL yes exactly the .width is a number what I meant is that tex.baselineskip itself is a userdata thing. The width field is just a normal Lua integer (786432 here) – David Carlisle Apr 27 '17 at 21:23
• You can also do tex.baselineskip.width/tex.sp("1pt") if you don't want to memorize that 1pt = 65536sp. – Henri Menke Apr 27 '17 at 21:50