3

A while ago I asked the question how to write an integer variable into an aux file and read it: Write LaTeX3 variable to aux file and recover it

Now, I'm faced with an almost identical problem where I have to write/read a variable to/from the aux file but this time it is of type floating point. Have a look at the minimal example:

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn
\fp_new:N \g_maxmaier_foo_fp
\fp_new:N \l_maxmaier_bar_fp

\NewDocumentCommand{\setbar}{m}
 {
  \fp_set:Nn \l_maxmaier_bar_fp { #1 }
 }
\NewDocumentCommand{\showfoo}{}
 {
  \fp_to_tl:N \g_maxmaier_foo_fp
 }

\AtEndDocument
 {
  \iow_now:cx { @auxout }
   {
    \token_to_str:N \ExplSyntaxOn
    ^^J
    \fp_gset:Nn \g_maxmaier_foo_fp { \fp_to_scientific:N \l_maxmaier_bar_fp }
    ^^J
    \token_to_str:N \ExplSyntaxOff
   }
 }
\ExplSyntaxOff

\begin{document}

The value of \texttt{foo} is \showfoo

\setbar{42}

\end{document}

While executing LaTeX I get an error. If we look into the the aux file we find:

\relax 
\ExplSyntaxOn
\fp_gset:Nn \s__fp \__fp_chk:w 00\s__fp_exact ;{4.2e1}
\ExplSyntaxOff

The floating point number is correct but the variable name is screwed up. I would have expected an aux file of the following form:

\relax 
\ExplSyntaxOn
\fp_gset:Nn \g_maxmaier_foo_fp {4.2e1}
\ExplSyntaxOff

Why can't I use the variable name in this place as before when I used integers? Is there any suggested work around available?

Max Maier
  • 859

1 Answers1

4

For implementation reasons, the macro representing a floating point variable is not \protected against x expansion. Just tell LaTeX not to expand it.

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn
\fp_new:N \g_maxmaier_foo_fp
\fp_new:N \l_maxmaier_bar_fp

\NewDocumentCommand{\setbar}{m}
 {
  \fp_set:Nn \l_maxmaier_bar_fp { #1 }
 }
\NewDocumentCommand{\showfoo}{}
 {
  \fp_to_tl:N \g_maxmaier_foo_fp
 }

\AtEndDocument
 {
  \iow_now:cx { @auxout }
   {
    \token_to_str:N \ExplSyntaxOn
    ^^J
    \fp_gset:Nn \exp_not:N \g_maxmaier_foo_fp { \fp_to_scientific:N \l_maxmaier_bar_fp }
    ^^J
    \token_to_str:N \ExplSyntaxOff
   }
 }
\ExplSyntaxOff

\begin{document}

The value of \texttt{foo} is \showfoo

\setbar{42}

\end{document}

The contents of the aux file will be

\relax 
\ExplSyntaxOn
\fp_gset:Nn \g_maxmaier_foo_fp {4.2e1}
\ExplSyntaxOff
egreg
  • 1,121,712