TransWikia.com

How to understand correctly scope of local variable in LaTeX3?

TeX - LaTeX Asked on July 29, 2021

Common programming languages (C).

// foo is global scope
int foo = 0;

int main() {
    // bar is local scope within main
    int bar = 1;
}

Let’s consider LaTeX3

documentclass[]{article}

ExplSyntaxOn
cs_set:Nn create_list:n
{
    tl_set:Nn l_my_tl {(#1)}
}

NewDocumentCommanduselist{m}
{
    create_list:n {#1}
     tl_use:N l_my_tl
}
ExplSyntaxOff

begin{document}

uselist{2}

end{document}

In LaTeX3 I expected the scope of l_my_tl is a body of create_list:n. But l_my_tl visible also from uselist{m} macro. Why is that? What am I misunderstanding here?

One Answer

Scope in TeX, and thus in expl3, is not defined by macros: they are simply replaced by their definition when 'used'. Scope is therefore provided using explicit groups:

  • group_begin:/group_end: (expl3) - begingroup/endgroup (classical TeX names)
  • { ... } or bgroup/egroup

The brace-based version potentially impacts on math mode spacing, and in expl3 we always use the first type of grouping.


There are some 'hidden' scopes, such as table cells, but for the bulk of programming these are not what we use.

Correct answer by Joseph Wright on July 29, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP