# Using calc-like (calc Package) Commands within LaTeX3/expl3

In my pursue to switch as much as possible from LaTeX2e code to LaTeX3/expl3, I got stuck with the usage of some of the calc package facilities. I've read of the l3calc package, but it seems it is currently deprecated. In particular, I'm running into trouble with the calc pkg command \widthof. What's the LaTeX3/expl3 way to proceed in this regard without declaring a box? I mean, something like \widthof{This text} chez LaTeX3/expl3.

To be more precise, I run into trouble with:

\documentclass{memoir}

%\usepackage{calc}

\usepackage{xparse}

\ExplSyntaxOn

\dim_new:N \g_tmp_dim
\dim_set:Nn \g_tmp_dim { \widthof{This ~ text} } % { 3pt } It works with this

\NewDocumentCommand \ShowWidth { }
{ \dim_use:N \g_tmp_dim }

\ExplSyntaxOff

\begin{document}
\ShowWidth
\end{document}


Maybe my trouble has something to do with the expansion of the n argument here and I should define a variant for the function \dim_set:Nn that manages the expansion in a different way? Just pondering over it...

-
There's nothing like \widthof in expl3; the function \dim_set:Nn wants a <dimension expression> as its second argument, which must be expandable; nothing like \widthof can be, because a box must be set for measuring it. – egreg Aug 27 '13 at 15:04
You could do something similar to what I did in my answer here: tex.stackexchange.com/questions/126476/… – clemens Aug 27 '13 at 15:30
@egreg: So the best we can do is defining our function to do this? It seems to me that it would be nice having this kind of functionality available as out-of-the-box tools from the LaTeX3 Kernel. I've always found very useful the calc-based commands returning a length (or dimension) without requiring to set up a box beforehand. – Marcos Aug 27 '13 at 16:01
@cgnieder Marcos ... the kernel is not complete but as far as I'm concerned the data type "dim" is. The goal of the core data types in expl3 is that they are fast and that rules out adding a parsing and intepretation layer to them that is triggered each time a "dim" operation happens. Just try \tracingall\setlength\textheight{100pt} - with calc 76 lines of tracing code without 5. That ratio roughly translates into speed (so a factor of 15-20). This is why the 2e kernel is carefully not using \setlength but lowlevel assignments. – Frank Mittelbach Aug 28 '13 at 20:13
@Marcos that doesn't rule out that a higher-level type will be provided as part of the designer interface but not as part of the programming interface. In fact there will be this kind of functionality on the designer level eventually. – Frank Mittelbach Aug 28 '13 at 20:16

Functions such as \dim_set:Nn need that their second argument, in this case a <dimension expression>, is fully expandable. A function working similarly to \widthof cannot return a dimension expandably, because a box for doing the measurement must be set.
This doesn't mean that user level macros that work similarly to the behavior of \setlength with calc cannot be defined; however a parser for the expression must be defined.
By the way, nothing prevents you from using \setlength (with calc) inside LaTeX3 code (although it would be bad style). Or, for simple applications, use code similar to cgnieder's answer to Macro to calculate \baselineskip for given font