如何檢測下一個字元是否為 ^?

如何檢測下一個字元是否為 ^?

我該怎麼做呢?使用\@ifnextchar^{command1}{command2}失敗。我應該做什麼來檢測 ^?

更新。事實證明該命令沒有問題,只是我的巨集定義中其他地方的名稱衝突。感謝您的快速反應!

答案1

它與^以下命令一起使用\sp

\documentclass{article}

\usepackage{amsmath}

\makeatletter
\newcommand{\foo}{%
  \@ifnextchar\sp{\cmdone}{\cmdtwo}
}

\newcommand{\cmdone}{\text{Is superscripted!}}
\newcommand{\cmdtwo}{\text{Is not superscripted!}}

\makeatother

\begin{document}

$\foo^2$

$\foo E=mc^2$
\end{document}

在此輸入影像描述

答案2

以下乳膠檔案產生的終端輸出為

yes
no

顯示\@ifnextchar已測試 true^和 false!

\makeatletter

\@ifnextchar^{\typeout{yes}\@gobble}{\typeout{no}\@gobble}^

\@ifnextchar^{\typeout{yes}\@gobble}{\typeout{no}\@gobble}!

\stop

相關內容