「非法計量單位(插入 pt)」 foreach 問題

「非法計量單位(插入 pt)」 foreach 問題

我正在嘗試製作一系列帶有漸變的圓圈。在創建的第二頁上,只有 /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}

相關內容