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

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}
markings,% switch on markings
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]
\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 %
\rlap{%
\loop
\setbox0\hbox{\randdot\ }%
\ifdim\dimen@>\z@
\box\z@
\repeat
}%
\fi
\hfill