4

In LaTeX3, what are the differences - both practical and philosophical - between a private command taking no parameters and a local token list variable? In what situations should one be preferred over the other? Is there an accepted best practice?

As an example, suppose that I am defining a key to hold font switches such as \color{red}\itshape. Using a private command to store the font switches, I might define:

\cs_new:Npn \@@_font: { }

\keys_define:nn { @@ } {
  font .code:n = { \cs_set:Npn \@@_font: {#1} } ,
  font .value_required:n = true ,
  font .initial:n = ,
}

On the other hand, using a local token list variable, I might define:

\tl_new:N \l_@@_font_tl

\keys_define:nn { @@ } {
  font .tl_set:N = \l_@@_font_tl ,
  font .value_required:n = true ,
  font .initial:n = ,
}

Which of these two versions is preferred in LaTeX3?

  • 3
    The difference is in the concept, rather than in the implementation; both will be a parameterless macro, but the idea is that functions do something, variables store something. In this case, if font is used to define a font switch, a function is right; if it is used in other code, it should probably be a variable. – egreg Apr 20 '17 at 17:23
  • 2
    Depends imho on the use. A variable should be passed through a function: \module_use_font:n{\l_@@_font_tl}, as a standalone call it is a function. – Ulrike Fischer Apr 20 '17 at 17:25
  • 1
    @UlrikeFischer Probably \module_use_font:V \l_@@_font_tl – egreg Apr 20 '17 at 17:34
  • @TeXnician Done – egreg Oct 25 '17 at 11:13
3

This is a borderline case. The distinction between functions and variables is that

  • functions perform actions
  • variable store values

Changing the current font is an action, so in this case I'm more inclined towards a function.

The cleanest way to proceed would be something like

\cs_new_protected:Nn \@@_use_font:n { #1 }
\cs_generate_variant:Nn \@@_use_font:n { V }

\keys_define:nn { @@ }
 {
  font .tl_set:N = \l_@@_font_tl  ,
  font .value_required:n = true ,
  font .initial:n = ,
}

with

\@@_use_font:V \l_@@_font_tl

at point of use.

On the other hand, directly using a token list variable is rather common in existing expl3 code, for efficiency. Thus, for a case like this, it's more personal preference than strict respect of guidelines: the overhead introduced with \@@_use_font:V is negligible in this case, in other similar cases efficiency might make the choice of using the variable more attractive.

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.