在新命令中输入值会出现错误

在新命令中输入值会出现错误

因此,我尝试将一个值传递给名为 的新命令circleFig。但是,编译器一直尝试pt在其前面插入,这实际上破坏了代码。我如何才能强制编译器将输入视为实数,而不是它想要感知的数值?

\documentclass[convert]{standalone}
\usepackage{tkz-euclide}
\newcommand{\circleFig}[1]{
    \begin{tikzpicture}
    \tkzDefPoints{0/0/O, 3/0/P, 3/0/R}
    \tkzDrawCircle[line width=0.1pt](O,P)
    
    \foreach \i in {0,...,#1 - 1}
    {
        \tkzDefPointOnCircle[angle=360*\i /#1 ,center=O,radius=3]
        \tkzGetPoint{P}
        \tkzDefPointOnCircle[angle=360*\i /#1 + 360/#1,center=O,radius=3]
        \tkzGetPoint{R}
        \tkzDrawPolygon[fill=blue!20!white, line join=round, line width=0.1pt](O,P,R)
    }
    \tkzLabelPoints(O)
\end{tikzpicture}
}
\begin{document}
    \circleFig{3}
\end{document}

这是我遇到的严重错误。

<to be read again> 
                   -
l.20     \circleFig{3}
                      
Dimensions can be in units of em, ex, in, pt, pc,
cm, mm, dd, cc, nd, nc, bp, or sp; but yours is a new one!
I'll assume that you meant to say pt, for printer's points.
To recover gracefully from this error, it's best to
delete the erroneous units; e.g., type `2' to delete
two letters. (See Chapter 27 of The TeXbook.)

! Missing = inserted for \ifdim.
<to be read again> 
                   -
l.20     \circleFig{3}
                      
I was expecting to see `<', `=', or `>'. Didn't.

! Illegal unit of measure (pt inserted).
<to be read again> 
                   -
l.20     \circleFig{3}
                      
Dimensions can be in units of em, ex, in, pt, pc,
cm, mm, dd, cc, nd, nc, bp, or sp; but yours is a new one!
I'll assume that you meant to say pt, for printer's points.
To recover gracefully from this error, it's best to
delete the erroneous units; e.g., type `2' to delete
two letters. (See Chapter 27 of The TeXbook.)

Missing character: There is no - in font nullfont!
Missing character: There is no 1 in font nullfont!
Missing character: There is no p in font nullfont!
Missing character: There is no t in font nullfont!
LaTeX Font Info:    External font `cmex10' loaded for size
(Font)              <7> on input line 20.
LaTeX Font Info:    External font `cmex10' loaded for size
(Font)              <5> on input line 20.
[1```

答案1

您需要“告诉”\foreach语句通过 —do 来评估上限\pgfmathparse
\foreach \i [parse=true] in {0,...,#1-1}

\documentclass[convert]{standalone}
\usepackage{tkz-euclide}
\newcommand{\circleFig}[1]{%
    \begin{tikzpicture}
    \tkzDefPoints{0/0/O, 3/0/P, 3/0/R}
    \tkzDrawCircle[line width=0.1pt](O,P)
    
    \foreach \i [parse=true] in {0,...,#1-1}
    {
        \tkzDefPointOnCircle[angle=360*\i /#1 ,center=O,radius=3]
        \tkzGetPoint{P}
        \tkzDefPointOnCircle[angle=360*\i /#1 + 360/#1,center=O,radius=3]
        \tkzGetPoint{R}
        \tkzDrawPolygon[fill=blue!20!white, line join=round, line width=0.1pt](O,P,R)
    }
    \tkzLabelPoints(O)
\end{tikzpicture}
}%
\begin{document}
    \circleFig{3}
\end{document}

在此处输入图片描述

但您仍然会收到另一条错误消息:

(/usr/local/texlive/2020/texmf-dist/tex/latex/standalone/standalone.cls
Document Class: standalone 2018/03/26 v1.3a Class to compile TeX sub-files stan
dalone
[...]
Class standalone:
Output written on test.png.
) )
(\end occurred when \ifx on line 2 was incomplete)

我认为这是独立的一个错误。

相关内容