2

I want to have a macro that checks if the string has been used in the previous call of the macro as well. I found some examples here how to create switch structure comparing strings in latex but I wasn't able to store and retrieve the value in a variable. What can I do to make this code work?

\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}
  • Thanks, I just had a short look at the xstring documentation, it looks interesting, but I don't see how it could help here. My problem is not the comparison itself, because that works with "static values". I don't know how to compare to the saved-before value. Replacing "comparetothis" with "ArgOne" works, but I don't know how to put the value of \myoldvalue in there (or later at the place of \@tempb). – user2567875 Jun 30 '15 at 9:00
4

I guess you want to use \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}

enter image description here

An expl3 version:

\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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.