如何在Astropy中指定LaTeX输出格式

问题描述:

我刚开始使用Astropy以LaTeX格式编写表格。但是,当我写下这张表格时,它的工作就是完成这个工作,标准化为大质量的单位,通常是1e6太阳质量,显示时没有科学记数法。如何在Astropy中指定LaTeX输出格式

一个例子:

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 


def table_write(): 
    from astropy.io import ascii 
    import astropy.table 
    import astropy.units as u 


    #fake data, ~ the same order of magnitude of real ones 
    Mbh = [1e1, 7e3] 
    t_final = [13, 12.2] 

    tab = astropy.table.Table([Mbh, t_final], 
      names = ['Mbh', 't_final']) 
    tab['Mbh'].unit = '1e6 Msun' 
    tab['t_final'].unit = 'Gyr' 

    ascii.write(tab, 
      Writer=ascii.Latex, 
      latexdict=ascii.latex.latexdicts['AA']) 


if __name__ == "__main__": 
    table_write() 

输出是

\begin{table} 
\begin{tabular}{cc} 
\hline \hline 
Mbh & t_final \\ 
$\mathrm{1000000\,M_{\odot}}$ & $\mathrm{Gyr}$ \\ 
\hline 
10.0 & 13.0 \\ 
7000.0 & 12.2 \\ 
\hline 
\end{tabular} 
\end{table} 

这是好的,除了

\ mathrm {百万\中,M _ {\ ODOT}}

哪sh乌尔德是一个不错的

\ mathrm {10^{6} \中,M _ {\ ODOT}}

所以,我想格式化单元的一部分。 documentation似乎报告了一种方法来做到这一点,但它绝对不清楚。

+0

这两个输出都不是我通过长镜头称之为“好”的东西。相反,萎缩应该输出使用LaTeX 软件包的代码,该软件包可在LaTeX中自定义。这可能吗? –

+0

siunitx是否可用,例如,在mathjax中? – Iguananaut

您可以使用astropy.units.def_unit()方法定义新单元u.Msun。如果你愿意,你也可以指定列的格式科学记数法与参数的astropy.io.ascii.write()方法formats

from astropy.io import ascii 
import astropy.table 
import astropy.units as u 

def table_write(): 

    #fake data, ~ the same order of magnitude of real ones 
    Mbh = [1e1, 7e3] 
    t_final = [13, 12.2] 

    tab = astropy.table.Table([Mbh, t_final], 
      names = ['Mbh', 't_final']) 

    # Define new unit with LaTeX format 
    new_Msun = u.def_unit('1E6 Msun', 10**6*u.Msun, format={'latex': r'10^6\,M_{\odot}'}) 

    tab['Mbh'].unit = new_Msun 
    tab['t_final'].unit = u.Gyr 

    ascii.write(tab, 
      Writer=ascii.Latex, 
      latexdict=ascii.latex.latexdicts['AA'], 
      formats={'Mbh':'%.0E'}) # Set the column's format to scientific notation 


if __name__ == "__main__": 
    table_write() 

乳胶:

\begin{table} 
\begin{tabular}{cc} 
\hline \hline 
Mbh & t_final \\ 
$\mathrm{10^6\,M_{\odot}}$ & $\mathrm{Gyr}$ \\ 
\hline 
1E+01 & 13.0 \\ 
7E+03 & 12.2 \\ 
\hline 
\end{tabular} 
\end{table} 

正如你可以在这里看到新单位实际上是太阳质量的10^6倍,而用LaTeX格式的文本是正确的,

enter image description here