我正在写一份大型报告(使用该类report
),因此有一个“主”文件与所有的部分作为单独的 TeX 文件。该subfiles
包用于导入所有部分文件放入“主”文件中。
虽然从“主”文件编译时,这可以很好地工作,但在编译时,标头(使用fancyhdr
)无法正确呈现部分文件独立,因为标题定义依赖于未定义的宏,例如\thepart
。
有没有办法“检测”子文件是否正在独立编译,如果是,则指定一些可以禁用标题的内容(例如\pagestyle{empty}
)?
答案1
这电子工具箱包可以轻松测试控制序列是否已定义;这也可以使用 e-TeX 原语“手动”完成,例如\ifcsname
:
\documentclass{article}
\usepackage{etoolbox}
\begin{document}
% We could of course factor out the “is” too, but let's favor clarity over
% trying to be smart.
\verb|\thepart|
\ifdef{\thepart}{is}{is not} defined.
\verb|\thechapter|
\ifdef{\thechapter}{is}{is not} defined.
\bigskip
% Alternatively:
\verb|\thepart|
\ifcsdef{thepart}{is}{is not} defined.
\verb|\thechapter|
\ifcsdef{thechapter}{is}{is not} defined.
\bigskip
% Using the underlying e-TeX primitive (requires a TeX engine with e-TeX
% extensions, but not the 'etoolbox' package):
\verb|\thepart|
\ifcsname thepart\endcsname
is % <--- space here
\else
is not % <--- space here
\fi
defined.
% Terse version, for a change
\verb|\thechapter| is
\ifcsname thechapter\endcsname\else not \fi defined.
\bigskip
% Using TeX only (no e-TeX extension), we can test if a control sequence is
% either undefined or \let-equal to \relax:
\verb|\thepart| is
\expandafter\ifx\csname thepart\endcsname\relax
either undefined or \verb|\let|-equal to \verb|\relax|.
\else
defined.
\fi
\verb|\thechapter| is
\expandafter\ifx\csname thechapter\endcsname\relax
either undefined or \verb|\let|-equal to \verb|\relax|.
\else
defined.
\fi
\end{document}