4

I have a number of commands that work a lot like the \ref command in that its true typeset value is only defined after an initial pass is performed. In the first pass they typeset ? - like the \ref command, and in the second pass that ? is instead a number or some string.

It would be nice if I could use these commands inside a tikzpicture environment as a parameter. Obviously, tikz errors out when it gets the LaTeX command, and even if I can get the command to expand, it still errors due to tikz trying to understand the ? on the first pass - thus breaking the compile before it can build the correct value for a second pass.

Is there a way to generically redefine a command (or tikz elements I guess?) so such commands still executes as normal on the first pass (a \ref command waits for a corresponding label to get defined so it has a value, but a generic command may execute some TeX like writing to an external file or change some settings/variables in the compile which it would still need to do on the first pass before it was defined in a way to be used in the tikzpicture env on the second pass), but it doesn't generate an error in tikz in the first pass (or the error is ignored/doesn't stop compile) so that the command gets a chance to be defined for the second pass - then have tikz actually use the command output on the second pass once it is correctly defined?

Below is a MWE for what I am looking for, using a \label and \ref combo as a simple version of what I am after, but it would be ideal if it could work on generic commands that expand into arbitrary tikz code.

\documentclass{article}
\usepackage{tikz}

\begin{document}
\setcounter{section}{6}
\section{Introduction}\label{temp}
\ref{temp}% Should display 7

\begin{tikz}% Should display a 7 radius circle
    \draw circle(\ref{temp}pt);
\end{tikz}

\setcounter{section}{13}
\section{Intro2}\label{temp2}

\ref{temp2}% Should display 14
\begin{tikz}% Should display a 14 radius circle
    \draw circle(\ref{temp}pt);
\end{tikz}

\end{document}

EDIT FOR MORE INFO

The primary use case I want to apply this to is the \sage{arg} command, which on the first pass outputs a ? and writes out to an external file. That file is then processed before a second LaTeX pass. On the second pass, the \sage{arg} command grabs the definition of arg from the external file, and then the \sage{arg} command expands as the definition given by the external file.

The actual particulars of sage itself aren't really important (I think), it's more that I want to have a normal LaTeX command get run inside a tikz environment, but without stopping the compile because its initial output is just a ?. But the first pass generates something so that on the second pass the command expands into tikZ parseable code, so that in the second pass it will then actually be used in tikzpicture correctly.

In particular, I would actually expect any solution for this, to also work for \label and \ref, assuming that \ref is a number or somehow a string that tikz should be able to parse normally.

0

1 Answer 1

5

You can use “properties” (texdoc ltproperties).

\documentclass{article}
\usepackage{tikz}

\NewProperty{jason}{now}{0}{\mynumber}
\newcommand{\setmynumber}[2]{%
  \def\mynumber{#2}%
  \RecordProperties{#1}{jason}%
}

\begin{document}

\setmynumber{foo}{10}

\begin{tikzpicture}
\draw (-1,-1)--(1,1);
\draw (0,0) circle(\RefProperty{foo}{jason}pt);
\end{tikzpicture}

\end{document}

After the first run you get

first run

because the default value when the label hasn't been set yet, is 0. After the second run you get

next run

4
  • 1
    it looks a bit as if they want to retrieve a section number, so perhaps \NewProperty{secnum}{now}{1}{\number\value{section}} is the wanted property. Commented Nov 25 at 16:08
  • I'd want to be able to retrieve the contents of a variety of different commands, but each of them have dummy contents in the first pass. So the more accurate version I'd want to work in this context (I think?) would be: Instead of using the command \setmynumber{foo}{10}, I'd need to execute the equivalent to the command" \setmynumber{foo}{\ref{temp}} which doesn't seem to quite work - I assume because the \ref{temp} is not expanding or being read correctly by the \setmynumber command? Commented Nov 25 at 16:14
  • @Jason You don't want to use \label and \ref for this endeavour. You need to be a bit more specific, I'm afraid. Commented Nov 25 at 16:18
  • @egreg I added some further details, although I suspect that any solution that would work for my desired usecase, should also work with \label and \ref as they were used in this example. Commented Nov 25 at 16:28

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.