当我写演讲稿时,我会把它们写在一个文档中,比如这个 mwe
% document name : main.tex
\documentclass{scrbook}
\usepackage{etoc}
\newcommand{\chaptertoc}[1][Contenu du chapitre]{%
%\etocsettocstyle{\addsec*{#1}}{}%
\etocsettocstyle{\addsec*{#1\\\rule{\textwidth}{0.4pt}} \small }{}
\localtableofcontents%
}
\usepackage{hyperref}
\usepackage[]{blindtext}
\begin{document}
\tableofcontents
\chapter{Chapter One}
\chaptertoc{}
\section{section one of chapter one}
\Blindtext
\section{section two of chapter one}
\Blindtext
\chapter{Chapter Two}
\chaptertoc{}
\section{section one of chapter two}
\Blindtext
\section{section two of chapter two}
\Blindtext
\section{section three of chapter two}
\Blindtext
\end{document}
我继续修改和扩展这个 main.tex 文档,并根据讲座的进度与我的学生分享内容,通常是逐章分享。因此,我创建了一个新文档,其中包含以下内容:
% document name: pdf_chapter1.tex
\documentclass{article}
\usepackage[]{pdfpages}
\usepackage{hyperref}
\begin{document}
\includepdf[pages={3-5}]{main}
\end{document}
并分享编译结果。这个工作流程很方便,因为我已经这样做了很多年,但问题是我丢失了文档中章节、目录和公式的交叉引用的所有链接。你能帮我解决这个问题吗?如果不可能,建议另一种工作流程吗?
答案1
我建议使用它\includeonly
来编译文档的较小部分。确保先编译整个文档,以便较小部分的页码和交叉引用是正确的。
\documentclass{scrbook}
\usepackage{etoc}
\newcommand{\chaptertoc}[1][Contenu du chapitre]{%
%\etocsettocstyle{\addsec*{#1}}{}%
\etocsettocstyle{\addsec*{#1\\\rule{\textwidth}{0.4pt}} \small }{}
\localtableofcontents%
}
\usepackage{hyperref}
\usepackage[]{blindtext}
\begin{filecontents*}[overwrite]{cap1.tex}
\chapter{Chapter One}
\chaptertoc{}
\section{section one of chapter one}
\Blindtext
\[
E = mc^{2}\label{eq}
\]
\section{section two of chapter one}
\Blindtext
\end{filecontents*}
\begin{filecontents*}[overwrite]{cap2.tex}
\chapter{Chapter Two}
\chaptertoc{}
\section{section one of chapter two}
\Blindtext
\section{section two of chapter two}
\Blindtext
\section{section three of chapter two}
\Blindtext
\ref{eq}
\end{filecontents*}
\includeonly{cap2}
\begin{document}
\tableofcontents
\include{cap1}
\include{cap2}
\end{document}
答案2
\include
也许类似于/机制的机制\includeonly
可能会很好。
当某些内容(文件部分而不是文件)未经排版处理时,该机制还会将带有计数器值的所谓检查点写入辅助文件。
\include
但与/机制有以下区别\includeonly
:
不是读取具有特定名称的文件,而是读取 LaTeX 文件的各个部分,这些部分被赋予名称并且在 .tex-input-file 中嵌套在序列
\Filepart{⟨name of filepart⟩}
与序列之间\EndOfFilepart
。类似于命令
\includeonly{⟨comma list with the names of the files that are to be read in⟩}
,应该有一个命令\OnlyFileparts{⟨comma list with the names of those fileparts that are to be read in for typesetting⟩}
。如果不应读入文件部分进行排版,则 LaTeX 必须切换到逐字模式并从文件中读取并创建字符标记并将其丢弃,直到
\EndOfFilepart
找到字符标记序列。如果文件部分的内容未在当前 LaTeX 运行中排版,但仍可读取包含此文件部分中定义的交叉引用标签和类似数据的相关辅助文件,则必须有一个可选参数,在加载 hyperref 包的情况下,可以指定包含此文件部分的另一个 PDF 文件的 URL,以便在交叉引用在非排版文件部分中定义的交叉引用标签时,会出现指向该其他 PDF 文件的外部链接,而不是没有目的地的内部链接(因此 pdfTeX 引擎的目的地自动固定到第 1 页)。
通过以下最小工作示例,我们可以将以下两个组件组合在一起:
\OnlyFilepart{⟨comma list with the names of those fileparts that are to be read in for typesetting⟩}
和
\Filepart[⟨URL of pdf-file containing this part⟩]{⟨name of filepart⟩}...\EndOfFilepart
\OnlyFileparts
只能出现在主 .tex 文件的前言中。- 使用
\include
/\includeonly
机制时,检查点的名称与文件的名称相对应。使用\Filepart
...\EndOfFilepart
/\OnlyFileparts
机制时,检查点从数字 1 开始连续编号,并且检查点和关联的辅助文件/.aux 文件的名称符合方案Filepart⟨number of the checkpoint belonging to the filepart⟩
。这样,无论如何都必须一起读入的多个文件部分可以具有相同的名称。 - 在未排版文件部分的情况下,该机制的一部分是逐字读取该文件部分并创建字符标记,然后将其丢弃,直到
\EndOfFilepart
找到序列,该机制不是逐行读取,而是逐个字符读取。一方面,这样一行中不会丢失任何字符。另一方面,该机制在较慢的计算机上速度较慢。 - 不要在文件部分内嵌套文件部分。
\Filepart
并且匹配\EndOfFilepart
必须在同一个范围/组中。\Filepart
并且匹配\EndOfFilepart
必须在同一个.tex 输入文件中。\Filepart[⟨URL of pdf-file containing this part⟩]{⟨name of filepart⟩}...\EndOfFilepart
既不能出现在宏参数中,也不能出现在宏的替换文本中,也不能成为令牌寄存器值的组成部分等。- 以下最小工作示例的代码是在安装了 TeX Live 2020 的智能手机上编写的。它使用了 expl3 编程环境提供的功能。为了进行测试,它是使用 pdflatex [pdfTeX,版本 3.14159265-2.6-1.40.21 (TeX Live 2020) (预加载格式=pdflatex),LaTeX2e <2020-10-01> 补丁级别 4,L3 编程层 <2021-02-18>] 编译的。
- 在较新版本的 LaTeX 2ε 中,新钩子系统可用,
\include
/\includeonly
机制附带一些钩子,用于每次包含文件时执行一些标记。使用\Filepart
...\EndOfFilepart
/\OnlyFileparts
机制时,不会实现任何钩子/不使用 LaTeX 2ε 的新钩子系统。这是因为此答案的初始版本的作者还不太熟悉 LaTeX 2ε 的新钩子系统。 \include
/机制\includeonly
只有在激活之前\includeonly
,对整个文档进行了编译,所有文件都按需要多次包含,以获得所有交叉引用等都匹配的文档时才会起作用。类似地,\Filepart
...\EndOfFilepart
/\OnlyFileparts
机制只有在激活之前\OnlyFileparts
,对整个文档进行了编译,所有文件部分都按需要多次包含,以获得所有交叉引用等都匹配的文档时才会起作用。- 代码尚未经过严格测试。使用时请自负风险。
\makeatletter
%----------------------------------------------------------------------------------------------------
% Infrastructure for switching to verbatim-mode and gobbling things until encountering
% \EndDocumentPart
%----------------------------------------------------------------------------------------------------
\ExplSyntaxOn
\cs_new_eq:NN \UD@StringCase \str_case:nnTF
\cs_new_eq:NN \UD@StringTail \str_tail:n
\cs_new_eq:NN \UD@IfEmpty \tl_if_empty:nTF
%\cs_new_eq:NN \UseName \use:c
%\cs_new:Npn \ExpandArgs #1
% {
% \cs_if_exist_use:cF { exp_args:N #1 }
% { \msg_expandable_error:nnn { kernel } { unknown-arg-expansion } {#1} }
% }
%\msg_new:nnn { kernel } { unknown-arg-expansion }
% { Unknown~arg~expansion~"#1" }
\ExplSyntaxOff
\newcommand\UD@SwitchToVerbatimAndGgobbleToEndOfFilepart[1]{%
\begingroup
\endlinechar=-1\relax
\let\do\@makeother\dospecials\do\^^I\do\^^M%
\UD@GgobbleToEndOfFilepartLoop{#1}{}%
}%
\begingroup
\newcommand\UD@GgobbleToEndOfFilepartLoop[4]{%
\if#4Z\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
{%
\endgroup
\newcommand\UD@GgobbleToEndOfFilepartLoop[3]{%
\UD@StringCase{##2##3}{#1{#3}{\endgroup##1}}{}{%
\UD@IfEmpty{##2}%
{\UD@GgobbleToEndOfFilepartLoop{##1}{##3}}%
{\ExpandArgs{ne}\UD@GgobbleToEndOfFilepartLoop{##1}{\UD@StringTail{##2}}{##3}}%
}%
}%
}{%
\UD@GgobbleToEndOfFilepartLoop
{#1#2}%
{{#3#4}{\UD@GgobbleToEndOfFilepartLoop{##1}{##2##3}}}%
{#3#4}%
}%
}%
\@firstofone{%
\let\do\@makeother\dospecials\do\^^I\do\^^M%
\UD@GgobbleToEndOfFilepartLoop{}{}{}%
}\EndOfFilepartZ%
%----------------------------------------------------------------------------------------------------
% Infrastructure for patching/restoring \newlabel and \new@label@record:
%----------------------------------------------------------------------------------------------------
\newcommand\UD@PassFirstToSecond[2]{#2{#1}}%
\@ifdefinable\UD@Savednewlabel{}%
\@ifdefinable\UD@Savednew@label@record{}%
\newcommand*\UD@ResetNewlabelAndNew@label@record[1]{%
\AtIfFilepartIncluded{#1}{}{%
\csname @\ifx\csname ver@hyperref.\@pkgextension\endcsname\relax gobble\else firstofone\fi\endcsname
{\let\newlabel\UD@Savednewlabel}%
\let\new@label@record\UD@Savednew@label@record
}%
}%
\newcommand\UD@SwitchNewlabelAndNew@label@record[2]{%
\AtIfFilepartIncluded{#2}{}{%
\csname @\ifx\csname ver@hyperref.\@pkgextension\endcsname\relax gobble\else firstofone\fi\endcsname
{%
\let\UD@Savednewlabel\newlabel
\def\newlabel{\UD@Patchednewlabel{#1}}%
}%
\let\UD@Savednew@label@record\new@label@record
\def\new@label@record{\UD@Patchednew@label@record{#1}}%
}%
}%
\@ifdefinable\UD@Patchednewlabel@AddURL{%
\long\def\UD@Patchednewlabel@AddURL#1#2#3#4#5#6\\{{#2}{#3}{#4}{#5}{#1}}%
}%
\newcommand\UD@Patchednewlabel[3]{%
\expandafter\UD@PassFirstToSecond
\expandafter{%
\UD@Patchednewlabel@AddURL{#1}#3{}{}{}{}\\%
}%
{\UD@Savednewlabel{#2}}%
}%
\newcommand\UD@Patchednew@label@record[3]{%
\UD@Savednew@label@record{#2}{#3{xr-url}{#1}}%
}%
%----------------------------------------------------------------------------------------------------
% Infrastructure for \Filepart and \EndOfFilepart
%----------------------------------------------------------------------------------------------------
\newwrite\UD@Filepartaux
\newcommand\UD@previous@auxout{}%
\newif\ifUD@FilepartSW\UD@FilepartSWfalse
\newcommand\UD@FilepartNesting{0}%
\newcommand\UD@FilepartList{}%
\newcounter{UD@FilepartCheckpoints}%
%----------------------------------------------------------------------------------------------------
\newcommand\OnlyFileparts[1]{%
\UD@FilepartSWtrue
\let\UD@FilepartList\@empty
\@for\reserved@a:=#1\do
{%
\ifx\UD@FilepartList\@empty
\expandafter\def\expandafter\UD@FilepartList\expandafter{\reserved@a}%
\else
\expandafter\expandafter\expandafter\def
\expandafter\expandafter\expandafter\UD@FilepartList
\expandafter\expandafter\expandafter{\expandafter\UD@FilepartList\expandafter,\reserved@a}%
\fi
}%
}%
\@onlypreamble\OnlyFileparts
%----------------------------------------------------------------------------------------------------
\NewDocumentCommand\Filepart{om}{%
\relax
\xdef\UD@FilepartNesting{\the\numexpr((\UD@FilepartNesting)+1)\relax}%
\ifnum\UD@FilepartNesting>1 \expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
{%
\@latex@error{\string\Filepart\space cannot be nested}\@eha
\UD@SwitchToVerbatimAndGgobbleToEndOfFilepart{\EndOfFilepart}%
}%
{%
\clearpage
\addtocounter{UD@FilepartCheckpoints}\@ne
\if@filesw
\IfNoValueF{#1}{%
\immediate\write\@auxout{\string\UD@SwitchNewlabelAndNew@label@record{#1}{#2}}%
}%
\immediate\write\@auxout{\string\@input{Filepart\arabic{UD@FilepartCheckpoints}.aux}}%
\IfNoValueF{#1}{%
\immediate\write\@auxout{\string\UD@ResetNewlabelAndNew@label@record{#2}}%
}%
\fi
\let\UD@previous@auxout\@auxout
\@tempswatrue
\AtIfFilepartIncluded{#2}%
{%
\let\@auxout\UD@Filepartaux
\if@filesw
\immediate\openout\UD@Filepartaux "Filepart\arabic{UD@FilepartCheckpoints}.aux"
\immediate\write\UD@Filepartaux{\relax}%
\fi
}{%
\UD@SwitchToVerbatimAndGgobbleToEndOfFilepart{%
\deadcycles\z@
\@nameuse{cp@Filepart\arabic{UD@FilepartCheckpoints}}%
\let\@auxout\UD@previous@auxout
\xdef\UD@FilepartNesting{\the\numexpr((\UD@FilepartNesting)-1)\relax}%
}%
}%
}%
}%
%----------------------------------------------------------------------------------------------------
\newcommand\AtIfFilepartIncluded[1]{%
\begingroup
\@tempswatrue
\ifUD@FilepartSW
\@tempswafalse
\def\reserved@b{#1}%
\@for\reserved@a:=\UD@FilepartList\do{%
\ifx\reserved@a\reserved@b\@tempswatrue\fi
}%
\fi
\expandafter\endgroup
\if@tempswa\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
}%
%----------------------------------------------------------------------------------------------------
\newcommand*\EndOfFilepart{%
\ifnum\UD@FilepartNesting=1 \expandafter\@firstofone\else\expandafter\@gobble\fi
{%
\clearpage
\if@filesw
\immediate\write\UD@Filepartaux{\string\@setckpt{Filepart\arabic{UD@FilepartCheckpoints}}\@charlb}%
{\let\@elt\@wckdocumentptelt \cl@@ckpt}%
\immediate\write\UD@Filepartaux{\@charrb}%
\immediate\closeout\UD@Filepartaux
\fi
\let\@auxout\UD@previous@auxout
}%
\ifnum\UD@FilepartNesting<1 \expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
{\@latex@error{Unmatched \string\EndOfFilepart}\@eha}%
{\xdef\UD@FilepartNesting{\the\numexpr((\UD@FilepartNesting)-1)\relax}}%
}%
\newcommand*\@wckdocumentptelt[1]{%
\immediate\write\UD@Filepartaux{\string\setcounter{#1}{\the\@nameuse{c@#1}}}%
}%
%----------------------------------------------------------------------------------------------------
\makeatother
\documentclass{scrbook}
\usepackage{etoc}
\newcommand{\chaptertoc}[1][Contenu du chapitre]{%
%\etocsettocstyle{\addsec*{#1}}{}%
\etocsettocstyle{\addsec*{#1\\\rule{\textwidth}{0.4pt}}\small}{}%
\localtableofcontents
}
%\OnlyFileparts{Lecture2}
%\OnlyFileparts{Lecture1}
%\OnlyFileparts{Lecture3,Lecture4}
\usepackage[pdfnewwindow=true]{hyperref}
\usepackage[]{blindtext}
\begin{document}
\Filepart{ToC}%
\tableofcontents
\EndOfFilepart
\Filepart[file:./Lecture1.pdf]{Lecture1}%
\chapter{Chapter One}
\chaptertoc{}
\section{section one of chapter one}
\Blindtext
\section{section two of chapter one}
\label{A label}%
\Blindtext
\EndOfFilepart
\Filepart{Lecture2}%
\chapter{Chapter Two}
\chaptertoc{}
\section{section one of chapter two}
\Blindtext
\section{section two of chapter two}
\Blindtext
\section{section three of chapter two}
\Blindtext
\EndOfFilepart
\Filepart{Lecture3}%
\chapter{Chapter Three}
\chaptertoc{}
\section{section one of chapter three}
\Blindtext
\ref{A label}%
%\expandafter\show\csname r@A label\endcsname
\section{section two of chapter three}
\Blindtext
\section{section three of chapter three}
\Blindtext
\EndOfFilepart
\Filepart{Lecture4}%
\chapter{Chapter Four}
\chaptertoc{}
\section{section one of chapter four}
\Blindtext
\section{section two of chapter four}
\Blindtext
\section{section three of chapter four}
\Blindtext
\EndOfFilepart
\Filepart{Lecture2}%
\chapter{Chapter Five - should always go together with Chapter Two}
\chaptertoc{}
Chapter Five should always go together with Chapter Two,
thus\verb|\Filepart|-commands get the same name.
\section{section one of chapter five}
\Blindtext
\section{section two of chapter five}
\Blindtext
\section{section three of chapter five}
\Blindtext
\EndOfFilepart
\end{document}