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

  \settowidth{\spaceWidth}{\ }
  • 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 '19 at 6:45

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:

\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 }
  \dim_new:N \spaceWidth
  \bp_set_to_width:Nn \spaceWidth {\ }
  \dim_use:N \spaceWidth
  \dim_use:N \WidthOfSpace


enter image description here

| improve this answer | |
  • Why \__? These would need to be public – Joseph Wright May 24 '19 at 6:06
  • 1
    I wouldn't use \l_tmpa_box. – egreg May 24 '19 at 6:24
  • @JosephWright Only the \SetToSomething, no? Why does \__bp_box_set_to:NNn need to be public? – Phelype Oleinik May 24 '19 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 '19 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 '19 at 9:20
\hcoffin_set:Nn\spaceCoffin{\ }
% space width

\hbox_set:Nn\spaceBox{\ }
% space width
| improve this answer | |
  • 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 '19 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.