![「非法計量單位(插入 pt)」 foreach 問題](https://rvso.com/image/392319/%E3%80%8C%E9%9D%9E%E6%B3%95%E8%A8%88%E9%87%8F%E5%96%AE%E4%BD%8D%EF%BC%88%E6%8F%92%E5%85%A5%20pt%EF%BC%89%E3%80%8D%20foreach%20%E5%95%8F%E9%A1%8C.png)
我正在嘗試製作一系列帶有漸變的圓圈。在創建的第二頁上,只有 /2pt/1pt/2pt,最後一個為 /20pt。我猜我的 foreach 迴圈有問題,但我不知道是什麼。
\documentclass[tikz]{standalone}
\usepackage{xcolor}
\definecolor{myyellow}{cmyk}{0,0,10,0}
\begin{document}
\foreach \Radius/\j in {6.0/1, 6.05/2,...,8.0/42}
{
\begin{tikzpicture}[scale=.5]
\pgfmathsetmacro\k{\j*10}
\useasboundingbox[fill=black] (-8.2,-8.2) rectangle (8.2cm,8.2cm);
\fill[fill=myyellow!\k] (0,0) circle (\Radius);
\end{tikzpicture}
}
\end{document}
錯誤:
Illegal unit of measure (pt inserted).
<to be read again>
/
l.15 }
答案1
您需要使用整數作為顏色分數,並且可以選擇count
在這種情況下使用。
\documentclass[tikz]{standalone}
\usepackage{xcolor}
\definecolor{myyellow}{cmyk}{0,0,10,0}
\begin{document}
\foreach \Radius [count=\j] in {6.0, 6.05,...,8.0}
{
\begin{tikzpicture}[scale=.5]
\pgfmathtruncatemacro\k{\j*10}
\useasboundingbox[fill=black] (-8.2,-8.2) rectangle (8.2cm,8.2cm);
\fill[fill=myyellow!\k] (0,0) circle (\Radius);
\end{tikzpicture}
}
\end{document}
答案2
這樣做其實沒有意義myyellow!420
:從 100 開始的所有值都會產生相同的顏色。
這裡,隨著步驟 2 的變化,梯度從 2 變成 84。
\documentclass[tikz]{standalone}
\usepackage{xcolor}
\definecolor{myyellow}{cmyk}{0,0,10,0}
\begin{document}
\foreach \x in {2,4,...,84}
{
\begin{tikzpicture}[scale=.5]
\pgfmathsetmacro\Radius{(\x-2)/41+6}
\useasboundingbox[fill=black] (-8.2,-8.2) rectangle (8.2cm,8.2cm);
\fill[fill=myyellow!\x] (0,0) circle (\Radius);
\end{tikzpicture}
}
\end{document}