针对 Sage 命令进行字符串检查,是否发出扩展命令?

针对 Sage 命令进行字符串检查,是否发出扩展命令?

我正在尝试编写一个宏来对两个字符串进行条件检查。我遇到的问题是我想检查命令的输出\sage并进行字符串比较而不扩展\sage输出的内容。通常我会做一些\expandafterand/or的组合\noexpand,但我似乎无法克服的问题是该\sage命令是多么脆弱,因为它需要多次编译才能有内容。

就伪代码而言,我想做如下的事情:

\documentclass{article}
\usepackage{sagetex}
\newcommand{\sagecheck}[3]{%
% #1 is the sagevariable, 
% #2 is what I want to check to see if it is equal to 
% #3 is the action to take if they are equal.

\ifthen{%
    \magicstringcheck{$\sage{#1}$}{%
         \newlabel{@sageinline0}{{%
         #2}{}{}{}{}}%
         }%
    {#3}
}% End of \sagecheck

\newcommand{\magicstringcheck}[2]{%
     %This is the part I don't know how to write. It should detokenize the result of the \sage command, but not the \sage command itself, and it should check that against the expected sage string output, which is of the form (in the sout file):
%     \newlabel{@sageinline0}{{%
%     (sage_variable_output)}{}{}{}{}}
}

\begin{document}
\begin{sagesilent}
k = 4
\end{sagesilent}
\sagecheck{k}{4}{This should work}
\sagecheck{k}{15}{This should do nothing}
\sagecheck{foo}{12}{Ideally this would error, since there is no sage variable named 'foo'}
\end{document}

相关内容