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?


2 Answers 2


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

% Define a dataset that will store our key-value tables


    % 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.
    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]


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:


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

In the document


and later in the document:


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
    Commented May 11, 2012 at 19:43
  • 1
    @Marco: Thanks! I did not know ConTeXt was such a different TeX. I have added an according "header"-note to my answer. OK?
    – Stephen
    Commented May 12, 2012 at 18:53
  • 1
    +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
    Commented May 12, 2012 at 20:43
  • @Daniel: OK, done!
    – Stephen
    Commented May 18, 2012 at 17:17

You must log in to answer this question.

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