I am trying to write a macro to do a conditional check against two strings. The problem I'm having is that I want to check the output of a \sage command and do a string comparison without expanding the content of the \sage output. Normally I'd do some combinations of \expandafter and/or \noexpand, but the wrinkle I can't seem to get past is how fragile the \sage command is, since it requires multiple compiles before it has content.

In terms of pseudocode I want to do something like the following:

% #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.

}% End of \sagecheck

     %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)}{}{}{}{}}

k = 4
\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'}
  • Is there a reason you can't do it all in Sage? if k == 4: .... – John Palmieri Sep 24 '19 at 17:56
  • For fairly irritating reasons I need the output to be generated by a \sage command specifically, and not have a string directly displayed from sage. My hope was to have a LaTeX command that could parse the sage output and write it's own string (or something else) in native LaTeX, but based upon the sage result. – Jason Sep 24 '19 at 18:47
  • Can't you do something like \edef\temp{\sage{<variable name>}} and then \ifx that against the expected value? Why do you use the whole label definition in the comparison? – siracusa Sep 25 '19 at 2:56
  • I lack the technical knowledge to understand fully what you want; this reminds me of a question I answered earlier: Filling the Latex table dynamically. Does that approach of doing the work with Sage and outputting the result as a raw string through \sagestr work? – DJP Sep 25 '19 at 12:19