# How to retrieve a value which is set later on in the document?

According to What is the recommended way to assign a value to a variable and retrieve it for later use?, in ConTeXt, one can use \setvalue{name}{value} to set a value and \getvalue{name}, to retrieve that value later. This seems to only work if \setvalue is used before \getvalue is needed. How can I retrieve a value early on in a document, when the value is set much later in the document?

-
– Marco May 11 '12 at 11:14
Also worth reading: Context wiki - Multi-pass data – Marco May 11 '12 at 11:37

What Marco said: use ConTeXt's multi-pass data mechanism. Here's how.

% Define a dataset that will store our key-value tables
\definedataset[mydataset]

\starttext

\framed[
% Here we use \datasetvariable to retrieve the values of myheight and mywidth.
% Notice that we only define those values further on in the document.
% Notice also that we don't worry about the first pass: through some black magic,
% we don't get 'variable undefined' problems.
% Ah, the wonders of automatic multi-pass compiling.
height=\datasetvariable{mydataset}{mytable}{myheight},
width=\datasetvariable{mydataset}{mytable}{mywidth}
]{
This is my text in a framed box
}

% Observe how we define myheight and mywidth here at the end,
% *after* having already invoked their values.
\setdataset[mydataset][mytable][myheight=3cm, mywidth=3cm]

\stoptext


Typically myheight and mywidth would depend on the height or width of some other object in the document, but I can't be bothered to muck around with boxes for this example.

-

The OP asked for a ConTeXt solution, which is provided by Esteis (only works with ConTeXt). For "overkill" here a non-ConTeXt solution (only works without ConTeXt):

In the document's preamble:

\setvalue{name}{1}


or some other "harmless" value, just to have the value set for the first compilation run.

In the document

\getvalue{name}


and later in the document:

\makeatletter%
\immediate\write\@mainaux{\string\setvalue{name}{value}}%
\makeatother%


which writes \setvalue{name}{value} into \jobname.aux. (The \string is necessary, because the \setvalue command shall be written and not executed, e.g. \string\thepage writes \thepage, while \thepage would write the page number, for example 49.) When the document is compiled for the next time, the .aux file (this time containing the \setvalue{name}{value}) is processed after \setvalue{name}{1} at the beginning of the document (really immediately before the content of \AtBeginDocument{...}), and \setvalue{name}{value} is executed (before \getvalue{name}). The .aux file is also processed between \AtEndDocument{...}'s content and \end{document}, but this has no effect for this case here.

-
-1 Context neither uses an aux file, nor supports the macros you mention. – Marco May 11 '12 at 19:43
@Marco: Thanks! I did not know ConTeXt was such a different TeX. I have added an according "header"-note to my answer. OK? – Stephen May 12 '12 at 18:53
+1 for the most condensed explanation on how to use the .aux file mechanism. :-) You might want to describe a bit more precisely, when exactly the .aux file is processed. – Daniel May 12 '12 at 20:43
@Daniel: OK, done! – Stephen May 18 '12 at 17:17