4

I would like to draw a picture in which a line connects two points. Along this line, there should be a text "x", where x is the number of a specific equation in another document. Unfortunately, I am not able to realize this idea. So far, I have a main document (main.tex) and a figure document (figure.tex). I first compile main.tex, then I compile figure.tex. Using references as node labels in the figure seems fine. However, for text decorations they seem to totally lock up the pdflatex application. Here is a minimum working example:

main.tex

\documentclass{standalone}
\begin{document}

% the equation of interest
\begin{equation} \label{x}
  x = 42
\end{equation}

% the figure of interest
\includegraphics{figure.pdf}

\end{document}

figure.tex

\documentclass{standalone}
\usepackage{xr} % edit: this line and the next allow for cross-referencing
\externaldocument{main}
\usepackage{tikz}
\usetikzlibrary{decorations.text}
\begin{document}
\begin{tikzpicture}

\draw[] (0,0) node[] (A) {\ref{x}}; % this works
\draw[] (1,1) node[] (B) {x};       % this also works

\draw[decoration={text along path,text align={align=center},%
text={\ref{x}}},postaction={decorate}] (A) -- (B); % does not compile, i. e., pdflatex seems to be in an infinite loop

\draw[decoration={text along path,text align={align=center},%
text={x}},postaction={decorate}] (A) -- (B); % does compile, but is useless

\end{tikzpicture}
\end{document}

So my questions are as follows:

1.) (Solved?) How do I have to configure the compiling procedure to be able to use references from main.tex in figure.tex? Ideally, I would like to use externalization to produce the figures in figure.tex. This is my motivation for using two different files in the first place. Edit: I think I can answer this question for myself now using the answer to this question: By adding

\usepackage{xr} \externaldocument{main}

to figure.tex, referencing from figure.tex to main.tex should work! If this should be a bad style, please correct me. Question 2 remains unsolved though.

2.) How can I use a reference as a text decoration? I have no idea why this fails.

Jill
  • 41

2 Answers2

3

With or without the xr package, \ref{x} is expanded to 1\hbox {} that is not a correct content for text along path.

To get exactly 1, you may use the \getrefnumber macro provided by the refcount package (cf. How to compare the output of \ref to a number?).

Here is a working figure.tex file:

\documentclass{standalone}
\usepackage{refcount}
\usepackage{xr} % edit: this line and the next allow for cross-referencing
\externaldocument{main}
\usepackage{tikz}
\usetikzlibrary{decorations.text}
\begin{document}
\begin{tikzpicture}
  \draw[] (0,0) node[] (A) {\ref{x}};
  \draw[] (1,1) node[] (B) {x}; 

  \draw[postaction={
    decoration={text along path,text align={align=center},
      text={\getrefnumber{x}}},decorate}]
  (A) -- (B);
\end{tikzpicture}
\end{document}
Paul Gaborit
  • 70,770
  • 10
  • 176
  • 283
2

Your first question has been solved using xr package, as long as main.aux is in the same folder as figure.tex. For the second question, you can (ab-)use the character command key as follows.

First, you need the key text effects along path, so that each character is inserted in the picture as a TikZ node. Then use text effects/.cd to enable access to keys customizing text effects, in particular, the character command key. We define a macro \mycommand as \def\mycommand#1{\ref{#1}} to be used with this key. The character command=\mycommand key expands the TeX macro \mycommand taking each character in text={x} as its argument. In this case, there's only one character x, and so \mycommand{x} expands to \ref{x}. [If your label has more than one character, the same trick applies, but you'll need to add the key group letters, so that a string of letters are treated as one "character".]

Code

\documentclass{article}
\usepackage{tikz,mathtools}
\usetikzlibrary{decorations.text}
\begin{document}

\begin{equation}\label{x}
  a^2=b^2+c^2 
\end{equation}

\def\mycommand#1{\ref{#1}} % define character command

\begin{tikzpicture}

\draw[] (0,0) node[] (A) {\ref{x}}; % this works
\draw[] (1,1) node[] (B) {x};       % this also works

\draw[decoration={
    text effects along path,                          % new
    text align={center},
    text={x},
    text effects/.cd,                                 % new
    text along path,                                  % new
    group letters,                                    % new
    every word/.style={character command=\mycommand}  % new
  },
  postaction={decorate}
] (A) -- (B); 

\end{tikzpicture}

\end{document}

Output

enter image description here

Herr K.
  • 17,946
  • 4
  • 61
  • 118