Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expl3 variables not expanded #52

Open
Estorva opened this issue Dec 15, 2024 · 1 comment
Open

Expl3 variables not expanded #52

Estorva opened this issue Dec 15, 2024 · 1 comment

Comments

@Estorva
Copy link

Estorva commented Dec 15, 2024

I use expl3 commands to generate tikz figures. I was trying to cache these tikz figures and I found out that expl3 variables are not expanded in tikzpicture environment when I use robust-externalize. MWE:

\documentclass{article}
\usepackage{tikz}
\usepackage{robust-externalize}
\cacheTikz

\usepackage{expl3}
\ExplSyntaxOn
\NewDocumentCommand{\myFig}{m}{
  \tl_set:Nn \l_tmpa_tl {#1}
  \begin{tikzpicture}
    \node (A) at (0,0) {\tl_use:N \l_tmpa_tl};
  \end{tikzpicture}
}
\ExplSyntaxOff

\begin{document}
\myFig{Foo}
\end{document}

The compilation fails because \tl_use:N \l_tmpa_tl is not expanded.

(robExt)                failed with errors:
(robExt)                vvvvvv
(robExt)                ! Undefined control sequence.
(robExt)                15   \begin {tikzpicture}\node (A)at(0,0){\tl
(robExt)                                                              
(robExt)                _use:N \l_tmpa_tl };\end {tik...
(robExt)                !  ==> Fatal error occurred, no output PDF file
(robExt)                produced!
(robExt)                Transcript written on
(robExt)                robExt-D5DF5E4E0CE8EC4C4E65E20E3EA09C13.log.
(robExt)                
@tobiasBora
Copy link
Contributor

tobiasBora commented Dec 15, 2024

So here you have two issues, coming, I think, from the same mis-understanding: you have to remember that this library is not magical, what it does is quite simple:

  1. it takes the code to cache (here everything around the tikzpicture) and put it into a .tex file with some code to compute its depth etc
  2. it compiles this file and include back the pdf (with the proper depth)

You can inspect the file by inspecting robustExternalize/robExt-D5DF5E4E0CE8EC4C4E65E20E3EA09C13.tex (the hash is given in the error message). Here you realize 2 things:

  1. first the line \tl_set:Nn \l_tmpa_tl {#1} is not present… which makes sense since it was before the tikzpicture
  2. similarly, you can't see \ExplSyntaxOn… same reason, it was before the tikzpicture.

The fix is quite simple: put all the needed code in the tikzpicture. I tried to do:

\documentclass{article}
\usepackage{tikz}
\usepackage{robust-externalize}
\cacheTikz

\usepackage{expl3}
\ExplSyntaxOn
\NewDocumentCommand{\myFig}{m}{
  \begin{tikzpicture}
    \ExplSyntaxOn
    \tl_set:Nn \l_tmpa_tl {#1}
    \node (A) at (0,0) {\tl_use:N \l_tmpa_tl};
    \ExplSyntaxOff
  \end{tikzpicture}
}
\ExplSyntaxOff

\begin{document}
\myFig{Foo}
\end{document}

and it worked as expected.

In some cases, the macros used inside the function were defined much before, so you cannot easily add them inside you picture. That's why I created the notion of autoForward, that detects if a macro (marked as forwardable) is present, and if so copy backs its definition in the file. But at least with you current MWE this is not needed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants