将 SageTeX 与 siunitx 结合使用

将 SageTeX 与 siunitx 结合使用

我有以下问题:我想使用 SageTeX 进行计算并在表达式中使用结果siunitx,以便(例如)

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

会得到与

\SI{12.25}{kg m}

然而,由于 SageTeX 的工作方式,事情并不那么简单。尝试编译以下文档:

\documentclass{scrartcl}

\usepackage{siunitx}
\usepackage{sagetex}

\begin{document}

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

\end{document}

给我这个:

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.

不幸的是,我对 (La)TeX 的内部结构了解不够,无法确定是否有可行的解决方案。是否有可能在 LaTeX 中以某种方式解决这个问题,或者最好尝试在 Sage 本身中编写一些程序?

答案1

每个人都可以通过以下方式访问 Sage萨基马云;无需安装。我发现,解决与其他软件包结合使用时无法正常工作的问题的方法是在sagesilent模式下进行计算,然后创建插入到文档中的文本(例如,参见我的回答这里)。我仍然发现一个问题,即输出给出的数字太多,即使我指定了我想要的数字。将其转换为字符串使我可以控制格式。我不是专家,所以sagetex也许有更好的方法,但代码有效。

\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} 

以下是在 SageMathCloud 中运行的代码: 在此处输入图片描述

相关内容