这是有问题的代码:
\documentclass{article}
\usepackage{etoolbox}
\usepackage{datatool}
\usepackage{longtable}
\usepackage{fp}
\def\initialValue{10}
\DTLnewdb{thisEntryDBWarehouse}
\makeatletter
\newcommand*{\ifstartswith}[2]{%
\if\@car#1.\@nil\@car#2.\@nil
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi}
\makeatother
\newcommand{\evalModifier}[1]{
\ifstartswith{#1}{+}{\FPeval\result{clip(#1+\initialValue)}\result}{
\ifstartswith{#1}{-}{\FPeval\result{clip(#1+\initialValue)}\result}{
#1
}
}
}
\newcommand{\addEntry}[3]{%
\def\thisValue{\evalModifier{#2}}
\thisValue
% \def\thisValue{#2}
\ifcsname thisEntry#1\thisValue\endcsname
\cseappto{thisEntry#1\thisValue}{\unexpanded{\unexpanded{\\}} & #3 }%
\else
\DTLnewrow{thisEntryDB#1}%
\DTLnewdbentry{thisEntryDB#1}{value}{\thisValue}%
\csedef{thisEntry#1\thisValue}{{\noexpand\noexpand\noexpand\bf \thisValue} & #3}%
\fi
}
\newcommand{\printAllEntries}[1]{%
% % Packs all the events in thisCharEvents tmp var
\edef\thisList{}
\DTLsort{value}{thisEntryDB#1}
\DTLforeach*{thisEntryDB#1}{\theValue=value}{\edef\thisList{\expandonce{\thisList} \csuse{thisEntry#1\theValue} \unexpanded{\\}}%
}
% Prints them in a table - if they exist
\ifdefempty{\thisList}{}{\paragraph{\bf Entries:}\mbox{}%
\begin{longtable}{c p{9cm}}%
Value & Description\\
\thisList%
\end{longtable}%
}
}
\begin{document}
\addEntry{Warehouse}{10}{Entry 0}
\addEntry{Warehouse}{10}{Entry 1}
\addEntry{Warehouse}{12}{Entry 2}
\addEntry{Warehouse}{+0}{Entry 3}
\addEntry{Warehouse}{+2}{Entry 4}
\addEntry{Warehouse}{11}{Entry 5}
\addEntry{Warehouse}{+3}{Entry 6}
\addEntry{Warehouse}{-2}{Entry 7}
\printAllEntries{Warehouse}
\end{document}
预期输出如下表:
8 Entry 7
10 Entry 0
Entry 1
Entry 3
11 Entry 5
12 Entry 2
Entry 4
13 Entry 6
但源代码无法编译。第 29 行仅用于测试 \evalModifier 是否正确执行其工作(似乎正确)。第 30 行之所以存在,是因为 \addEntry 宏的其余部分似乎无法很好地处理 \thisValue 的值。这是问题之一。我尝试在第 28 行使用 edef 而不是 def,但 \evalModifier 不起作用。
还有一个问题。如果你取消注释第 30 行,它将会编译,但第 36 行似乎扩展 \thisValue 太晚了。所有条目的值列都以“-2”结尾。