是否可以在输入或包含文件中使用 usepackage?

是否可以在输入或包含文件中使用 usepackage?

换句话说,我可以将包含文件的软件包依赖项放在文件本身中吗?还是必须使用主文件来实现这一点?

我遇到的情况是,我在一个main.tex文件中包含了不同的幻灯片。不同的幻灯片使用使用不同包的命令。

\documentclass{beamer}
\usepakcage{apackage}
\usepackage{bpackage}
\title{Title}
\author{hola}
\begin{document}
\include{slide1} %slide1.tex needs apackage
\include{slide2} %slide2.tex needs bpackage
\end{document}

我可以以不需要知道幻灯片所需的包的方式来slide1.tex编写吗?slide2.texmain.tex

\documentclass{beamer}
\title{Title}
\author{hola}
\begin{document}
\include{slide1} %slide1.tex needs apackage, slide1.tex declares its own needed package
\include{slide2} %slide2.tex needs bpackage, slide2.tex declares its own needed package
\end{document}

并且(不是工作代码)slide1.tex

"\usepackage{apackage}"
\begin{frame}
\commandfromapackage{...}
\end{frame}

这当然是 C 等语言中非常常见的特性,其中包含的文件可以声明自己的依赖项。

当然,解决办法可能是这样写main.tex,但那样就太糟糕了。

\documentclass{beamer}
\input{slide1_packages}
\input{slide2_packages}
\title{Title}
\author{hola}
\begin{document}
\include{slide1}
\include{slide2}
\end{document}

答案1

David Carlisle 已经提到了独立包,这是 MWE 如何使用它:

主要.tex:

\documentclass{article}
\usepackage[subpreambles]{standalone}

% https://tex.stackexchange.com/q/120060/120953
% "packages that do some of their jobs \AtBeginDocument are likely to fail" with subpreambles (egreg)
% => must be loaded in main document as well
\usepackage{siunitx}

\begin{document}
\input{content/section-01}
\input{content/section-02}
\end{document}

内容/section-01.tex:

\documentclass[crop=false]{standalone}

\usepackage{tikz}
\usetikzlibrary{lindenmayersystems,shadings}
\pgfdeclarelindenmayersystem{Koch curve}{\rule{F -> F-F++F-F}}

\begin{document}
\section{TikZ}
\tikz\shadedraw[shading=color wheel] [l-system={Koch curve, step=2pt, angle=60, axiom=F++F++F, order=4}] lindenmayer system -- cycle;
\end{document}

内容/section-02.tex:

\documentclass[crop=false]{standalone}

\usepackage{siunitx}
\usepackage{booktabs}

\begin{document}
\section{siunitx}
\begin{center}
\begin{tabular}{ c S[table-format=7.6] }
\toprule
Prefix & {Value} \\
\midrule
\si{\mega} & 1 000 000 \\
\si{\kilo} & 1 000 \\
\si{\milli} & .001 \\
\si{\micro} & .000 001 \\
\bottomrule
\end{tabular}
\end{center}
\end{document}

content/section-01.tex 和 content/section-02.tex 可以单独编译,也可以包含在 main.tex 中。

答案2

一般情况下不会。(如果与 C 进行比较,请记住 TeX 是一个宏处理器,因此您应该与 C 预处理器以及同一文件中两个 #include 文件所做的任何定义进行比较)。软件包只能包含在序言中。

然而,对于幻灯片来说,如果它是一段独立的文本,并且与其他幻灯片中的文本依赖性很小,那么您可以将幻灯片编译为单独的 1 页文档,然后包含生成的 pdf,因此

\begin{slide}
\includegraphics{slide1.pdf}
\end{slide}

standalone软件包有一些工具可以在后台使用这种技术来安排文档包含,或者您也可以直接这样做。

答案3

从评论来看,您真正想要做的是仅在特定包含文件需要时才加载特定包。正如其他人所说,您无法在包含文件中执行此操作,因为\usepackage{...}需要进入序言。您可能还希望这相当“自动化”,这样您就不必继续检查每个包含文件到底需要哪些包。

您可以做的是创建自己的“样式文件”,以便为您加载所有必需的包。我考虑使用myfileoptions.sty以下命令加载的“包”,例如

\usepackage[filea,filec]{myfileoptions}

并且该包足够智能,myfileoptions知道为输入文件加载哪些包filea.texfileb.tex

这是一个可能的模型myfileoptions.sty

\RequirePackage{pgfopts}

\newif\ifTikz\Tikzfalse
\newif\ifTcolorbox\Tcolorboxfalse

\pgfkeys{/myfileoptions/.is family, /myfileoptions,
  filea/.code = {\global\Tikztrue},
  fileb/.code = {\global\Tcolorboxtrue},
  filec/.code = {\global\Tikztrue
                 \global\Tcolorboxtrue}
}

\ProcessPgfOptions{/myfileoptions}% process options

\ifTikz
   \RequirePackage{tikz}
\fi

\ifTcolorbox
   \RequirePackage{tcolorbox}
\fi

这一切都是为了加载普格福普特包来处理提供给类文件的选项。类文件中有一组 if\newif语句,用于表示每个可能加载的包,\pgfkeys用于在文件需要所述包时打开这些开关。最后,myfileoptions.sty使用相应的 加载所有需要的包\newif

当然,您仍然需要\include{filea}在想要插入的地方有fileaetc。

相关内容