2017-08-16 36 views
1

我剛開始使用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

回答

2

您可以使用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