3

How can I substitute following code with expl3 interfaces (that do the same job)?

\documentclass[varwidth]{standalone}
\begin{document}
  \newlength\spaceWidth
  \settowidth{\spaceWidth}{\ }
  \the\spaceWidth
\end{document}
  • 1
    your question title is about measuring boxes but if your actual goal is the example in the code then this is a very inefficient way to find the width of a space, that width is already available as a tex length it is fontdimen 2 of the current font. – David Carlisle May 24 at 6:45
6

There is nothing equivalent (that I know of), but you can implement your own (and by implement I mean copy from latex.ltx and change the syntax). I defined \SetToHeight, \SetToWidth, and \SetToDepth, that do the same as the LaTeX2e's variants.

However, for the specific case of a space, you can use \fontdimen2\font, which will get the width of a space in the current font without the need to measure a box, thus it is expandable, so you can use in dimension expressions. I defined an expl3y wrapper for that as well:

\documentclass[varwidth]{standalone}
\usepackage{expl3}
\ExplSyntaxOn
\box_new:N \l_bp_set_to_box
\cs_new_protected:Npn \bp_box_set_to:NNn #1 #2 #3
  {
    \hbox_set:Nn \l_bp_set_to_box {#3}
    \dim_set:Nn #2 { #1 \l_bp_set_to_box }
    \box_set_eq:NN \l_bp_set_to_box \c_empty_box
  }
\cs_new_protected:Npn \bp_set_to_height:Nn { \bp_box_set_to:NNn \box_ht:N }
\cs_new_protected:Npn \bp_set_to_depth:Nn  { \bp_box_set_to:NNn \box_dp:N }
\cs_new_protected:Npn \bp_set_to_width:Nn  { \bp_box_set_to:NNn \box_wd:N }
% Expandable width of a space:
\cs_new:Npn \WidthOfSpace { \tex_fontdimen:D 2 \tex_font:D }
\ExplSyntaxOff
\begin{document}
\ExplSyntaxOn
  \dim_new:N \spaceWidth
  \bp_set_to_width:Nn \spaceWidth {\ }
  \dim_use:N \spaceWidth
  \par
  \dim_use:N \WidthOfSpace
\ExplSyntaxOff
\end{document}

Output:

enter image description here

  • Why \__? These would need to be public – Joseph Wright May 24 at 6:06
  • 1
    I wouldn't use \l_tmpa_box. – egreg May 24 at 6:24
  • @JosephWright Only the \SetToSomething, no? Why does \__bp_box_set_to:NNn need to be public? – Phelype Oleinik May 24 at 9:00
  • @PhelypeOleinik Because of the document/code level split: all document commands should be defined in terms of documented code-level ones. – Joseph Wright May 24 at 9:13
  • 1
    @PhelypeOleinik Model is: document command defines user interface, this is implemented by code-level commands which start \module_... (so should be documented), and then they can use \__module... commands which are internal and thus 'no use'. – Joseph Wright May 24 at 9:20
2
\coffin_new:N\spaceCoffin
\hcoffin_set:Nn\spaceCoffin{\ }
% space width
\dim_eval:n{\coffin_wd:N\spaceCoffin}

\box_new:N\spaceBox
\hbox_set:Nn\spaceBox{\ }
% space width
\dim_eval:n{\box_wd:N\spaceBox}
  • 2
    You can, sure, use a coffin, however the computational overhead is much larger than for a simple box (as you edited in just now :-) – Phelype Oleinik May 24 at 0:23

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.