當我寫講座時,我將它們寫在一份文件中,例如此 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 文檔,並根據講座的進度(通常是逐章)與我的學生分享內容。因此,我創建了一個新文檔,其中包含與此 mwe 類似的內容:
% 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-engines 目標自動固定到第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}