删除 \documentclass 的最不具侵入性且最优雅的方法是什么?

删除 \documentclass 的最不具侵入性且最优雅的方法是什么?

上下文如下:我有一个名为(比如说)的文件00.tex,我以以下详细方式使用它。

编译文件,生成辅助 tex 文件,然后执行\include{00.tex}。动机是辅助文件被命名为

slides-1-1-introduction.tex
handouts-1-1-introduction.tex
slides-1-2-administration.tex
handouts-1-2-administration.tex
...
slides-2-1-landing.tex
handouts-2-1-landing.tex
slides-2-2-speech.tex
handouts-2-2-speech.tex

每个部分都代表了分为多个章节和小节的大课程的一部分和一个视角。

现在,文件00.tex必须有\documentclass,因为它是单独编译的,要么用于生成辅助文件,要么用于生成完整演示文稿。另一方面,\documentclass如果它包含在其他文件中,则不能有。

让事情变得更加复杂的是,\documentclass不一定是第一个命令,例如,您必须在发出它之前向类声明选项。

我可以想到涉及\documenclass从辅助文件内部重新定义的技巧,但我相信这些人有更优雅的想法。

答案1

最简单的解决方案是不将 放在\documentclass您包含的文件中。如果11.tex没有,\documentclass那么您可以\include毫无问题地使用它,要制作独立版本,您只需要一个额外的文件,11-only.tex如下所示

 \documentclass{whatever}
 \usepackage{whatever}
 \begin{document}
 \input{11}
 \end{document}

答案2

我提供这个答案是为了帮助那些可能需要为大型课程编写幻灯片的人。为了让作者专注于内容而不是熟悉或不太熟悉的命令,主文件00.tex仅包含绝对最少的内容:

\usepackage{00}
\input{00-nesting.tex}

\ifdef\unitFileName\relax{
% Feel free to change, if you wish, for some reason, to 
  % use the main file for \include{}ing one or more files.
  \includeonly{nothing} 
}

\title{my title}
\institute{where I am}

\begin{document}
\fullTOC
\UNIT{Introduction}{introduction}{introduction}
  \unit{Administration}{administration}
  \unit{Motivation}{motivation}
  \unit{History of Programming Languages}{history}
  \unit{Programming Paradigms}{paradigms}
  \unit{Syntax Specification}{syntax}
  \unit{Syntactical Elements}{elements}
  \unit{Programming Language Design}{design}
  \unit{Hello, World!}{hello}

\UNIT{Values and Types}{values-and-types}
  \unit{Value Systems}{value-systems}
  \unit{Why Types}{why-types}
  \unit{Type Constructors}{type-constructors}
  \unit{Atomic Types}{atomic}
\UNIT{Advanced Typing}{advanced-typing}
  \unit{Classification of Type Systems}{classification}
  \unit{Structural vs.\ Nominal Typing}{structural}
  \unit{Polymorphism}{polymorphism}
\UNIT{Storage}{storage}
 \unit{Storage Models}{models}
 \unit{Arrays}{arrays}
 \unit{Variables' Life time}{variables-lifetime}
 \unit{Automatic Memory Management}{automatic-memory-management}
 \unit{Functional Abstractions}{functional}
 \unit{Value vs.\ Reference Semantics}{reference-vs-value-semantics}
 \unit{Run Time Type Information}{run-time-type-information}
\UNIT{Miscellaneous}{misc}
 \unit{Commands}{commands}
    \subsubsection{Recursive Structure of Commands} 
    \subsubsection{Atomic Commands in \protect\Java, \protect\Pascal and \protect\CC} 
    \subsubsection{Command Constructors} 
    \subsubsection{Sequencers}
 \unit{Definitions and Declarations}{declarations}
 \unit{Management of Names Spaces in \protect\Java}{name-spaces-in-java}
 \unit{Compiling, Loading and Running \protect\Java Programs}{compiling-and-running-java}
\end{document}

(临时名称00-nesting.tex) 包含大部分丑陋的内容。(请注意,这是当前正在进行的版本的快照)

\usepackage{etoolbox}

\makeatother

\newcommand\handoutsHeaderFileName{-handouts-header.tex}
\newcommand\slidesHeaderFileName{-slides-header.tex}

\WarningFilter{latex}{Overwriting file}
\typeout{Generating common header file for handouts (that is, in beamer's "handout" mode): './handout-header.tex'}

\begin{filecontents}{\handoutsHeaderFileName}
  \PassOptionsToPackage{fleqn}{amsmath}
  \PassOptionsToPackage{hidelinks,final}{hyperref}
  \PassOptionsToPackage{usenames,dvipsnames}{xcolor}
  \documentclass[t]{beamer}
  \input{00.tex}
\end{filecontents}

\typeout{Generating common header file for slides (that is, in beamer's "beamer" mode): './slides-header.tex'}
\begin{filecontents}{-slides-header.tex}
  \PassOptionsToPackage{fleqn}{amsmath}
  \PassOptionsToPackage{hidelinks,final}{hyperref}
  \PassOptionsToPackage{usenames,dvipsnames}{xcolor}
  \documentclass[t]{beamer}
  \hypersetup{pdfpagemode=FullScreen}
\end{filecontents}


\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
}

\newcommand\unit[2]{%
  \isincluded{#2}{\fullTOC}{}%
  \subsection{#1}%
  \CreateIncludingFileSubsection{#1}{#2}%
  \include{#2}%
  \emptyTOC
}
\newcommand\UNIT[2]{%
%  \isincluded{#2}{\fullTOC}{}%
  \section{#1}%
  \CreateIncludingFileSection{#1}{#2}%
  \include{#2}%
%  \emptyTOC
}

  % 1 = kind (handouts/slides)  
  % 2 = section, 
  % 3 = subsection, 
  % 4 = file name, 
  % 5 = Header file name, 
  % 6 = Title. 
  \newcommand\toFile[6]{% 
      \immediate\openout\tempfile=#1-#2-#3-#4.tex%
      \immediate\write\tempfile{\@percentchar\space Hi! I am a file which was automatically generated on \today;} 
      \immediate\write\tempfile{\@percentchar\space Any changes you should make to me, are very ephemeral in nature. Sooner}
      \immediate\write\tempfile{\@percentchar\space rather than later, they will be overriden by the program that generated}
      \immediate\write\tempfile{\@percentchar\space me. May the odds be ever in your favor!}
      \immediate\write\tempfile{\noexpand\input{#5}}%
      \immediate\write\tempfile{\unexpanded{\def\unitFileName{#4}}}%
      \immediate\write\tempfile{\unexpanded{\def\unitTitle{#6}}}%
      \immediate\write\tempfile{\unexpanded{\includeonly{\unitFileName}}}%
      \immediate\write\tempfile{\noexpand\input{00.tex}}%
      \immediate\closeout\tempfile
  }
  \newcommand\makeHandouts[4]{\toFile{handouts}{#1}{#2}{#3}{\handoutsHeaderFileName}{#4}}
  \newcommand\makeSlides[4]{\toFile{slides}{#1}{#2}{#3}{\slidesHeaderFileName}{#4}}
  \newcommand\makeInclusion[4]{\makeHandouts{#1}{#2}{#3}{#4}\makeSlides{#1}{#2}{#3}{#4}}


\ifdef{\unitFileName}{}
\ifdef\unitFileName{
  \typeout{LaTeXing a specific section/subsection entitled "\unitTitle", residing in file \unitFileName}
  \newcommand\CreateIncludingFileSubsection[2]{\relax}%
  \newcommand\CreateIncludingFileSection[2]{\relax}%
}{
  \typeout{LaTeXing the full file \jobname}
  \newwrite\tempfile
  \newcommand\CreateIncludingFileSubsection[2]{\makeInclusion{\thesection}{\thesubsection}{#2}{#1}}
  \newcommand\CreateIncludingFileSection[2]{\makeInclusion{\thesection}{0}{#2}{#1}}
  \input{\slidesHeaderFileName}
}

相关内容