# How do I use let in node in tikzpicture?

The code that I've tried to use is shown below. How do I make this work with pdflatex?

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,intersections,through,backgrounds,fit}
\begin{document}
\begin{tikzpicture}[framed]

\foreach \y in {0, 1, 2}{
\foreach \x in {0, 1, 2}{
\path
let
\n1 = {\x+random(2)},
\n2 = {\y+random(2)}
in
node at (\n1,\n2) [shape=circle,draw] {$p_{\x,\y}$};
}
}

\end{tikzpicture}
\end{document}

• Please post a minimal document, not just fragments – user31729 Jan 1 '17 at 7:57

I think you're bitten by the fact that inside a let block \x and \y can have a special meaning. E.g., if you say \p1=(3,2), then \x1 is 3 and \y1 is 2. I know you don't do that, but I think the definition of \x and \y change after let.

If you change them to \X and \Y your code works. But note that you don't actually need it here I think, you can do calculations directly in a coordinate specification, so you can say \node at ({\x+random(2)},{\y+random(2)}) ....

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}

\foreach \y in {0, 1, 2}{
\foreach \x in {0, 1, 2}{
\node at ({\x+random(2)},{\y+random(2)}) [shape=circle,draw] {$p_{\x,\y}$};
}
}

\end{tikzpicture}
\begin{tikzpicture}
\foreach \Y in {0, 1, 2}{
\foreach \X in {0, 1, 2}{
\path
let
\n1 = {\X+random(2)},
\n2 = {\Y+random(2)}
in
node at (\n1,\n2) [shape=circle,draw] {$p_{\X,\Y}$};
}
}
\end{tikzpicture}
\end{document}

• perfect! Tanks a lot! – aban Jan 1 '17 at 8:08