有人能帮我解决以下代码吗?我正在尝试检索一个值,但它给出了一个错误。
我想使用 pgfmathparse 对 \CPAN 执行一些数学运算,但它给我带来了一个问题。
\usepackage[a4paper, margin=1in]{geometry}
\usepackage{graphicx}
\usepackage{fancyhdr}
\usepackage{ifthen}
\usepackage{floatrow}
\usepackage{calculator}
\usepackage{etoolbox}
\usepackage{pgf}
\pgfmathsetseed{\number\pdfrandomseed}% set a "random" seed based on time
\pgfmathdeclarerandomlist{myarray}{{semianually}{daily}{anually}{monthly}{quarterly}}
\newcommand{\CompoundPeriod}[1]
{
\edef\mytemp{{#1}}
\expandafter\ifstrequal\mytemp{daily}{365}{ }
\expandafter\ifstrequal\mytemp{semianually}{2}{ }
\expandafter\ifstrequal\mytemp{quarterly}{4}{ }
\expandafter\ifstrequal\mytemp{monthly}{12}{ }
\expandafter\ifstrequal\mytemp{anually}{1}{ }
}
\begin{document}
\pgfmathrandomitem{\CPA}{myarray}
\newcommand{\CPAN}{\CompoundPeriod{\CPA}}
\pgfmathparse{\CPAN+2}
\pgfmathresult
\end{document}
它给了我一个未定义的控制序列错误。
答案1
\pgfmathparse
完全扩展其参数,类似于\edef
。因此,您不能在参数中使用不可扩展的命令,但您\CompoundPeriod
使用的\edef
和\ifstrequal
是不可扩展的。
所以你必须避免使用这些。如果你使用 pdfTeX,你可以使用它\pdfstrcmp
来构建一个可扩展的\ifstrequal
,它也会扩展它的参数,所以你不需要\edef
:
\documentclass{scrartcl}
\usepackage[a4paper, margin=1in]{geometry}
\usepackage{graphicx}
\usepackage{fancyhdr}
\usepackage{ifthen}
\usepackage{floatrow}
\usepackage{calculator}
\usepackage{etoolbox}
\usepackage{pgf}
\pgfmathsetseed{\number\pdfrandomseed}% set a "random" seed based on time
\pgfmathdeclarerandomlist{myarray}{{semianually}{daily}{anually}{monthly}{quarterly}}
\makeatletter
\newcommand\ifexpstrequal[2]{%
\ifnum\pdfstrcmp{#1}{#2}=0
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
}
\makeatother
\newcommand{\CompoundPeriod}[1]{
\ifexpstrequal{#1}{daily}{365}{ }
\ifexpstrequal{#1}{semianually}{2}{ }
\ifexpstrequal{#1}{quarterly}{4}{ }
\ifexpstrequal{#1}{monthly}{12}{ }
\ifexpstrequal{#1}{anually}{1}{ }
}
\begin{document}
\pgfmathrandomitem{\CPA}{myarray}
\newcommand{\CPAN}{\CompoundPeriod{\CPA}}
\pgfmathparse{\CPAN+2}
\pgfmathresult
\end{document}