1

I'd like to create a Tikz plot where a vector of numbers is used to include a picture. I want the picture to be scaled using the current value, so that the pictures higher on the y-axis are larger than those at low values. Somehow I always get the error "Illegal unit of measurement (pt inserted)" when trying to use my variable in the width argument. That's what I've done so far:

\begin{tikzpicture}
\foreach \y [count=\x] in {0.5, 0.7, 0.8, 0.9, 0.95, 1, 1.05, 1.07, 1.2, 1.205, 2, 3, 3.1, 5, 5.4, 5.8, 6.1, 6.15, 6.125, 6.4}
\node at(\x,\y) {\includegraphics[width=\y*0.2cm]{../figs/Potato.png}};
\end{tikzpicture}

2 Answers 2

1

\includegraphics does, unlike pgf, not parse the arguments of its keys, so you need to parse.

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}
\foreach \y [count=\x] in {0.5, 0.7, 0.8, 0.9, 0.95, 1, 1.05, 1.07, 1.2, 1.205, 2, 3, 3.1, 5, 5.4, 5.8, 6.1, 6.15, 6.125, 6.4}
\node at(\x,\y) {\pgfmathparse{\y*0.2}\includegraphics[width=\pgfmathresult cm]{example-image-duck}};
\end{tikzpicture}
\end{document}

enter image description here

BTW, \x and \y also get used by the calc library. So I personally would perhaps use

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}
\foreach \Y [count=\X] in {0.5, 0.7, 0.8, 0.9, 0.95, 1, 1.05, 1.07, 1.2, 1.205, 2, 3, 3.1, 5, 5.4, 5.8, 6.1, 6.15, 6.125, 6.4}
\node at(\X,\Y) {\pgfmathparse{\Y*0.2}\includegraphics[width=\pgfmathresult cm]{example-image-duck}};
\end{tikzpicture}
\end{document}

to be on the safe side if I later decide to use calc in the loop.

1
  • My answer explains why the error occurs, and avoids to introduce a new macro (in particular a macro that has a meaning in some packages).
    – user194703
    Dec 9, 2019 at 17:34
3

It's possible to advance the calculations on foreach loop and avoid \pgfmathparse

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}
\foreach \y [count=\x, evaluate=\y as \width using \y*0.2] in {0.5, 0.7, 0.8, 0.9, 0.95, 1, 1.05, 1.07, 1.2, 1.205, 2, 3, 3.1, 5, 5.4, 5.8, 6.1, 6.15, 6.125, 6.4}
\node at(\x,\y) {\includegraphics[width=\width cm]{example-image}};
\end{tikzpicture}
\end{document}

enter image description here

1
  • Well, this introduces a new macro, and also makes sure that \includegraphics does not have to parse what you feed it with, like my answer.
    – user194703
    Dec 9, 2019 at 17:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .