
讓我們先考慮這兩個文件。
\documentclass{article}
\parindent 0.0mm
\hyphenpenalty 500
\tolerance 700
\usepackage{lipsum}
\begin{document}
\lipsum[1-10]
\end{document}
\documentclass{article}
\usepackage{lipsum}
\begin{document}
\parindent 0.0mm
\hyphenpenalty 500
\tolerance 700
\lipsum[1-10]
\end{document}
這兩個文件的內容相同,只是前後使用了一些指令\begin{document}
。然而,它們產生完全相同的輸出。
現在,如果我們想要新增一個\baselineskip
指令,只要在\begin{document}
.
同樣,有些命令僅在序言中允許。\usepackage
是其中最常見的。
當然,我們理解\begin{document}
啟動一個環境,可能是最高或最主要的環境,你知道我的意思。
(嘗試在 Latex.ltx 中搜尋類似的內容\newenvironment{document}
。沒有成功。可能是我沒有找對地方。)
但這個命令到底是做什麼的呢?
請回答這兩個問題
\begin{document} 指令到底觸發了什麼?它會打開哪些東西?
為什麼有些指令只允許出現在序言中?
當然,其他相關問題也會隨之而來。
答案1
您應該在裡面搜尋\document
(和/或\enddocument
)latex.ltx
,因為環境env
由宏對\env
和組成\endenv
:
\def\document{\endgroup
\ifx\@unusedoptionlist\@empty\else
\@latex@warning@no@line{Unused global option(s):^^J%
\@spaces[\@unusedoptionlist]}%
\fi
\@colht\textheight
\@colroom\textheight \vsize\textheight
\columnwidth\textwidth
\@clubpenalty\clubpenalty
\if@twocolumn
\advance\columnwidth -\columnsep
\divide\columnwidth\tw@ \hsize\columnwidth \@firstcolumntrue
\fi
\hsize\columnwidth \linewidth\hsize
\begingroup\@floatplacement\@dblfloatplacement
\makeatletter\let\@writefile\@gobbletwo
\global \let \@multiplelabels \relax
\@input{\jobname.aux}%
\endgroup
\if@filesw
\immediate\openout\@mainaux\jobname.aux
\immediate\write\@mainaux{\relax}%
\fi
\process@table
\let\glb@currsize\@empty %% Force math initialization.
\normalsize
\everypar{}%
\ifx\normalsfcodes\@empty
\ifnum\sfcode`\.=\@m
\let\normalsfcodes\frenchspacing
\else
\let\normalsfcodes\nonfrenchspacing
\fi
\fi
\@noskipsecfalse
\let \@refundefined \relax
\let\AtBeginDocument\@firstofone
\@begindocumenthook
\ifdim\topskip<1sp\global\topskip 1sp\relax\fi
\global\@maxdepth\maxdepth
\global\let\@begindocumenthook\@undefined
\ifx\@listfiles\@undefined
\global\let\@filelist\relax
\global\let\@addtofilelist\@gobble
\fi
\gdef\do##1{\global\let ##1\@notprerr}%
\@preamblecmds
\global\let \@nodocument \relax
\global\let\do\noexpand
\ignorespaces}
在許多事情中,document
環境引發了以下事情:
- 報告未使用的
\documentclass
選項; - 設定頁面佈局;
- 讀入
.aux
文件; - 打開一個流以開始(覆蓋)寫入
.aux
; - 啟動文件的字體大小 (
\normalsize
); \AtBeginDocument
執行透過( )收集到的所有內容\@begindocumenthook
;- 停用僅前導碼指令(通過
\@preamblecmds
,它收集定義為僅可透過 前導碼使用的指令\@onlypreamble
)。
請注意,這是 LaTeX 中使用的預設定義。根據要求,某些文件可能會變更或附加到此定義。
從根本上講,套件旨在執行許多操作,包括幹預環境啟動期間完成的操作document
(如上所述)。清楚地,geometry
是其中之一,因為它需要先設定頁面佈局和尺寸,然後才能開始編寫某些內容。然而,作為更一般的規則,最好將結構與內容分開,並且由於包提供結構接口,因此它們更適合序言。您可以在環境中載入一個相當簡單的套件mypackage
(比如說)document
,使用
\makeatletter
\input{mypackage.sty}% Load mypackage.sty
\makeatother
\makeatletter
……這\makeatother
對避免@
了宏觀問題。但是,某些軟體包是使用本身只能在前導碼中使用的命令編寫的。規避這一過程將是一個乏味的過程,而且沒有必要。