是否可以检查文件是否列在里面\includeonly{...}
?
例如我想做这样的事情
\includeonly{file1,file2}
\if\isincluded file1 % \isincluded is a placeholder for the real test macro
\usepackage{apackage}
\fi
\begin{document}
\include{file1}
\include{file2}
\end{document}
避免apackage
在未包含 file1 时加载(因为该包的加载成本很高,例如 TikZ)
答案1
以下宏\isincluded
使用与使用相同的测试\include
(从那里复制的代码)。如果将包含第一个参数中的文件,则调用第二个参数,否则调用第三个参数。此外,宏会捕获未\includeonly
使用的情况,并包含所有文件。
\documentclass{article}
\includeonly{file,file2}
\makeatletter
\newcommand*{\isincluded}[1]{%
\@tempswatrue
\if@partsw
\@tempswafalse
\edef\reserved@b{#1}%
\@for\reserved@a:=\@partlist\do
{\ifx\reserved@a\reserved@b\@tempswatrue\fi}%
\fi
\if@tempswa
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
}
\makeatother
\isincluded{file1}{%
\usepackage{apackage}%
}{}
\begin{document}
\include{file1}
\include{file2}
\end{document}
如果\includeonly
使用 ,则开关\if@partsw
为真。然后 的文件\includeonly
可在 中使用\@partlist
。
扩展:\isincluded
带有文件列表
用例:如果包含某些文件,则必须加载资源(包)。这意味着如果\isincluded
包含 中的任一文件,则 的测试
\isincluded
结果为 true(逗号为“或”运算符):
\documentclass{article}
\includeonly{file1,file3}
\makeatletter
\newcommand*{\isincluded}[1]{%
\@tempswatrue
\if@partsw
\@tempswafalse
\edef\isincluded@list{\zap@space#1 \@empty}% removes spaces in file list
\@for\reserved@b:=\isincluded@list\do{%
\@for\reserved@a:=\@partlist\do{%
\ifx\reserved@a\reserved@b\@tempswatrue\fi
}%
}%
\fi
\if@tempswa
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
}
\makeatother
\isincluded{file2,file3}{%
\usepackage{apackage}%
}{}
\begin{document}
\include{file1}
\include{file2}
\include{file3}
\end{document}
答案2
文件\includeonly
已保存并edef
称为\@partlist
。
\documentclass{article}
\usepackage{filecontents}
\begin{filecontents}{test1.tex}
file 1
\end{filecontents}
\begin{filecontents}{test2.tex}
file 2
\end{filecontents}
\includeonly{test2, test1}
\begin{document}
\makeatletter
\@partlist
\makeatother
\meaning\@partlist
\end{document}
不太确定这是否有帮助,但你可以阅读 source2e 例程。
答案3
您可以编写自己的包装器来\includeonly
测试文件名
\documentclass{article}
\usepackage{etoolbox}
\newcommand{\myincludeonly}[1]{%
\def\do##1{\ifstrequal{##1}{file1}{\usepackage{tikz}}{}}
\docsvlist{#1}
\includeonly{#1}
}
\myincludeonly{file1,file2}
\begin{document}
\include{file1}
\include{file2}
\end{document}
答案4
以下是我对 Heiko 解决方案提出的问题。在下文中,韓國尽管\includeonly
被注释掉了,但仍是输入。
\documentclass{article}
%\includeonly{file,file2} % commented
\makeatletter
\newcommand*{\isincluded}[1]{%
\@tempswatrue
\if@partsw
\@tempswafalse
\edef\isincluded@list{\zap@space#1 \@empty}%
\@for\reserved@b:=\isincluded@list\do{%
\@for\reserved@a:=\@partlist\do{%
\ifx\reserved@a\reserved@b\@tempswatrue\fi
}%
}%
\fi
\if@tempswa
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
}
\makeatother
\isincluded{file1}{%
\usepackage{ifpdf}%
}{}
\begin{document}
\include{file1}
\include{file2}
\end{document}
我的方法是
\documentclass{article}
% Comment out or retain the following:
\includeonly{file,file2}
\makeatletter
\newif\if@tempswb
\newcommand*{\ifincluded}[1]{%
\begingroup
\@tempswbfalse
\if@partsw
\edef\@tempa{\zap@space#1 \@empty}%
\@for\reserved@b:=\@tempa\do{%
\@for\reserved@a:=\@partlist\do{%
\ifx\reserved@a\reserved@b\@tempswbtrue\fi
}%
}%
\fi
\expandafter\endgroup
\if@tempswb
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
}
\makeatother
\ifincluded{file1}{%
\usepackage{ifpdf}%
}{}
\begin{document}
\include{file1}
\include{file2}
\end{document}