如何使用 siunitx 取消方程中的指數?

如何使用 siunitx 取消方程中的指數?

下面的最小工作範例演示了該問題。我有許多格式類似於下面第一個方程的方程,其中分子為平方米,分母為米。我想取消指數,但現在我必須說“m m”並取消第一個“m”,這看起來有點愚蠢。

\documentclass{article}
\usepackage{siunitx}
\usepackage{cancel}
\begin{document}
    How can I cancel out the exponent in the numerator of the following equation?
    \begin{equation}
        W = \frac{\SI{32}{\square\meter}}{\SI{4}{\meter}} = \SI{8}{\meter}
    \end{equation}
    If instead of using \si{\square\meter} I use \si{\meter\meter}, it's easy:
    \begin{equation}
        W = \frac{\SI{32}{\cancel\meter\meter}}{\SI{4}{\cancel\meter}} = \SI{8}{\meter}
    \end{equation}
\end{document}

顯然,如果它是米的立方除以米,我想取消 3 並在其後面加上 2,但我還沒有遇到這個問題。先感謝您!

答案1

正如 Mico 所提到的,跳過siunitx所有內容並手動設定可能會更容易。如果您確實想繼續使用siunitx,您可以嘗試使用一些字距調整將 放在\cancel正確的位置。例如,您可以使用下面的一對宏1來劃掉最終的單位中的下標。

\usepackage{calc}

\newdimen{\KernAmount}

\newcommand{\cancelsup}[2]{%
  \setlength{\KernAmount}{\widthof{{\scriptsize \cancel{#1}}}*\real{-1}}%
  #2\kern\KernAmount\vphantom{}^{\cancel{\phantom{#1}}}}

\newcommand{\canceltosup}[3]{%
  \setlength{\KernAmount}{\widthof{{\scriptsize \cancel{#1}}}*\real{-1}}%
  #3\kern\KernAmount\vphantom{}^{\cancel{\phantom{#1}}}\vphantom{}^{^{#2}}}

第一個只是劃掉指數,而第二個還在取消上方以小文字形式放置替換冪。例如:

\[
    W = \frac{\cancelsup{2}{\SI{32}{\square\meter}}}{\SI{4}{\cancel\meter}} = \SI{8}{\meter}
\] \[
    W = \frac{\canceltosup{3}{2}{\SI{32}{\cubic\meter}}}{\SI{4}{\cancel\meter}} = \SI{8}{\square\meter}
\]

上述程式碼的輸出螢幕截圖

請注意,兩者都需要您為要取消的號碼提供佔位符。這是為了確保出現正確的取消形狀和位置。


1對於每個程式碼片段,這只是經過輕微測試,可能會以不可預測的方式中斷。

相關內容