\begin{document} コマンドは具体的に何をトリガーするのでしょうか?

\begin{document} コマンドは具体的に何をトリガーするのでしょうか?

まず、これら 2 つのファイルについて考えてみましょう。


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

これら 2 つのファイルは、前後にいくつかのコマンドが使用されていることを除いて内容は同じです\begin{document}。ただし、出力は完全に同じです。

ここで、コマンドを追加したい場合は\baselineskip、 の後にのみ使用すると機能します\begin{document}

また、プリアンブル内でのみ許可されるコマンドもあります。 は、\usepackageその中で最も一般的なものです。

\begin{document}確かに、これが環境の始まりであり、おそらく最も高い、あるいは最も重要な環境であることは理解しています。

( \newenvironment{document}latex.ltx のようなものを検索してみましたが、成功しませんでした。適切な場所を見ていないのかもしれません。)

しかし、このコマンドは具体的に何をするのでしょうか?

以下の2つの質問にお答えください。

  1. \begin{document} コマンドは具体的に何をトリガーしますか? これによって何がオンになりますか?

  2. 一部のコマンドがプリアンブル内でのみ許可されるのはなぜですか?

間違いなく、他の関連する問題も答えとともに出てくるでしょう。

答え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は、特定のコンテンツを書き始める前にページのレイアウトと寸法を設定する必要があるため、1つです。ただし、より一般的なルールとして、構造とコンテンツを分離する方が適切であり、パッケージは構造インターフェイスを提供するため、プリアンブルに適しています。環境内で、かなり簡単なパッケージをロードするには、mypackage次のようにします。document

\makeatletter
\input{mypackage.sty}% Load mypackage.sty
\makeatother

\makeatletter...ペアはマクロ内の問題\makeatotherを回避します@。ただし、一部のパッケージは、プリアンブル内でのみ使用可能なコマンドを使用して記述されています。これを回避するのは面倒なプロセスであり、必要ありません。

関連情報