如何比较扩展命令是否等于另一个命令

如何比较扩展命令是否等于另一个命令

好的,我正在使用 datatool 处理 CSV 文件:

"TRANSID","DATE","QUANTITY"
"141f8b4a-e83e-4222-911a-5a977b4d0afca","11/9/2015","10"
"88cdad7e-eb21-4010-94c5-acca8ac2e559a","11/9/2015","5"
"f15c1ef0-316b-4fdd-9c3a-8a111d60d27da","11/9/2015","50"
"f15c1ef0-316b-4fdd-9c3a-8a111d60d27da","11/9/2015","50"

问题是我想将最后两行合并到一页上,所以我需要检查它们是否等效 --- 但没有任何运气:

\documentclass{memoir}
\usepackage{ifthen}
\usepackage{etoolbox}
\usepackage{datatool}
\DTLloaddb{data}{C:/Users/wfadams/Desktop/heiferinvoice/test.csv}

\newcommand{\oldtransactionid}{false}

\newboolean{continuerecord}
\setboolean{continuerecord}{false}

\begin{document}%
Initial setup here\par
%
\DTLforeach*{data}{%
\orderNo=TRANSID, \date=DATE, \quantity=QUANTITY}%
{%
\DTLiffirstrow{Processing first record}{second or further record}%
%
\ifthenelse{\boolean{continuerecord}}%
{Continuing a record\par}%
{Not continuing a record, clear to new page\par}%false
%
\hspace*{-3bp}\fontsize{10bp}{22bp}\selectfont\hbox to 42bp{\quantity}\hbox to 72bp{\hspace*{30bp} \quantity}\hbox to 144bp{\hspace*{15bp}ITEMCODE}\hbox to 288bp{\hspace*{15bp}\orderNo\hfill}\par
%
\expandafter\ifstrequal\expandafter{\expandafter\oldtransactionid}{\expandafter\orderNo}%
{\setboolean{continuerecord}{true}T\par}%
{\setboolean{continuerecord}{false}F\par}%
OLD: \oldtransactionid \par
NEW: \orderNo\par
\edef\oldtransactionid{\orderNo}
}%
%
place last page here
\end{document}

我认为添加 \edef 让我遇到了最后的障碍 --- 我只需要一种方法来使比较正常工作 --- 并且显然 \expandafter 命令没有帮助(也尝试了 \immediate(正如 egreg 指出的那样,这是不正确的))。

还尝试过:

\expandafter\ifstrequal{\expandafter{\oldtransactionid}}{\expandafter{\orderNo}}%

还有一个疯狂的想法,把数据写到临时文件中然后再读回来,对此我感到非常尴尬,不愿意表现出来。

答案1

您必须扩展传递给的两个宏\ifstrequal,但是您使用的字符串\expandafter只会影响第一个(我想说,实际上不会影响)。

如果数据是 ASCII 字符串,则标准技术是使用\edef

\begin{filecontents*}{\jobname.csv}
"TRANSID","DATE","QUANTITY"
"141f8b4a-e83e-4222-911a-5a977b4d0afca","11/9/2015","10"
"88cdad7e-eb21-4010-94c5-acca8ac2e559a","11/9/2015","5"
"f15c1ef0-316b-4fdd-9c3a-8a111d60d27da","11/9/2015","50"
"f15c1ef0-316b-4fdd-9c3a-8a111d60d27da","11/9/2015","50"
\end{filecontents*}

\documentclass{memoir}
\usepackage{ifthen}
\usepackage{etoolbox}
\usepackage{datatool}
\DTLloaddb{data}{\jobname.csv}

\newcommand{\oldtransactionid}{false}

\newboolean{continuerecord}
\setboolean{continuerecord}{false}

\begin{document}

Initial setup here

\DTLforeach*{data}{\orderNo=TRANSID, \date=DATE, \quantity=QUANTITY}%
  {%
   \DTLiffirstrow{Processing first record}{second or further record}%
   %
   \ifthenelse{\boolean{continuerecord}}%
     {Continuing a record\par}%
     {Not continuing a record, clear to new page\par}%false
   %
   \hspace*{-3bp}\fontsize{10bp}{22bp}\selectfont\hbox to 42bp{\quantity}%
   \hbox to 72bp{\hspace*{30bp} \quantity}%
   \hbox to 144bp{\hspace*{15bp}ITEMCODE}%
   \hbox to 288bp{\hspace*{15bp}\orderNo\hfill}\par
   %
   \begingroup\edef\x{\endgroup
     \noexpand\ifstrequal{\oldtransactionid}{\orderNo}%
   }\x
   {\setboolean{continuerecord}{true}T\par}%
   {\setboolean{continuerecord}{false}F\par}%
   OLD: \oldtransactionid \par
   NEW: \orderNo\par
   \edef\oldtransactionid{\orderNo}
  }

place last page here
\end{document}

在此处输入图片描述

相关内容