# Relaxing a command's requirement to be run in mathmode (specifically \sage in tikz \draw command)

I would like to be able to build a dynamic tikz picture using some random numbers generated via sage. Presently this seems to fail because tikz doesn't like the mathmode that sage has to be run inside of, but as I am just generating a simple integer number, latex shouldn't need mathmode to parse it. Thus I am wondering if there is a way to allow \sage (or any other command) to run outside of mathmode when it normally requires it (without digging all through the package to remove the requirement in the original definition).

I can't supply a MWE right now, but will edit with one in a couple hours if it's still needed.

• what does \sage command do? – percusse Apr 6 '18 at 14:03
• @percusse It writes its arg to a file \jobname.sage, then on a subsequent run it inputs data from a file obtained by running the Sage software on \jobname.sage (not sure about names). So it's not expandable and what Jason is asking is quite hard. Not necessarily impossible since tikz parses numbers etc using non-expandable code so it might be possible to pause that parsing and restart it with the output of \sage. Still, changes are needed in the internals. – Bruno Le Floch Apr 6 '18 at 14:08
• Saying you want sage to run outside of math mode doesn't make sense to me since using sagetex requires a 3 step compilation process: 1. Run LaTeX, 2. run Sage, 3. run LaTex using the data generated by Sage in step 2. Most mistakes come from calling \sage in the first pass because Sage hasn't run. One way to get around this is to make sure that the tikz code is generated during the Sage calculations in step 2. See, for example, my answer to plotting the Cantor function here. – DJP Apr 6 '18 at 15:34
• I said that badly. Most mistakes come from calling Sage to get a calculation needed for the LaTeX code to run. – DJP Apr 6 '18 at 16:49
• Welcome to TeX.SX!! Can you please give a minimal (non) working example. A MWE should start with a \documentclass command, have a minimal preamble and then \begin{document}...\end{document}. Ideally the code should compile, although it probably won't in this case, and be as small as possible to demonstrate your problem. If you give us code to start from it is much easier to help you -- and hence much more likely that some one will! – user30471 Apr 7 '18 at 5:54