引用方程式時出錯

引用方程式時出錯

我的文件中的一個方程式有一個奇怪的問題:每當我清除輸出檔案以進行新的重新編譯時,所有與參考書目相關的文件也必須重新生成,而且這段程式碼無法編譯:

\begin{equation}
    A\stackrel{\cite{A01}}{=}B
\end{equation}

錯誤:命令 \bfseries 在數學模式下無效。 A\stackrel{\cite{A01}}{=}

我發現如果我像這樣編輯文件:

\begin{equation}
    A\stackrel{\cite{A01}}{=}B
%   A=B  %First compile with this, then with the other!!
\end{equation}

我只需要註釋第一行,取消註釋第二行,編譯,切換回註解行並重新編譯,它就可以工作了。

基本上它只在新編譯時失敗,之後就沒有問題了。但這仍然很煩人,因為我每天 24/7 都在寫論文,而且每天至少要做幾次。
另外:當提交我的論文(包括 LaTeX 程式碼)時,有人會遇到這個問題,我不想強迫他們找出問題所在來編譯我的檔案。

感謝您對此的任何幫助!

微量元素:

\documentclass{scrreprt}
\usepackage{natbib}
\usepackage{hyperref}
\usepackage{filecontents}

\begin{filecontents*}{foo.bib}
    @misc{A01,
        author = {Author, A.},
        year = {2001},
        title = {Alpha},
    }
    @misc{B02,
        author = {Buthor, B.},
        year = {2002},
        title = {Bravo},
    }
\end{filecontents*}

\begin{document}
    \begin{equation}
    A\stackrel{\cite{A01}}{=}B
%   A=B  %First compile with this, then with the other!!
    \end{equation}

    \nocite{*} 
    \bibliography{foo}
    \bibliographystyle{plain}
\end{document}

答案1

\begin{equation}
  A\stackrel{\cite{A01}}{=}B
\end{equation}

處於\cite{A01}數學模式。您可以透過編寫類似的內容來輕鬆驗證這一點x^2

當 LaTeX 嘗試在第一次運行時以數學模式處理引文時,當它仍然未知時,\reset@font\bfseries ? natbib想要列印會導致您看到的錯誤:Command \bfseries invalid in math mode

避免這種情況的方法是確保返回文字模式。我會透過加載amsmath並說來做到這一點

A\stackrel{\text{\cite{A01}}}{=}B

順便說一句,該錯誤不會發生在 的標準定義中\cite(即,如果您不加載natbib),因為它會在 a 中列印問號\hbox,從而打破數學模式

\hbox{\reset@font\bfseries ?}

相關內容