
我想創建一個Tikz 圖,其中使用數字向量包含圖片。我希望使用當前值縮放圖片,以便較高值的圖片y-axis
比低值的圖片更大。不知怎的,我總是收到錯誤“非法測量單位(插入 pt)”當嘗試在寬度參數中使用我的變數時。這就是我到目前為止所做的:
\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}
答案1
\includegraphics
與 pgf 不同,它不會解析其鍵的參數,因此您需要解析。
\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}
順便說一句,\x
也\y
被圖書館使用calc
。所以我個人可能會使用
\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}
calc
如果我以後決定在循環中使用,為了安全起見。
答案2
可以提前foreach
循環計算並避免\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}