4

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)}
}

\NewDocumentCommand\uselist{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?

6

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.

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.