# leaders and evaluation order

I made this simple latex file :

\documentclass{minimal}
\usepackage{tikz}
\newcommand\randdot{%
\tikz{\pgfmathsetmacro{\r}{0.01+0.05*random()}\fill (0,0) circle (\r);}\ }

\begin{document}

\randdot\randdot\randdot\randdot\randdot\randdot

Test \leaders\hbox{\randdot\ }\hfill 3 \hfill \

\randdot\randdot\randdot\randdot\randdot\randdot

\end{document}


The new command randdot typeset a dot of random size. The command works good, BUT when I use it in a leader, the dots are no more random. The leaders command must evaluate only once the randdot, and copy it...

How can I obtain a random leader, with each dot being evaluated differently ?

Thanks !!

-
Leaders simply repeat copies of the box, they don't rebuild it at every instance. – egreg Jul 22 '12 at 20:10
Would it be possible to know how much will TeX expand a hfill, perhaps from lua? If that were possible, the problem would be solved writting a TikZ loop which fill that given dimension with random dots. – JLDiaz Jul 22 '12 at 20:43

## 2 Answers

The TikZ way David mentions is the now-famous (thanks to Andrew Stacey and Peter Grill) \tikzmark macro. Basically you leave TikZ coordinates at places you like and then refer to them in a later TikZ picture.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\tikzset{randlead/.style={decoration={
markings,% switch on markings
mark=% actually add a mark
between positions 0 and 1 step 5mm
with
{
\pgfmathsetmacro{\r}{0.01+0.05*random()}\fill (0,0) circle (\r);
}
},
decorate
}
}
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture,baseline=-0.5ex] \node (#1) {};}

\begin{document}
Test\tikzmark{a1} \hfill \tikzmark{a2}3 \hfill

Test some text and then Test \tikzmark{a3} \hfill \tikzmark{a4}165 \hfill

\begin{tikzpicture}[overlay,remember picture]
\draw[randlead] (a2) -- (a1);
\draw[randlead] (a4) -- (a3);
\end{tikzpicture}
\end{document}


Further tweaks are possible but take this as a proof of concept.

-
This is very elegant. Why do you need to set the baseline to -0.5ex in the definition of the tikzmark ? – Xoff Jul 22 '12 at 22:00
@Xoff Well just cosmetics. It starts a little higher on the text line. Also you need two runs with this solution too. – percusse Jul 22 '12 at 22:01

There's probably a tikz way of getting the coordinates but I use the pdftex primitive here. Use glue instead of leaders, but measure the distance and overlay some boxes on a second run.

\documentclass{minimal}
\usepackage{tikz}
\newcommand\randdot{%
\tikz{\pgfmathsetmacro{\r}{0.01+0.05*random()}\fill (0,0) circle (\r);}\ }

\begin{document}
\makeatletter

\randdot\randdot\randdot\randdot\randdot\randdot

Test \leaders\hbox{\randdot\ }\hfill 3 \hfill \

Test %
\pdfsavepos\write\@auxout{\gdef\noexpand\leada{\the\pdflastxpos}}%
\ifx\leada\@undefined\else
\rlap{%
\dimen@\leadb sp
\advance\dimen@ - \leada sp
\loop
\setbox0\hbox{\randdot\ }%
\advance\dimen@-\wd\z@
\ifdim\dimen@>\z@
\box\z@
\repeat
}%
\fi
\hfill
\pdfsavepos\write\@auxout{\gdef\noexpand\leadb{\the\pdflastxpos}}%
4 \hfill \

\randdot\randdot\randdot\randdot\randdot\randdot

\end{document}

-
I like that solution, but I was wondering : you need at least 2 compilations to get the result. But are there some special cases where you need more to converge ? – Xoff Jul 22 '12 at 21:59
I don't think so (if the file isn't edited to change the leader stretch) the boxes are in an \rlap so take up zero space so have no effect on the typesetting of other item, so the second run adding the boxes should not. move anything else. (you could generate degenerate case which explicitly check with \lastboox if a box has been used and do something strange so requiring a third run, but not in any normal processing. – David Carlisle Jul 22 '12 at 22:08
I checked the other solution with tikz, because it's easier to use, but I like that one very much too :) – Xoff Jul 23 '12 at 9:08