
以下 MWE 可以使用以下命令進行編譯,hyperref
但不能使用以下命令進行編譯nohyperref
:
\documentclass{article}
\usepackage{xifthen}
\usepackage{nohyperref} %with hyperref it compiles perfectly wihtout warnings
\newcommand{\f}[1]{
\ifthenelse{\equal{#1}{}}{f}{f(#1)}
}
\newcommand{\D}{\hyperref[eq:D]{\normalcolor D}}
\begin{document}
\begin{equation}\label{eq:D}
\f{\D}
\end{equation}
\end{document}
當我\ifthenelse{\equal{#1}{}}
用\ifblank{#1}
它替換時,它至少可以編譯,但仍然有錯誤。
答案1
我的印像是您使用了錯誤的工具(我指的是\ifthenelse
)。不管怎樣,只要堅強起來\hyperref
,你就會上路的。
\documentclass{article}
\usepackage{xifthen}
\usepackage{nohyperref} %with hyperref it compiles perfectly wihtout warnings
\MakeRobust{\hyperref}
\newcommand{\f}[1]{%
\ifthenelse{\equal{#1}{}}{f}{f(#1)}%
}
\newcommand{\D}{\hyperref[eq:D]{\normalcolor D}}
\begin{document}
\begin{equation}\label{eq:D}
\f{\D}
\end{equation}
\end{document}
答案2
我從不使用該包沒有超引用。
為了使用 hyperref 套件在開啟/關閉超連結之間進行切換,我讓 TeX 在全域範圍內執行通常僅在內部執行的操作超引用的環境NoHyper
;書籤也要注意:
\documentclass{article}
\usepackage{xifthen}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% For turning off hyperlinks activate, %
%% for turning on hyperlinks turn into a comment %
%% the following line: %
\PassOptionsToPackage{bookmarks=false}{hyperref}\newcommand\ProbablySwitchOffHyperlinks{\NoHyper}%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{hyperref}
\providecommand\ProbablySwitchOffHyperlinks{}%
\ProbablySwitchOffHyperlinks
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\f}[1]{\ifthenelse{\equal{#1}{}}{f}{f(#1)}}
\newcommand{\D}{\hyperref[eq:D]{\normalcolor D}}
\begin{document}
\tableofcontents
\section{bla}
\section{blu}
\begin{equation}\label{eq:D}
\f{\D}
\end{equation}
\end{document}
答案3
\ifthenelse{\equal{#1}{}}...
巨集定義的⟨替換文字⟩中的測試\f
似乎旨在近似測試參數表示的參數是否#1
包含標記,其處理結果是在輸出中創建一個區域可製作為可點擊超鏈接的文件(.pdf 文件或.dvi 文件)。
如果您這樣做\DeclareRobustCommand{\D}
而不是\newcommand{\D}
,那麼您的程式碼編譯時不會出現錯誤訊息:
\documentclass{article}
\usepackage{xifthen}
\usepackage{nohyperref} %with hyperref it compiles perfectly wihtout warnings
\newcommand{\f}[1]{%
\ifthenelse{\equal{#1}{}}{f}{f(#1)}%
}
\DeclareRobustCommand{\D}{\hyperref[eq:D]{\normalcolor D}}
\begin{document}
\begin{equation}\label{eq:D}
\f{\D}
\end{equation}
\begin{equation}\label{eq:empty}
\f{}
\end{equation}
\end{document}
此解決方案無需載入其他套件,也無需修補現有巨集。
該解決方案也應該適用於禁止 hyperref 的期刊。 ;-)
我認為將方程式的一個組成部分轉換為導致同一方程式數量的超連結可能與現實生活場景不同,但您的範例旨在展示與命令行為的需要相關的問題,具體取決於以下情況:他們被執行了。 (在評估 -test 的情況下,\ifthenelse
該命令的\hyperref
行為方式與在輸出檔案(.dvi-file/.pdf-file)中排版和建立超連結的情況下的行為不同。)
\ifthenelse{\equal{#1}{}}...
巨集定義的⟨替換文字⟩中的測試\f
似乎旨在近似測試參數表示的參數是否#1
包含標記,其處理結果是在輸出中創建一個區域可製作為可點擊超鏈接的文件(.pdf 文件或.dvi 文件)。
\ifthenelse
本地範圍內其他事物下的命令\let\equal\TE@equal
在評估其測試參數時執行。
這個事實可以用於分叉,這取決於當前是否處於評估巨集實例的測試參數的情況\ifthenelse
,並且在這種情況下省略/刪除在這種情況下無法執行/不需要的標記:
\documentclass{article}
\usepackage{xifthen, xcolor}
\usepackage{nohyperref}
\makeatletter
\newcommand\CheckWhetherCurrentlyEvaluatingIfthenelseTest{%
\ifx\equal\TE@equal\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
}%
\newcommand{\D}{%
\CheckWhetherCurrentlyEvaluatingIfthenelseTest{\@firstofone}{\hyperref[eq:D]}%
{\CheckWhetherCurrentlyEvaluatingIfthenelseTest{}{\normalcolor}D}%
}%
\makeatother
\newcommand{\f}[1]{\ifthenelse{\equal{#1}{}}{f}{f(#1)}}
\begin{document}
\color{blue}
\tableofcontents
\section{bla}
\section{blu}
\begin{equation}\label{eq:D}
\f{\D}
\end{equation}
\begin{equation}\label{eq:empty}
\f{}
\end{equation}
\end{document}
這個例子有點過分了。但它說明瞭如何將可擴展命令設計為根據擴展/執行的情況來執行不同的操作。
不幸的是,LaTeX 中沒有“情境管理”,您可以隨時可靠地查詢 LaTeX 當前處於何種情境,更不用說擴展情境類型的全部內容和命令的情境相關行為的全部內容了。