1

I have a situation where I need to access a variable which is defined after it is used in the text. An example of what I'm trying to achieve is below, essentially the definition of variable happens later in the latex document which obviously causes a compiler error.

\begin{document}
\Varirable
\renewcommand{\Varirable}{Updated}
\end{document}

Is there anyway I can somehow forward declare the variable and when it compiles it cross-references it so it can be updated with the correct value (which is defined at the end)?

F. Pantigny
  • 40,250

1 Answers1

2

You need to write it in the .aux file.

\documentclass{article}

\ExplSyntaxOn

% at point of definition, store in the aux file \NewDocumentCommand{\definevariable}{mm}{% % #1 = symbolic name, #2 = value \iow_now:cn { @auxout } {\setvariable{#1}{#2}} }

% the container for the variables \prop_new:N \g_tracey_variables_prop

% \setvariable populates the property list \NewDocumentCommand{\setvariable}{mm} { \prop_gput:Nnn \g_tracey_variables_prop { #1 } { #2 } }

% \usevariable uses (or prints ??) \NewExpandableDocumentCommand{\usevariable}{m} { \tl_if_empty:eTF { \prop_item:Nn \g_tracey_variables_prop { #1 } } { ?? } { \prop_item:Nn \g_tracey_variables_prop { #1 } } }

\ExplSyntaxOff

\begin{document}

\usevariable{mytext}

Some text

\definevariable{mytext}{Ööç}

\end{document}

First run:

enter image description here

Second run:

enter image description here

egreg
  • 1,121,712
  • That looks ideal!

    Although i get an error saying the line with \prop_new:N \g_tracey_variables_prop, needs to be in math mode.

    Any suggestions?

    – John Tracey Nov 09 '23 at 10:53
  • @JohnTracey You forgot \ExplSyntaxOn, I guess – egreg Nov 09 '23 at 11:02
  • I copy pasted it as above exactly including that bit.

    Also i'm using overleaf incase that matters? I do also get Undefined Control Sequence for \usevariable{mytext}. Compiles fine when i comment out that line.

    – John Tracey Nov 09 '23 at 11:05
  • @JohnTracey I guess it's a spurious warning from the editor. – egreg Nov 09 '23 at 13:03
  • I think the math mode one is but the undefined control sequence one is causing the code not to run. – John Tracey Nov 09 '23 at 13:11
  • @JohnTracey What undefined control sequence? – egreg Nov 09 '23 at 13:31
  • https://tex.stackexchange.com/a/416272/224969

    managed to get this example to work (also from you) but when I store my counter variable it doesn't seem to be working (counter is always 0) any suggestions?

    – John Tracey Nov 09 '23 at 14:44
  • @JohnTracey You said your variable is text, not a counter's value. – egreg Nov 09 '23 at 14:52
  • Yeah sorry I didn't appreciate that a counter and text was different in this context – John Tracey Nov 09 '23 at 16:29
  • If you want the value of a counter, why not using the built-in method with \labeland \ref? If you do \refstepcounter{mycounter}\label{foo}, then \ref{foo} will display the value. Can you be more specific about the context where you want to use this? – egreg Nov 09 '23 at 16:51
  • Yep that sorted it! I'm very new to counters I didn't know you could do this thanks! – John Tracey Nov 09 '23 at 18:18