这最初基于这篇文章: 执行查找表时超出容量
那里发布了一个非常好的解决方案。出于我的目的,我想增强原始解决方案,以便键冲突会报告错误,而不是简单地覆盖它。我已经实现了它并且有效。
但是,我遇到了第二个问题,即可以使用\lookupPut
无效的第二个参数值(要存储的值)执行 。 无法\lookupPut
检测到此问题。 错误在\lookupGet
完成后在下游生成,并且生成的错误似乎并不表明存在问题。
我不知道如何编码来检测无效的第二个参数值条件。
提前致谢!
这是我的修改lookup.sty
:
% !TeX root = lookup.sty
%%% lookup.sty %%%
%\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{lookup}[2015/06/10 Lookup for custom details]
%
%
%
% \lookupGet{key}
% returns the value for that key
%
\newcommand{\lookupGet}[1]{%
\@ifundefined{lookup@#1}{%
\PackageError{lookup}{No #1 key in lookup}{%
Key #1 was not found in lookup. %
You can insert it with \string\lookupPut{key}{value}.%
}%
}{%
\@nameuse{lookup@#1}%
}%
}
%%
%% \lookupPut{key}{value for that key}
%%
%\newcommand{\lookupPut}[2]{%
% \@namedef{lookup@#1}{#2}%
%}
%
% \lookupPut{key}{value for that key}
%
\newcommand{\lookupPut}[2]{%
\@ifundefined{lookup@#1}{%
\@namedef{lookup@#1}{#2}%
}%
{%
\PackageError{lookup}{Key #1 already defined when attempted lookupPut}{%
Key #1 was already defined when a lookupPut attempted. %
}%
}%
}%
\endinput
这是我的 MWE:
\documentclass[]{article}
\usepackage{lookup}
\newcommand{\keyone}{keymono}
\newcommand{\keytwo}{keyduo}
\newcommand{\keythree}{keytreble}
\newcommand{\dogone}{labrador}
\newcommand{\dogtwo}{collie}
\lookupPut{\keyone}{\dogone}
%\lookupPut{\keyone}{\dogtwo} % will now throw an error (good!). Should use \keytwo
\newcommand{\catone}{siamese}
\lookupPut{\keythree}{\cattwo} % should be \catone. No error generated!
\begin{document}
Hello world. I like \lookupGet{\keyone}. % This line is OK
Hello world. I like \lookupGet{\keythree}.
% However, the lookupGet above generates the error:
% Undefined control sequence. Hello world. I like \lookupGet{\keythree}
% The desired behavior is for the lookupPut to detect the problem with
% \cattwo so that it can throw an error.
\end{document}