
可能的重複:
如何讓 LaTeX 警告未引用的數字?
在我的文件中,我有幾個圖形,其中大多數是我\ref{}
在將它們放入文件後立即創建的引用(使用)。然而,我忘記在文件中引用一些數字,導致一些數字「懸掛」而沒有任何引用,這是我的錯誤,必須更正。
所以,我的問題是:
如果從未引用某個數字,是否有辦法強制 Latex 發出警告?
答案1
這refcheck
包在這裡可能有用。
下面的 MWE 有一個未引用的圖,因此refcheck
在您的 pdf 中圍繞它放置了一個警告?
。
\documentclass{article}
\usepackage{refcheck}
\begin{document}
\begin{figure}
\centering
\rule{20pt}{30pt}
\caption{}
\label{fig:myfig}
\end{figure}
\end{document}
一旦您引用該圖,該圖?
就會被刪除。請注意,此方法確實依賴每個圖形都有一個標籤。
請注意,我已經示範refcheck
了figure
進行了演示,但它的工作方式與任何事物有一個標籤(table
、chapter
、equation
等)。
答案2
這是一個手工製作的解決方案:
將以下內容加入序言中:
\makeatletter
\let\LaTeX@original@label\label
\let\LaTeX@original@ref\ref
\newcommand{\warnunused}{}
\renewcommand{\label}[1]{%
\expandafter\global\expandafter\def\csname warning@#1\endcsname{%
\typeout{warning: label #1 was not referenced.}}%
\g@addto@macro\warnunused{%
\csname warning@#1\endcsname}
\LaTeX@original@label{#1}}
\renewcommand{\ref}[1]{%
\expandafter\global\expandafter\def\csname warning@#1\endcsname{}
\LaTeX@original@ref{#1}}
\makeatother
以及之前的以下內容\end{document}
:
\warnunused
現在,在每次 LaTeX 運行中,對於每個未引用的標籤,您都會得到一行
warning: label (your label here) was not referenced.
在您的標準輸出和 refwarn.log 中。
如果您喜歡直接在文件中顯示警告,請刪除警告巨集中的\typeout
(並可能\texttt
在 周圍添加 a 並在末尾添加#1
a )。\\