段階 2 · テキストと文書構造 15 / 48 ≈ 18 分で読める

LaTeXの見出し階層と目次:secnumdepthとtocdepthの読み方

LaTeX の見出しコマンドは、カウンタを進める作業・そのカウンタから作られる番号の印字書式・段落の書式という三つの仕事を同時に担っています。目次もまた、その場で組まれるのではなく、ある回のビルドが書き出したファイルを次の回が読み戻す仕組みです。この課では secnumdepth と tocdepth が数え上げるレベル番号、星印が実際に消し去る三つのもの、番号なし見出しを目次へ戻す書き方、そして前付から後付までページ番号を噛み合わせ、titlesec や KOMA-script で番号と相互参照を生かしたまま見出しを組み直す通路まで扱います。

    六つの階層、三つのクラス、そして各コマンドが進めるカウンタ

    標準クラスが見出し用に用意している語彙は \part、続いて \chapter(report と book の専売です)、\section、\subsection、\subsubsection、\paragraph、そして \subparagraph までです。コマンド一つが同時に三つの仕事を担っており、進めるべきカウンタを持ち、そのカウンタから作られる印字番号の書式を持ち、段落の書式の一部も含みます。三つのどれを触ったのかを区別できることが、見出しを予測どおりに動かす条件です。クラス選びも見た目ではなく構造の話で、章が存在する文書なら report か book を選ぶしかありません。article には \chapter がそもそも存在しないからです。

    これらのコマンドはクラス内部でカーネルマクロ \@startsection を通して定義されています。引数の順は名前、レベル番号、字下げ、直前の空き、直後の空き、スタイル。この一行を見れば、多くの人が見出しと格闘する理由の大半が分かります。直前の空きが負の値なら、それは見出しの直後の段落をインデントするなという命令です。直後の空きが負なら見出しはそれ自体の行を取らず本文の中へ流れ込みます——\paragraph がまさにこの挙動であり、上位の階層と違って見える理由もそこにあります。

    見出しのとなりに印字される番号を生み出しているのは \thesection や \thesubsection です。article の中身は単純な \arabic{section} ですが、book では合成になり、\thesection は \thechapter.\arabic{section} として定義されています。「3.2」の由来はこの点です。この定義を一つ書き換えると \ref の出力も一緒に変わります。列表で \theenumi が果たす役割と同じ関係です。

    意味が成立する最浅の階層で止めること。第四階層までの番号が必要な文書は、たいてい構成そのものを組み直すべきという信号であり、雑誌や学位論文の規定が三階層超を認めることはめったにありません。その一段下にある \paragraph と \subparagraph は本文へ流れ込む見出しで、長い文章の塊を断ち切るためのものです。既定では番号も付かず、既定では目次にも載りません。

    \begin{array}{c|l|l|l}\text{level} & \text{command} & \text{counter} & \text{numbered by default} \\\hline 0 & \texttt{part / chapter} & \texttt{part, chapter} & \text{yes} \\ 1 & \texttt{section} & \texttt{section} & \text{yes} \\ 2 & \texttt{subsection} & \texttt{subsection} & \text{yes} \\ 3 & \texttt{subsubsection} & \texttt{subsubsection} & \text{article: yes} \\ 4 & \texttt{paragraph} & \texttt{paragraph} & \text{no} \\ 5 & \texttt{subparagraph} & \texttt{subparagraph} & \text{no}\end{array}
    \begin{array}{c|l|l|l}\text{level} & \text{command} & \text{counter} & \text{numbered by default} \\\hline 0 & \texttt{part / chapter} & \texttt{part, chapter} & \text{yes} \\ 1 & \texttt{section} & \texttt{section} & \text{yes} \\ 2 & \texttt{subsection} & \texttt{subsection} & \text{yes} \\ 3 & \texttt{subsubsection} & \texttt{subsubsection} & \text{article: yes} \\ 4 & \texttt{paragraph} & \texttt{paragraph} & \text{no} \\ 5 & \texttt{subparagraph} & \texttt{subparagraph} & \text{no}\end{array}
    The sectioning ladder with the level number each command carries, the number it prints in the article class, and where secnumdepth and tocdepth cut it off SIX LEVELS, THE COUNTER EACH ONE STEPS, AND THE TWO DIALS THAT CUT THE LADDER SHORT COMMAND LVL COUNTER PRINTED NUMBER NUM TOC \part \chapter (report · book only) \section \subsection \subsubsection \paragraph \subparagraph 0 0 1 2 3 4 5 part chapter section subsection subsubsection paragraph subparagraph Part I 2 2.1 2.1.3 (run-in) (run-in) — not in article yes yes no such command yes yes yes yes yes yes no no no no the red rule is where the article class stops: secnumdepth and tocdepth both ship at 3 THE TWO DIALS ARE INDEPENDENT article ships both at 3; report and book at 2. \setcounter{secnumdepth}{4} numbers \paragraph too \setcounter{tocdepth}{1} the list stops at \section \setcounter{secnumdepth}{-1} nothing numbered at all Ask your class, do not trust memory: \showthe\c@secnumdepth HOW "2.1.3" IS ASSEMBLED \thesection = \arabic{section} article — a plain number, 2 \thesection = \thechapter.\arabic{section} book — and that is where 3.2 comes from Redefine one of these and \ref changes with it: the counter and its printed shape are one macro. BELOW THE CUT, AND THE STAR \paragraph and \subparagraph are run-in: the space after them in \@startsection is negative, so they take no line to themselves. That is the difference. \section*{X} is a third answer, not a hidden number: no counter step, no .toc line, no running-head mark, and \ref inside it lands on the previous heading.
    六つの階層とレベル番号、そして標準 article クラスの既定が番号付与と目次掲載をどこで打ち切っているかを示したもの。

    secnumdepth と tocdepth:ダイヤルは二つある

    見出しコマンドには必ずレベル番号が付いています。\part が 0、\section が 1、\subsection が 2、\subsubsection が 3、\paragraph が 4、\subparagraph が 5。この数字と比較されるカウンタが二つあり、secnumdepth は番号の深さを、tocdepth は目次の深さを決めます。互いに独立なので、番号は付くのに目次に載らない階層、番号が無いのに目次へ出てくる階層がどちらも生じ、どちらも正当です。\subsection までしか番号を振らないのに \subsubsection の項目は目次へ載せる報告書、\section 以下に番号を打たず章だけが目次に並ぶ論文がそれに当たります。

    標準の article クラスは二つとも 3 で出荷されるため、\subsubsection は番号も目次も付きますが \paragraph はどちらも欠けます。report と book の既定はさらに浅く、論文テンプレートが真っ先に触るのはこの二つです。導言部で通常のカウンタとして \setcounter{secnumdepth}{4}、\setcounter{tocdepth}{2} と書けばいい。secnumdepth を -1 にすると一切番号が付きません。\section の書式と目次エントリは生かしたまま見出しを全部非番号にしたいとき、綺麗なやり方がこれです。

    途中で値を動かすこともできますが、その変更は \setcounter より \addtocontents で行う価値があります。仕組みはこうです。目次は次回に .toc ファイルを読み戻して組まれるため、本文の途中に置いた \setcounter{tocdepth}{...} はエントリの書き込みにしか効かず、読み出し側はそのままです。だから文書が落ち着くまでう一度余計な実行が要ることがある。\addtocontents{toc}{\protect\setcounter{tocdepth}{1}} とすれば変更そのものがファイルの中へ入るので、目次を組み直す側のパスに確かに効きます。

    既定値を誰かの記憶——このページの記憶も含めて——ではなく実際に確かめる方法もあります。\makeatletter \typeout{tocdepth=\the\c@tocdepth, secnumdepth=\the\c@secnumdepth} \makeatother を導言部に置けば両方の値がログへ書かれ、\showthe\c@tocdepth は実行を一度止めてそのうちの一つを印字します。どちらも数秒の作業で、使ったことのないクラスへの疑問をその場で片付けてくれる定番の手段です。

    \text{book}\colon\ \texttt{\textbackslash thesection} \;=\; \texttt{\textbackslash thechapter.\textbackslash arabic\{section\}} \qquad \text{article}\colon\ \texttt{\textbackslash thesection}=\texttt{\textbackslash arabic\{section\}}
    \text{book}\colon\ \texttt{\textbackslash thesection} \;=\; \texttt{\textbackslash thechapter.\textbackslash arabic\{section\}} \qquad \text{article}\colon\ \texttt{\textbackslash thesection}=\texttt{\textbackslash arabic\{section\}}
    \documentclass[11pt]{article}
    \usepackage[T1]{fontenc}
    \usepackage{lmodern}
    
    % How deep to number (levels: part 0, section 1, subsection 2,
    % subsubsection 3, paragraph 4, subparagraph 5) and how deep to list in the
    % table of contents. Raise both if you want paragraph headings numbered.
    \setcounter{secnumdepth}{4}
    \setcounter{tocdepth}{3}
    
    \usepackage{hyperref}          % so that \texorpdfstring below is defined
    
    \begin{document}
    
    \title{Structure and the table of contents}
    \author{A. Student}
    \date{}
    \maketitle
    
    \begin{abstract}
    One sentence that says what the report measured. The abstract is not a
    sectioning level, which is why it never appears in the contents list unless
    you add it by hand.
    \end{abstract}
    
    \tableofcontents
    
    \section{Method}
    Body text. Numbering comes from the counter, and the heading owns its own
    before-and-after space -- never add vertical space by hand here.
    
    \subsection{Sampling}
    \subsubsection{Site selection}
    Numbered at the third level.
    \paragraph{Equipment list}
    Level four is numbered only because \texttt{secnumdepth} was raised above its
    default; note that this style of heading runs into the text instead of taking
    a line to itself, which is what a level-four heading is for.
    
    \section*{Acknowledgements}
    % A starred heading is unnumbered and invisible to the contents list, so it
    % has to be announced explicitly:
    \addcontentsline{toc}{section}{Acknowledgements}
    
    \section{Results with a math title}
    % \texorpdfstring keeps the .toc and the PDF bookmarks readable.
    \section{\texorpdfstring{$E = mc^{2}$}{E = mc2} and its consequences}
    Nothing in this file needs a fourth pass; \texttt{latexmk} runs as many as
    the labels and the contents list ask for.
    
    \end{document}

    目次はどう作られ、なぜ二回目の実行が要るか

    LaTeX の中に、目次をその場で組み立てる仕組みは存在しません。見出しが処理されるとクラスは .toc ファイルへ一行を書き込んでおり、中身は事実上 \contentsline {section}{\numberline {2.1}Method}{3}{page.7} という形です。\tableofcontents はそのファイルを再生する側にすぎません。拡張子を変えれば同じ仕組みが図目次と表目次も生みます。\caption から .lof と .lot が書かれ、hyperref を読めば .out ファイルから PDF のブックマークツリーが組み上がります。

    そのファイルはまさに今起きている実行の副産物なので、一回目のコンパイルはリーダーがまだ見ていないエントリを書き込みます。つまり一回目の実行で 2 ページ目に出るのは前回の実行が残していったもので、最初に何も無ければ空のファイルです。\ref が後回しで解決されるのも同じ理屈で、ログも正直に LaTeX Warning: Label(s) may have changed. Rerun. と報告します。パス回数を数えてくれるのが latexmk で、目次を持つ文書を手動で組むなら最低二度です。

    各階層の描画は \l@<level> という名前のマクロが担当し、カーネルは三つの数字を渡した \@dottedtocline の呼び出しとして用意しています。数字はレベル、左余白からの字下げ、番号のために確保する幅の順です。点線が乱れて見える目次の説明はこれで全部、番号枠の幅を変えればタイトルは再び揃います。手で組み替えるとはこれらのマクロを再定義するということで、代行してくれるのが tocloft(階層ごとにキー形式で長さを指定)、titletoc(階層単位の罫線や点線なしまで行レイアウト全体を支配)、etoc(部や章ごとの部分目次と細かい深さ制御)です。

    一覧の上に出るタイトル名自体もクラスのマクロ、すなわち \contentsname です。普通の文書なら \renewcommand\contentsname{Outline} と書き換えますが、babel を読むとこの手は効かなくなります。名前が言語モジュールの支配下に入るため、正しい通路は \addto\captionsenglish{\renewcommand{\contentsname}{Outline}} です。\listfigurename と \tablename は残り二つの一覧に対応する同種のマクロで、KOMA-script クラスでは生の名前ではなく \addtokomafont{captionhead} 形式の要素へ三つとも包み直しています。

    \begin{array}{l}\texttt{\textbackslash addcontentsline\{toc\}\{subsection\}\{\{Title\}\}\{\{2\}\}} \\ \texttt{\textbackslash addtocontents\{toc\}\{\textbackslash protect\textbackslash setcounter\{tocdepth\}\{1\}\}}\end{array}
    \begin{array}{l}\texttt{\textbackslash addcontentsline\{toc\}\{subsection\}\{\{Title\}\}\{\{2\}\}} \\ \texttt{\textbackslash addtocontents\{toc\}\{\textbackslash protect\textbackslash setcounter\{tocdepth\}\{1\}\}}\end{array}
    How the table of contents is built: each heading writes a contentsline into the .toc file and tableofcontents replays that file on the next pass, with the page numbers it recorded NOTHING IN THE LIST IS COMPOSED LIVE — IT IS A FILE WRITTEN BY ONE RUN AND READ BY THE NEXT PASS 1 · WRITE \subsection{Method} steps the counters and appends one line report.toc THE .toc FILE a by-product of the run that is happening now, so nobody has read it yet \contentsline{subsection} PASS 2 · READ \tableofcontents replays the file of the previous run, one line \l@subsection{...} PASS 3 · SETTLED the numbers no longer move the breaks, so the list matches its own pages latexmk -pdf → 2-3 runs the page numbers the list prints can themselves shift a page break — that is the whole reason a second run exists on the very first compile the file is empty and the list is blank; the log says so: LaTeX Warning: Label(s) may have changed. Rerun. ONE LINE, FOUR ARGUMENTS \contentsline {subsection}{\numberline {2.1}Method}{3}{subsection.2.1} 1 the level name, which picks the \l@… macro that draws it 2 the number box plus the title 3 the page number as it stood then 4 the bookmark anchor AND THE ENTRY IT DRAWS 2.1 Method 3 number box · title · leaders · page indent and number-box width come from \@dottedtocline{2}{3.8em}{2em} SAME MECHANISM, OTHER FILES .lof \caption in a figure, read by \listoffigures .lot \caption in a table, read by \listoftables .out written by hyperref: the PDF bookmark tree WHEN THE LIST IS WRONG, NAME THE MECHANISM a starred heading wrote nothing: add the line by hand below tocdepth, or one run short — the file lags the text ragged titles: that level's number box is too narrow
    見出しが .toc ファイルへ一行を書き込み、次のパスがその中身を再生して目次になる、という向き合い方。

    星印が実際に消し去っているもの

    \section*{X} は「番号を隠しただけの節」ではありません。星付きの形はカウンタを進める工程も、.toc へ行く一行も、ヘッダーをつくるマークも、まとめて省略します。結果として星付き見出しは参照できず、その内側のラベルへ \ref を張ると最後に番号を振られたものが解決されてしまう。book クラスでは、星付き見出しが始めるページのヘッダーに前章の名残りが残り続けます。

    よくある依頼、つまり謝辞・抄録・参考文献・番号を付けたくない序論では、見出しの書式は欲しいが目次エントリは番号なしにしたいのです。明示の二つ組が答えで、\section*{Acknowledgements} の直後に \addcontentsline{toc}{section}{Acknowledgements} を書きます。第二引数は階層名、\numberline を渡さないのですから項目は番号なしで現れます。KOMA-script クラスなら \addsec{...} と \addchap{...} として最初から備わっており、クラスが提供しているなら短く間違いも少ないこちらを採ります。

    兄弟コマンドの \addtocontents{toc}{...} は項目一つではなく生の材料をファイルへ書き込みます。章と章の間に罫線や縦の空き、スモールキャップスの区切りラベルを入れるのがこの方法で、上で触れたとおり文書の途中で tocdepth を変えて書き手と読み手を一致させるのもここです。この方法で書いたものは目次を読み直している最中に展開されるため、決まりの形は \protect を頭に置いた \addtocontents{toc}{\protect\vspace{6pt}} です。

    book クラスの中の星付き見出しは、ヘッダーを自分で直すのが通例です。見出しの直後に \markboth{Acknowledgements}{Acknowledgements}、片面印刷の文書なら \markright{...} を置く。この一行があるかどうかで、前章のままのヘッダーを抱えた前付と、締まった前付が分かれます。

    前付・本文・後付、そして噛み合うページ番号

    report と book は文書を \frontmatter、\mainmatter、\backmatter の三つに区切ります。三つの宣言は境界のたびに一束の仕事を一括でこなし、前付はロー数字で章は番号なし・掲載あり、本文はアラビア数字の 1 から数え直して章の番号付けをオン、後付はアラビア数字を保ちつつ再び章に番号を打ちません。article にこの仕組みが何も無いのが、抄録・目次・序論・付録を擁する報告書は結局 report になる、素直な理由です。

    並び順は構造そのものを支える重みがあります。\pagenumbering{...}(直接使える指定は roman、arabic、alph、Alph、fnsymbol)と \setcounter{page}{1} は \tableofcontents の前に実行しなければなりません。目次は各見出しを見つけた時点のページ番号をそのまま運ぶからです。一覧の後で書式を変えると載っている項目の数字は全部古いままになり、しかも LaTeX は警告すら出さない。次のパスになって初めてファイルと文書が一致します。

    両面印刷はレイアウトの残り半分を担っています。openright は章を右ページから始めさせ、手動でそこまで運ぶ道具が \cleardoublepage で、左側には意図的に空のページを据えます。\newpage と \clearpage はより弱い形、ためておいたフロートまで押し出すのは \clearpage のほうです。章末に \clearpage を置いた途端に図のページが一枚増えるのは、そのためです。

    ヘッダーとフッターはクラスが握るページスタイルの決定事です。\pagestyle{headings} は \markboth と \markright が供給するヘッダーをオンにし、\thispagestyle{empty} は現在のページからそれらを取り除きます。表紙の定番であり、titlepage オプションが代行してくれる処理です。\pagestyle{plain} は標準クラスの既定、下中央にページ番号でヘッダーは無し。独自のヘッダーは fancyhdr か KOMA-script 自身の scrlayer-scrpage で作りますが、あの有名な警告 headheight is too small は抑え込むバグではなく設定すべき長さの話です。

    \documentclass[12pt,a4paper,twoside,openright]{book}
    \usepackage[T1]{fontenc}
    \usepackage{lmodern}
    \usepackage{graphicx}
    \usepackage{hyperref}
    
    \hypersetup{bookmarksnumbered=true}
    \setcounter{tocdepth}{1}       % contents lists chapters and sections only
    
    \begin{document}
    
    \frontmatter
    \title{A Thesis Skeleton}
    \author{A. Student}
    \date{\today}
    \maketitle
    \chapter*{Abstract}
    Roman page numbers belong to the front matter, so the page numbering style
    has to be switched \emph{before} the contents list is written.
    \tableofcontents
    \listoffigures
    \listoftables
    
    \cleardoublepage          % start the main matter on a right-hand page
    \mainmatter
    
    \part{Theory}
    \chapter{Definitions}
    \section{Scope}
    In the book class a section number carries its chapter: this heading is
    numbered \thesection, built from \verb|\thechapter.\arabic{section}|.
    \subsection{Terms}
    Below \texttt{tocdepth} = 1, so this subsection is numbered but is not
    listed in the contents.
    \begin{figure}[ht]
      \caption{A caption becomes a line in the list of figures.}
    \end{figure}
    
    \part{Practice}
    \chapter{Fieldwork}
    
    \appendix
    \chapter{Questionnaire}
    \section{Open items}
    Appendix chapters become letters, so this is section \thesection.
    
    \backmatter
    \chapter*{Bibliography}
    \addcontentsline{toc}{chapter}{Bibliography}
    
    \end{document}

    番号を壊さずに見出しの書式を組み直す

    間違っているやり方は有名で、しかもいつも誘われます。\section{\Large\sffamily Method} と書く方法です。装飾をタイトル本文の内側へ入れ込むので、目次と PDF のブックマークがサイズ指定もファミリ指定も引き継ぎ、ヘッダーまで引き継ぐことがある。見出しはクラス側で組み直せる対象ではなくなり、こうして得られる結果はすべて、書式パッケージなら .toc へ漏れずに手に入ります。

    一般的な解が titlesec です。\titleformat{\section}[block]{\normalfont\LARGE\bfseries\sffamily}{\thesection}{1em}{} は順に形、番号の書式、番号とタイトルの間隔を指定し、\titlespacing*{\section}{0pt}{18pt plus 4pt minus 2pt}{6pt} がクラスの直前・直後のスキップを差し替えます。伸び縮みの指定が含まれているのは、見出しの直下でページが不自然に割れるのを防ぐため。\titlespacing の星付き形は、ページが割れた位置にある見出しの直後の空きを落としてくれますが、あれがまさに欲しい挙動であり、まさに多くの人が存在を知らない機能です。

    KOMA-script のクラス(scrartcl、scrreprt、scrbook)では、装飾はオプションと要素フォントで指定します。\addtokomafont{section}{\sffamily}、\RedeclareSectionCommand[beforeskip=18pt,afterskip=6pt,indent=0pt,runin=false]{paragraph}、それに headings=small|normal|big、numbers=noenddot、chapterprefix といったクラスオプションです。既にあるクラスを組み直すのではなく長い文書向きのクラスを選ぶ段階なら、継ぎはぎが一番少なくなる通路がこちらです。

    変更がファミリか太さだけなら sectsty が二行で済ませます(\allsectionsfont{\sffamily} と \sectionfont{\Large})。番号の書式そのもの、末尾の点を外すとか接頭辞を足すとかを変えるなら、\thesection を再定義すればいい。ただし \ref が同じマクロを読むことは知っておいてください。印字される見出しだけに効かせたいなら \@seccntformat を上書きするか、やはり titlesec に番号枠を任せます。

    \documentclass[12pt]{article}
    \usepackage[T1]{fontenc}
    \usepackage{lmodern}
    
    % ---- Route 1: titlesec, works with any class -----------------------
    \usepackage{titlesec}
    \titleformat{\section}[block]{\normalfont\LARGE\bfseries\sffamily}
                {\thesection}{1em}{}
    \titleformat{\subsection}[block]{\normalfont\large\bfseries}
                {\thesubsection}{1em}{}
    \titlespacing*{\section}{0pt}{18pt plus 4pt minus 2pt}{6pt plus 2pt minus 2pt}
    \titlespacing*{\subsection}{0pt}{12pt plus 3pt minus 2pt}{4pt plus 1pt minus 1pt}
    
    % ---- Route 2: KOMA-script, when the class is scrartcl/scrbook -------
    % \addtokomafont{section}{\sffamily\MakeUppercase}
    % \RedeclareSectionCommand[beforeskip=18pt,afterskip=6pt,indent=0pt]{subsection}
    % \KOMAoptions{headings=normal,numbers=noenddot}
    
    % ---- Route 3: sectsty, the one-line version ------------------------
    % \usepackage{sectsty}
    % \allsectionsfont{\sffamily}
    % \sectionfont{\Large}
    
    \begin{document}
    
    \tableofcontents
    
    \section{Restyled heading}
    The class still owns the counter and the contents entry; \texttt{titlesec}
    only changed how the heading is drawn and how much space stands around it.
    That division of labour is the reason to use a heading-styling package
    instead of \texttt{\textbackslash Large\textbackslash bfseries} typed by hand.
    
    \subsection{A subsection}
    Notice that the number \ref{sec:two} is still produced by \thesubsection,
    because redefining \verb|\thesection| would have changed the cross-reference
    as well as the heading -- which is sometimes what you want, and usually not.
    \label{sec:two}
    
    \end{document}

    目次がおかしいときの四つの症状と原因

    項目が消える。起こりやすい順に三つ、見出しが星付き(そもそも一行も書かれない)、レベルが tocdepth より深い、項目は書けているのに番号枠が狭すぎてタイトルが行からはみ出している。前者の二つはカウンタ、三つ目は \l@<level> の定義の問題です。カウンタ側の見立ては \the\c@tocdepth を本文へ一度印字して確かめ、星側の見立てはソースを *{ で検索すれば付きます。

    ページ番号が違う、または一覧が空。ほとんどはパス構成が原因です。一覧は前回の実行が書いたファイルから読まれるので、見出しが動いた文書はもう一度の実行が要り、しかも \cleardoublepage・フロート・\pagenumbering の変更が絡む文書は収束するまで必ず一回遅れます。latexmk に任せて回数を数えるのをやめ、別の物をデバッグ中なら pdflatex を手動で二度回してください。

    項目が文字化けする、または目次だけでエラーが出る。タイトルに、ファイルへ書いて読み戻す工程を生き残れない物が入っています。数式、\verb、fragile なコマンドです。対策はブックマークとタイトル側が \texorpdfstring{$E=mc^{2}$}{E = mc2}、一覧側が省略可能な引数、つまり \section[Plain title]{Title with maths}、そして fragile な物には前置きの \protect。\chapter のタイトルに含まれた数式も同じ問題で、行き先が .toc 行とブックマークの二つあるだけです。

    番号は正しいのに見出しの見た目がおかしい。これは書式の問題で、クラス側のフックではなく見出しテキストまで装飾が流れ込んでいる証拠です。前の節を参照してください。四つの症状に共通する目安はこれ、番号付け・一覧への掲載・描画は LaTeX では別々の三つの仕組みであり、見出し系のバグのほぼすべては三つの一つを別の仕組みから触ろうとした時に生まれます。

    このレッスンの早見表

    各セクションのキーコードを一行ずつ。見出しで該当箇所へ移動、コードはそのままコピーできます。

    セクション キーコード
    \begin{array}{c|l|l|l}\text{level} & \text{command} & \text{counter} & \text{numbered by default} \\\hline 0 & \texttt{part / chapter} & \texttt{part, chapter} & \text{yes} \\ 1 & \texttt{section} & \texttt{section} & \text{yes} \\ 2 & \texttt{subsection} & \texttt{subsection} & \text{yes} \\ 3 & \texttt{subsubsection} & \texttt{subsubsection} & \text{article: yes} \\ 4 & \texttt{paragraph} & \texttt{paragraph} & \text{no} \\ 5 & \texttt{subparagraph} & \texttt{subparagraph} & \text{no}\end{array}
    \documentclass[11pt]{article}
    \begin{array}{l}\texttt{\textbackslash addcontentsline\{toc\}\{subsection\}\{\{Title\}\}\{\{2\}\}} \\ \texttt{\textbackslash addtocontents\{toc\}\{\textbackslash protect\textbackslash setcounter\{tocdepth\}\{1\}\}}\end{array}
    \documentclass[12pt,a4paper,twoside,openright]{book}
    \documentclass[12pt]{article}

    よくある間違い

    • \section{...} を使わず、{\Large\bfseries Method\par} のように打って見出しを偽造すること。カウンタも目次エントリもヘッダーも生まれず、空きは前の段落の物になってしまいます。コマンドは \section に戻し、書式はクラス側のフックで組み直してください。
    • \section*{} を打ったあと、目次に載らない原因を探すこと。星は番号・目次行・ヘッダーマークを三つ同時に消します。\addcontentsline{toc}{section}{...} を明示で足すか、KOMA-script クラスなら \addsec のほうが短いです。
    • 見出しに数式を置くとき \texorpdfstring も短い代替タイトルも用意せず、結果として目次が文字化けして PDF のブックマークも壊れること。
    • 見出しの引数に \verb を入れること。見出し引数は moving argument で、\verb はそこに robust 化できません。\texttt{} とエスケープ文字の組合せにするか、cprotect パッケージを使います。
    • \setcounter{page}{1} や \pagenumbering{arabic} を \tableofcontents の後で実行し、一覧中の数字を一つ古い書式に残したままにすること。
    • article クラスの文書で \chapter に頼ること。あのクラスにコマンドは存在せず、エラーの Undefined control sequence が語っているのはパッケージ不足ではなくクラスの話です。
    • 構成が乱れたからと secnumdepth を 5 へ上げること。四階層の番号は既に読者が追える範囲を越えており、深く振るべきではなく階層を平らにすべきサインです。

    この課に関する質問

    LaTeX で節が目次に載らないのはなぜ?
    星付き見出しになっている場合か、レベルが tocdepth より深い場合のどちらかです。星は番号だけでなく目次行も抑えてしまうので、\addcontentsline{toc}{section}{Title} で項目を自分で書き添え、ダイヤルは導言部の \setcounter{tocdepth}{3} で引き上げます。項目自体は載っているのにタイトルが欠けて見えるなら、そのレベルの番号枠 \l@section が狭いだけで、深さの問題ではありません。
    secnumdepth と tocdepth の違いは?
    どちらも各見出しコマンドのレベル番号と比較されるカウンタで、part が 0、section が 1、subsection が 2、subsubsection が 3、paragraph が 4、subparagraph が 5 です。secnumdepth は印字される番号の深さ、tocdepth は目次の深さを決めます。互いに独立なので、番号だけ付く階層も、番号なしで目次にだけ載る階層も普通に作れます。
    番号を付けない節を目次へ入れるには?
    見出しは星付きで書き、項目は自分で宣言します。\section*{Acknowledgements} の直後に \addcontentsline{toc}{section}{Acknowledgements} を置く写法です。KOMA-script クラスなら同じことが \addsec{Acknowledgements} の一発で済み、ヘッダーも正しく保たれます。book クラスで章レベルの項目が欲しいときは第二引数を chapter にしてください。
    コンパイル直後に目次が空で出てくるのは?
    一覧は前回の実行が書いた .toc ファイルから組み立てられるため、初回はあのファイルがまだ存在しないからです。もう一度コンパイルし、見出しやページ番号が動いたならもう一度。latexmk は出力がまだ変わっているかを見てパス回数を自分で決めるので、目次付き文書を回す唯一の良識ある方法があれです。
    見出しのフォントや大きさを変えるには?
    タイトルの中身ではなく書式パッケージで変えます。titlesec なら形が \titleformat で周囲の空きが \titlespacing、KOMA-script なら \addtokomafont{section}{...} と \RedeclareSectionCommand、sectsty はファミリと太さなら二行です。\section{...} の内に打った装飾は目次エントリと PDF ブックマークが引き継ぐので、スタイルではなくバグになります。
    part・chapter・section の違いは?
    同じ番号体系の 0、0、1 階層という違いですが、クラス実装も異なります。article は part から subparagraph まであり chapter は無い。report と book は section の上へ chapter を追加し、book はさらに全体を部と前付・本文・後付で包みます。クラスは必要とする構造の方で選ぶもので、レベル自体の振る舞いはどこでも同じです。

    ライブ playground:LaTeX を編集して即描画

    入力すると即座にプレビューが更新されます。わざと壊してエラーの見た目も確認しましょう。

    KaTeX エラー
    例を試す

    続けて読む

    同じ話題への三つの入口:記法そのもの、それを教える課、そして実際に使っている記事。