如何使用 siunitx 將變數宣告為單位?

如何使用 siunitx 將變數宣告為單位?

在天文學中,哈伯常數的值仍然有些不確定。因此,它通常寫為$H = h \SI{100}{\kilo\meter\per\second\per\mega\parsec}$,其中$h$現在被視為單位的一部分。長度的單位是$h^{-1} \si{\mega\parsec}$,我想將其排版為$\si{\per\h\mega\parsec}$。我如何在乳膠中實現這一點,以便“h”像變量一樣以斜體排版?下面是一個最小的例子。

\documentclass{article}

\usepackage{siunitx}

\DeclareSIUnit \h {$h$}
\DeclareSIUnit \parsec {pc}


\begin{document}

\si{\per\h\mega\parsec}

\end{document}

這會出現錯誤:

! Missing } inserted.
<inserted text> 
            }
l.11 \si{\per\h\mega\parsec}

?

答案1

我不知道天文學中單位的排版,因此我不能說“h”應該如何排版,但是數學斜體中的“h”很容易:

\documentclass{article}

\usepackage{siunitx}

\DeclareSIUnit \h {\ensuremath{\mathit{h}}}
\DeclareSIUnit \parsec {pc}

\begin{document}
  \si{\per\h\mega\parsec} 
\end{document}

結果

答案2

在文字模式下轉義,然後使用數學模式。

\documentclass{article}
\usepackage{amsmath}
\usepackage{siunitx}

\DeclareSIUnit \h {\text{$h$}}
\DeclareSIUnit \parsec {pc}

\begin{document}

\unit{\per\h\mega\parsec} \unit[per-mode=fraction]{\per\h\mega\parsec}

\end{document}

在此輸入影像描述

相關內容