![使用计数器临时组成整数数组](https://linux22.com/image/369388/%E4%BD%BF%E7%94%A8%E8%AE%A1%E6%95%B0%E5%99%A8%E4%B8%B4%E6%97%B6%E7%BB%84%E6%88%90%E6%95%B4%E6%95%B0%E6%95%B0%E7%BB%84.png)
我需要跟踪几个计数器。为此,我使用一组计数器并尝试将它们绑定到一个\ifthenelse
生成临时整数数组的 hack 中。然后我稍后使用该 hack 的输出来执行\setcounter
。如果我尝试直接\thetypefirst
在 中使用\setcounter
,它会顺利通过。但是,如果它隐藏在一堆 中\ifthenelse
,它会产生缺失数字错误。
MWE(实际上不起作用):
\documentclass{article}
\usepackage{ifthen}
\newcounter{typezero}
\newcounter{typefirst}
\newcounter{typesecond}
\newcounter{typethird}
\newcounter{typefourth}
\newcounter{typefifth}
\newcounter{typesixth}
\newcounter{typeseventh}
\newcounter{typeeightth}
% set a few values
\setcounter{typefirst}{11}
\setcounter{typesecond}{12}
\newcommand{\typeofdata}[1]{ % fails in an \setcounter statement
\ifthenelse{\equal{#1}{0}}{ \thetypezero }{%
\ifthenelse{\equal{#1}{1}}{ \thetypefirst }{%
\ifthenelse{\equal{#1}{2}}{ \thetypesecond }{%
\ifthenelse{\equal{#1}{3}}{ \thetypethird }{%
\ifthenelse{\equal{#1}{4}}{ \thetypefourth }{%
\ifthenelse{\equal{#1}{5}}{ \thetypefifth }{%
\ifthenelse{\equal{#1}{6}}{ \thetypesixth }{%
\ifthenelse{\equal{#1}{7}}{ \thetypeseventh }{%
\ifthenelse{\equal{#1}{8}}{ \thetypeeightth }{%
}}}}}}}}}
}
\newcommand{\tempcmd}{\thetypefirst} % works in an \setcounter statement
\begin{document}
\newcounter{tmpp}
\setcounter{tmpp}{\tempcmd}
counter typefirst =\thetmpp \\
\setcounter{tmpp}{\typeofdata{2}} %fail
counter typesecond =\thetmpp \\
\end{document}
知道如何让它工作吗?
答案1
为什么不直接使用数字呢?
\documentclass{article}
\newcounter{type0}
\newcounter{type1}
\newcounter{type2}
\newcounter{type3}
\newcounter{type4}
\newcounter{type5}
\newcounter{type6}
\newcounter{type7}
\newcounter{type8}
\newcounter{type9}
% set a few values
\setcounter{type1}{11}
\setcounter{type2}{12}
\newcommand{\thetypeofdata}[1]{\csname thetype#1\endcsname}
\newcommand{\typeofdata}[1]{\value{type#1}}
\begin{document}
\newcounter{tmpp}
\setcounter{tmpp}{\typeofdata{1}}
counter typefirst = \thetmpp
\setcounter{tmpp}{\typeofdata{2}} %fail
counter typesecond =\thetmpp
\end{document}
答案2
我很乐意为您提供更优雅的建议。我认为这是可行的。
\documentclass{article}
\usepackage{ifthen}
\newcounter{typezero}
\newcounter{typefirst}
\newcounter{typesecond}
\newcounter{typethird}
\newcounter{typefourth}
\newcounter{typefifth}
\newcounter{typesixth}
\newcounter{typeseventh}
\newcounter{typeeightth}
% set a few values
\setcounter{typefirst}{11}
\setcounter{typesecond}{12}
\newcommand{\typeofdata}[1]{ % fails in an \setcounter statement
\ifthenelse{\equal{#1}{0}}{\global\def\pfft{\thetypezero}}{%
\ifthenelse{\equal{#1}{1}}{\global\def\pfft{\thetypefirst}}{%
\ifthenelse{\equal{#1}{2}}{\global\def\pfft{\thetypesecond}}{%
\ifthenelse{\equal{#1}{3}}{\global\def\pfft{\thetypethird}}{%
\ifthenelse{\equal{#1}{4}}{\global\def\pfft{\thetypefourth}}{%
\ifthenelse{\equal{#1}{5}}{\global\def\pfft{\thetypefifth}}{%
\ifthenelse{\equal{#1}{6}}{\global\def\pfft{\thetypesixth}}{%
\ifthenelse{\equal{#1}{7}}{\global\def\pfft{\thetypeseventh}}{%
\ifthenelse{\equal{#1}{8}}{\global\def\pfft{\thetypeeightth}}{%
}}}}}}}}}
}
\newcommand{\tempcmd}{\thetypefirst} % works in an \setcounter statement
\begin{document}
\newcounter{tmpp}
\setcounter{tmpp}{\tempcmd}
\typeofdata{2}
counter typefirst =\thetmpp \\
\typeofdata{2}\setcounter{tmpp}{\pfft}\thetmpp \\
\typeofdata{4}\setcounter{tmpp}{\pfft}\thetmpp \\
\end{document}