使用来自的代码如何检查数据工具数据库是否有现有成员对于同样的问题(构建datatool
具有唯一字符串的数据库),只要没有\DeclareDocumentCommand
具有相同功能的附加级别(此处通过),它就可以很好地工作。现在解决方案无法识别 ABC 字符串并将其再次添加到数据库中。定义\tmp
应该完全展开(xstring
用户手册)。结果应该是数据库ABC
中只有一个字符串duplicates
。
我需要引入的字符串在以 DB(下面是简化的 MWE)结束之前被多次分配给各种定义。
\documentclass{article}
\usepackage{datatool}
\usepackage{xparse,xstring}
\makeatletter
% Patch \DTLgetlocation to gobble the error
\let\ErrorFreeDTLgetlocation\DTLgetlocation
\patchcmd{\ErrorFreeDTLgetlocation}% <cmd>
{\PackageError}% <search>
{\@gobbletwo}% <replace>
{}{}% <success><failure>
\def\@@dtlnovalue{\@dtlnovalue}
\newcommand*{\DBKey}{Value}
\newcommand*{\AddMemberToDB}[2]{%
% #1 = name of DB
% #2 = member to be added to DB
\DTLnewrow{#1}%
\DTLnewdbentry{#1}{\DBKey}{#2}%
}
\newcommand*{\IfIsInDB}[4]{% %New definition!
% #1 = name to be used for this duplicate's DB
% #2 = member to check if in DB (gets added if not)
% #3 = code to execute if member is in DB
% #4 = code to execute if member is not in DB
\ErrorFreeDTLgetlocation{\RowIndex}{\ColIndex}{#1}{#2}%
\ifx\RowIndex\@@dtlnovalue
#4%
\AddMemberToDB{#1}{#2}%
\else
#3
\fi
}
\makeatother
\DeclareDocumentCommand{\AddEntrytoDB}{m m}{
\def\tmp{}
\StrDel{#2}{ }[\tmp]% delete the spaces
\IfIsInDB{#1}{\tmp}
{CMD: \tmp is a duplicate}
{CMD: \tmp\, added DB}
}
\begin{document}
\def\ABC{A B C} % ABC string with spaces
\def\varABC{}
\StrDel{\ABC}{ }[\varABC]% delete the spaces
\DTLnewdb{duplicates} % create new DB
\AddMemberToDB{duplicates}{\varABC} % add ABC string w/o spaces to DB
Current DB:\par % display the DB
\DTLdisplaydb{duplicates}
\medskip
\IfIsInDB{duplicates}{\varABC} % check another ABC string w/o spaces
{ABC is a duplicate}% Yes: is in
{ABC added DB}% No: is not in, add it
\par
\AddEntrytoDB{duplicates}{\ABC} % add another ABC string via Command
\medskip
Current DB:\par % display the DB
\DTLdisplaydb{duplicates}
\end{document}