![如何使用 siunitx 將變數宣告為單位?](https://rvso.com/image/309844/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20siunitx%20%E5%B0%87%E8%AE%8A%E6%95%B8%E5%AE%A3%E5%91%8A%E7%82%BA%E5%96%AE%E4%BD%8D%EF%BC%9F.png)
在天文學中,哈伯常數的值仍然有些不確定。因此,它通常寫為$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}