我想要一个宏来检查该字符串是否在之前的宏调用中使用过。我在这里找到了一些例子如何在 Latex 中创建比较字符串的开关结构 但我无法在变量中存储和检索值。我该怎么做才能使此代码正常工作?
\documentclass{article}
\usepackage{pdftexcmds}
\def \myoldvalue {emptystart}
\makeatletter
\newcommand\foo[1]{%
\\myoldvalue: \myoldvalue \def \myoldvalue {#1} \newline %
ARG1: #1 \newline %
\def\@tempa{#1}%
\def\@tempb{comparetothis}% % what do I have to put into comparethis to compare against \myoldvalue
compare: \@tempa ~ \@tempb \newline %
\begingroup
\@onelevel@sanitize\@tempa
\@onelevel@sanitize\@tempb
\ifx\@tempa\@tempb
\aftergroup\@firstoftwo
\else
\aftergroup\@secondoftwo
\fi
\endgroup
{TRUE}
{FALSE}%
\newline next old value: \myoldvalue
}
\makeatother
\begin{document}
~\\
\foo{ArgOne} \\ % excepted print out is: FALSE
\foo{ArgOne} \\ % TRUE
\foo{ArgTwo} \\ % FALSE
\foo{ArgTwo} \\ % TRUE
\foo{ArgOne} \\ % FALSE
\foo{ArgTwo} \\ % FALSE
\end{document}
答案1
我猜你想使用\pdf@strcmp
:
\documentclass{article}
\usepackage{pdftexcmds}
\def\myoldvalue{emptystart} % initialize
\makeatletter
\newcommand\foo[1]{%
\texttt{\string\myoldvalue}: \myoldvalue\\
ARG1: #1\\
\ifnum\pdf@strcmp{\myoldvalue}{#1}=\z@
TRUE%
\else
FALSE%
\fi
\par
\gdef\myoldvalue{#1}%
}
\makeatother
\begin{document}
\foo{ArgOne} % excepted print out is: FALSE
\foo{ArgOne} % TRUE
\foo{ArgTwo} % FALSE
\foo{ArgTwo} % TRUE
\foo{ArgOne} % FALSE
\foo{ArgTwo} % FALSE
\end{document}
一个expl3
版本:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\str_new:N \g_egreg_laststring_str
\str_gset:Nn \g_egreg_laststring_str { emptystart } % initial value
\NewDocumentCommand{\foo}{m}
{
The~current~value~is:~\texttt{\tl_use:N \g_egreg_laststring_str} \par
ARG:~#1\par
\str_if_eq:VnTF \g_egreg_laststring_str { #1 } { TRUE } { FALSE }
\str_gset:Nn \g_egreg_laststring_str { #1 }
}
\ExplSyntaxOff
\begin{document}
\foo{ArgOne} % excepted print out is: FALSE
\foo{ArgOne} % TRUE
\foo{ArgTwo} % FALSE
\foo{ArgTwo} % TRUE
\foo{ArgOne} % FALSE
\foo{ArgTwo} % FALSE
\end{document}