3

I have the following problem: I'd like to do calculations using SageTeX and use the result in an siunitx expression, so that (for example)

\SI{\sage{3.5^2}}{kg m}

would give me the same result as

\SI{12.25}{kg m}

However, due to how SageTeX works, it's not as easy as this. Trying to compile the following document:

\documentclass{scrartcl}

\usepackage{siunitx}
\usepackage{sagetex}

\begin{document}

\SI{\sage{3.5^2}}{kg m}

\end{document}

Gives me this:

pdflatex -interaction=nonstopmode test.tex

This is pdfTeX, Version 3.1415926-2.5-1.40.14 (TeX Live 2013/Debian)
 restricted \write18 enabled.
entering extended mode
(./test.tex
LaTeX2e <2011/06/27>
Babel <3.9h> and hyphenation patterns for 9 languages loaded.

...

No file test.sagetex.sout.
)
No file test.aux.

...

! Argument of \reserved@a has an extra }.
<inserted text> 
                \par 
l.8 \SI{\sage{3.5^2}}{kg m}

Runaway argument?
{\def \@currenvir {NoHyper}\edef \@currenvline {\on@line }\csname NoH\ETC.
! Paragraph ended before \reserved@a was complete.
<to be read again> 
                   \par 
l.8 \SI{\sage{3.5^2}}{kg m}


!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! siunitx error: "invalid-token-in-number"
! 
! Invalid token '\immediate ' in numerical input.
! 
! See the siunitx documentation for further information.
! 
! For immediate help type H <return>.
!...............................................  

l.8 \SI{\sage{3.5^2}}{kg m}

! Argument of \reserved@a has an extra }.
<inserted text> 
                \par 
l.8 \SI{\sage{3.5^2}}{kg m}

Runaway argument?
{\def \@currenvir {NoHyper}\edef \@currenvline {\on@line }\csname NoH\ETC.
! Paragraph ended before \reserved@a was complete.
<to be read again> 
                   \par 
l.8 \SI{\sage{3.5^2}}{kg m}


!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! siunitx error: "invalid-number"
! 
! Invalid numerical input '\immediate \write \ST@sf {try:
 _st_.inline(0,
! latex(3.5^2))
except:
 _st_.goboom(8)}\def \par '.
! 
! See the siunitx documentation for further information.
! 
! For immediate help type H <return>.
!...............................................  

l.8 \SI{\sage{3.5^2}}{kg m}

*********************************************************************
[1{/var/lib/texmf/fonts/map/pdftex/updmap/pdftex.map}] (./test.aux) )
(\end occurred when \iffalse on line 8 was incomplete)
(\end occurred when \iffalse on line 8 was incomplete)
(see the transcript file for additional information)</usr/share/texlive/texmf-d
ist/fonts/type1/public/amsfonts/cm/cmr10.pfb>
Output written on test.pdf (1 page, 8198 bytes).
Transcript written on test.log.

Unfortunately, I don't understand (La)TeX's internals well enough to decide whether there is a feasible solution to this. Is it possible to work around this somehow in LaTeX, or would it be better to try to program something in Sage itself?

  • I don't have sage so cant test it, but as this works with pgf math, you could try saving the computation in a variable and then using it: \pgfmathsetmacro{\Value}{3.5^2} and then \SI{\Value}{\kilo\gram\meter}. If you are interested in a non-sage answer let me know. – Peter Grill May 6 '14 at 23:17
  • Unfortunately, pgf seems to work quite differently than the SageTeX package (pgf is pure TeX, after all). While your example using pgf does indeed work, something like \pgfmathsetmacro{\Value}{\sage{3.5^2}} (same error as above) or \NewDocumentCommand{\Value}{}{\sage{3.5^2}} \SI{\Value}{kg m} does not (error: "Invalid token '\Value ' in numerical input \SI{\Value}{kg m}"). I want to use Sage specifically because the actual computations are much more complicated than the example in the question and involve some Sage (Python) code. – Socob May 6 '14 at 23:26
  • If you just print \Value do you get a numerical output? – Peter Grill May 6 '14 at 23:30
  • Well, yes, but of course, I first have to run Sage on the generated .sage file, and then I have to run pdflatex again, since SageTeX (as far as I understand) uses references to insert its computed results. – Socob May 6 '14 at 23:36
  • 1
    A solution that works with PythonTeX is here. Since PythonTeX operates in a manner similar to SageTeX, I expect a similar or even almost identical solution may be possible. But I don't have Sage to test this. – G. Poore May 7 '14 at 0:21
4

Everyone can get access to Sage with Sagemath Cloud; no installation necessay. I've found that the way around the problems of not working when combined with other packages is to do the calculations in sagesilent mode and then create the text that gets inserted into your document (see for example, my answer here). I still found an issue with the output giving me too many digits, even when I specified how many I wanted. Converting it to a string gave me control over the format. I'm no expert in sagetex so maybe there's a better way, but the code works.

\documentclass{scrartcl}
\usepackage{siunitx}
\usepackage{sagetex}
\begin{document}
\begin{sagesilent}
answer = 4.5^2
output1 = r"\SI{%f}{kg m}"%(answer.n(digits=4))
output2 = r"\SI{%s}{kg m}"%(str(answer.n(digits=4)))
\end{sagesilent}
Output without using a string:
\sagestr{output1}

Output using a string:
\sagestr{output2}
\end{document} 

Here's the code running in SageMathCloud: enter image description here

| improve this answer | |
  • Oh, that's just perfect! I didn't realize that text output by \sagestr{} would not only be pasted verbatim into the document, but also evaluated by LaTeX! That's great, thank you very much! – Socob May 7 '14 at 19:56
  • Note: SageMathCloud changed name, and is now CoCalc: cocalc.com. – Samuel Lelièvre Jan 10 '19 at 11:48

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.