我正在设计 2 个宏,但嵌套它们失败了,我看不出我哪里犯了错误。
第一个宏取一个浮点数(例如 3.2)并再次检查 3 个阈值。第二个宏根据阈值显示一个标签。
两个宏单独工作,但嵌套它们会带来我无法解决的错误:
! Incomplete \iffalse; all text was ignored after line 61.
<inserted text>
\fi
以下是代码:
\documentclass{article}
\usepackage{xcolor}
\usepackage[skins]{tcolorbox}
\tcbuselibrary{xparse}
\newcommand*{\ScoreHigh}{7.0}
\newcommand*{\ScoreLow}{3.0}
% Will output None, Low or High depending on the input
\newcommand{\category}[1]{%
\pgfmathparse{#1 < \ScoreLow}
\ifnum\pgfmathresult=1
None%
\else
\pgfmathparse{#1 < \ScoreHigh}
\ifnum\pgfmathresult=1
Low%
\else
High%
\fi%
\fi%
}
\colorlet{color@None}{black!20}
\definecolor{color@Low}{rgb}{0.0, 0.65, 0.31}
\colorlet{color@High}{orange}
\DeclareTotalTCBox{\Tag}{m}{
enhanced,nobeforeafter,
tcbox raise base,
boxrule=0.4pt,
top=0mm,bottom=0mm,right=1mm,left=1mm,
boxsep=2pt,
colframe=color@#1,
colback=tcbcolframe,
coltext=black,
}{#1}%
\MakeRobust\Tag
\ifdefined\pdfstringdefDisableCommands
\pdfstringdefDisableCommands{\def\Tag#1{'#1'}}
\fi
\setlength{\parindent}{0pt}
\begin{document}
$5.3/10 \rightarrow$ category \textbf{\category{5.3}}.
\bigskip
This is a \Tag{Low} tag.
\Tag{\category{5.3}} % <--- Chaining these command fail
\end{document}
事实上,错误与第 61 行有关,因为该行是嵌套宏的。
有什么想法表明我为什么可能会失败吗?
答案1
pgf 计算不可扩展。但你可以改用 expl3 和 l3fp:
\documentclass{article}
\usepackage{xcolor}
\usepackage[skins]{tcolorbox}
\tcbuselibrary{xparse}
\newcommand*{\ScoreHigh}{7.0}
\newcommand*{\ScoreLow}{3.0}
% Will output None, Low or High depending on the input
\ExplSyntaxOn
\newcommand{\category}[1]{
\fp_compare:nNnTF {#1}<{\ScoreLow}{None}
{
\fp_compare:nNnTF{#1}<{\ScoreHigh}
{Low}{High}
}}
\ExplSyntaxOff
\colorlet{color@None}{black!20}
\definecolor{color@Low}{rgb}{0.0, 0.65, 0.31}
\colorlet{color@High}{orange}
\DeclareTotalTCBox{\Tag}{m}{
enhanced,nobeforeafter,
tcbox raise base,
boxrule=0.4pt,
top=0mm,bottom=0mm,right=1mm,left=1mm,
boxsep=2pt,
colframe=color@#1,
colback=tcbcolframe,
coltext=black,
}{#1}%
\MakeRobust\Tag
\ifdefined\pdfstringdefDisableCommands
\pdfstringdefDisableCommands{\def\Tag#1{'#1'}}
\fi
\setlength{\parindent}{0pt}
\begin{document}
\Tag{\category{5.3}}
\Tag{\category{-1}}
\Tag{\category{8}}
\end{document}