4

Is there a way to simultaneously declare and initialize an expl3 integer variable?

Consider the following LaTeX code, which simultaneously declares and initializes a floating point variable.

\documentclass{article}
\begin{document}
\ExplSyntaxOn
\fp_gset:Nn\myvar{1}
\fp_use:N\myvar
\ExplSyntaxOff
\end{document}

When this code is compiled with Overleaf's 2022 LuaLaTeX engine, the compilation completes successfully and prints out 1.

However, if now the two occurrences of the string "fp" are replaced by "int", and the code is recompiled, the following error message results:

<argument> \myvar

l.4 \int_gset:Nn\myvar{1}

The control sequence at the end of the top line
of your error message was never \def'ed.

1 Answer 1

5

Sorry, but you're wrong. The instruction \fp_gset:Nn does not really declare the variable.

Indeed, if you run

\documentclass{article}
\usepackage[check-declarations]{expl3}
\begin{document}
\ExplSyntaxOn
\fp_gset:Nn\myvar{1}
\fp_use:N\myvar
\ExplSyntaxOff
\end{document}

which is recommended during code testing, you will see that compilation stops with

! LaTeX Error: The variable \myvar has not been declared on line 5.

For immediate help type H <return>.
 ...

l.5 \fp_gset:Nn\myvar{1}

By reasons of implementation, \fp_gset:Nn doesn't complain when the first argument hasn't been declared yet. The same happens for tl, clist or seq variables. But this doesn't mean that they don't need to be declared in advance: they should be anyway.

For integer variables it's a different thing, because they need to be assigned an internal register, so not declaring them in advance always produces an error.

Why the difference? Efficiency: overloading every <var>_(g)set:Nn function with the check about the variable being declared would slow processing too much.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .