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?
sage
so cant test it, but as this works withpgf
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.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.\Value
do you get a numerical output?.sage
file, and then I have to runpdflatex
again, since SageTeX (as far as I understand) uses references to insert its computed results.