단계 2 · 텍스트와 문서 구조 15 / 48 ≈ 16 분 분량

LaTeX 제목 단계와 목차: secnumdepth·tocdepth

LaTeX의 제목 명령은 카운터와 단계, 그리고 조판 매크로인 동시에이고, 목차는 한 패스에 쓰고 다음 패스에 읽어들인 파일입니다. 이 강의는 secnumdepth와 tocdepth가 대조하는 단계 번호, 별표가 만드는 정확한 차이, 번호 없는 제목을 목차에 올리는 방법, 그리고 번호나 참조를 망가뜨리지 않고 titlesec이나 KOMA-script로 제목을 다시 입히는 방법을 다룹니다.

    여섯 단계, 세 클래스, 그리고 각자가 밟는 카운터

    표준 클래스의 어휘 전체는 \part, 그다음 \chapter(report와 book에만 있음), \section, \subsection, \subsubsection, \paragraph, \subparagraph입니다. 각각은 동시에 세 가지입니다: 밟는 카운터 하나, 그 카운터로 만들어지는 인쇄 번호 형식 하나, 그리고 조판 조각 하나. 셋 중 어디를 만지고 있는지 아는 것이 제목을 예측 가능하게 만들고, 어느 클래스에 있는지는 화장의 문제가 아니라 구조의 결정입니다 — 문서에 장이 있다면 report나 book이고, article에는 \chapter이 그냥 존재하지 않습니다.

    클래스들은 그 명령들을 커널 매크로 \@startsection으로 정의하고, 그 인자는 이름, 단계 번호, 들여쓰기, 앞 여백, 뒤 여백, 스타일입니다. 저 한 줄이 사람들이 씨름하는 대부분의 것을 설명합니다. 음수 앞 여백이 바로 제목 다음 단락을 들여쓰지 말라고 LaTeX에 알리는 것이고, 음수 뒤 여백은 제목이 자기 줄을 차지하는 대신 본문에 달라붙게 만드는데, \paragraph가 정확히 그 동작이고 위에 있는 것들과 다르게 보이는 이유입니다.

    제목 옆에 인쇄되는 번호는 \thesection, \thesubsection 등에서 나옵니다. article에서는 단순하고(\arabic{section}), book에서는 합성입니다 — \thesection이 \thechapter.\arabic{section}으로 정의되어 있고 “3.2”가 여기서 나옵니다. 그 정의 하나를 바꾸면 목록 항목에서 \theenumi가 하는 것과 똑같은 방식으로 \ref 출력까지 바뀌게 됩니다.

    의미를 실을 수 있는 가장 얕은 단계를 쓰고 거기서 멈추십시오. 네째 번호 단계가 필요한 문서는 대개 구조를 다시 짜야 하고, 학술지나 학위논문의 사내 규범은 셋을 넘는 일이 드뭅니다. 그 아래에서는 \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은 기본이 더 얕아서 학위논문 템플릿이 first로 올리는 것이 이 값들입니다. 프리앰블에서 평범한 카운터로 설정합니다: \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는 실행을 멈추고 하나를 출력합니다. 둘 다 2초면 되는 확인이고, 둘 다 한 번도 써 보지 않은 클래스에 대한 질문을 마무리하는 방법입니다.

    \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가 늦게 해결되는 것과 같은 2패스 논리이고, 로그가 그 사실을 말해 줍니다: 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 줄을 건너뛰고, 달리기 제목 표시까지 건너뜁니다. 그래서 별표 제목은 참조할 수 없고(그 안의 label에 대한 \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}, 또는 oneside 문서라면 \markright{...}. 그 한 줄이, 앞부분을 전문적으로 보이게 하는 것과 여전히 이전 장을 달고 있는 달리기 제목 사이의 차이입니다.

    앞부분, 본문, 그리고 서로 합의하는 페이지 번호

    report와 book은 문서를 \frontmatter, \mainmatter, \backmatter로 나눕니다. 세 선언은 경계에서 할 일 묶음을 함께 처리합니다: 앞부분은 로마 숫자로 번호 붙고 장은 번호 없이 목차에만 오르고, 본문은 아라비아 1에서 다시 시작해 장 번호를 켜고, 뒷부분은 아라비아 번호를 유지하면서 다시 장에는 번호를 붙이지 않습니다. article에는 이 중 아무것도 없고, 초록과 목차와 서론과 부록이 있는 리포트가 대개 report인 정직한 이유가 그것입니다.

    조각들의 순서가 하중을 받습니다. \pagenumbering{...}(직접도 사용 가능: roman, arabic, alph, Alph, fnsymbol)과 \setcounter{page}{1}은 \tableofcontents 이전에 실행되어야 합니다. 목차는 각 제목을 본 시점의 페이지 번호를 그대로 싣기 때문입니다. 목록 뒤에 번호 방식을 바꾸면 그 안의 모든 항목이 한 단계 어긋나고, LaTeX은 경고하지 않습니다 — 다음 패스만이 파일과 문서를 합의시킵니다.

    양면 인쇄는 레이아웃의 나머지 절반을 더합니다. openright는 장을 right-side 페이지에서 시작하게 하고, \cleardoublepage는 손으로 거기 도달하는 도구이며 verso는 의도적으로 빈 페이지로 채웁니다. \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}). 그리고 변경이 번호 형식 — 마지막 마침표 removal, 접두어 추가 — 이라면 \ref가 같은 매크로를 읽는다는 사실을 알면서 \thesection을 재정의하세요. 인쇄되는 제목에만 영향을 주려면 \@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}
    \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}

    자주 하는 실수

    • {\Large\bfseries Method\par}로 제목을 흉내 내는 것: 카운터도, 목차 항목도, 달리기 제목도 없고, 여백은 앞 단락의 것이 됩니다. \section{...}를 쓰고 클래스 후크를 다시 입히세요.
    • \section*{}를 쓴 뒤 사라진 목차 항목을 찾는 것. 별표는 번호와 목차 줄과 헤더 표시를 한 번에 지웁니다. \addcontentsline{toc}{section}{...}를 더하거나 KOMA의 \addsec을 쓰세요.
    • 수식을 \texorpdfstring이나 짧은 선택 제목 없이 제목에 넣어, 목차가 깨지고 PDF 북마크가 망가지는 것.
    • 섹션 제목에 \verb를 넣는 것. 제목 인자는 moving argument라서 \verb를 거기서 robust하게 만들 수 없습니다. 탈출 문자를 쓴 \texttt{}나 cprotect 패키지를 쓰세요.
    • \tableofcontents 뒤에 \setcounter{page}{1}이나 \pagenumbering{arabic}을 두는 것. 목록의 모든 번호가 한 방식 뒤처집니다.
    • 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까지 장 없이 제공하고, report와 book은 section 위에 chapter를 더하며, book은 추가로 모든 것을 부와 앞부분·본문·뒷부분으로 감쌉니다. 클래스는 필요한 구조로 고르세요. 단계 자체는 어디서나 같은 방식으로 동작합니다.

    실시간 놀이터: LaTeX를 편집하고 바로 렌더링

    입력하면 미리보기가 즉시 갱신됩니다. 일부러 깨뜨려서 오류가 어떻게 보이는지도 확인해 보세요.

    KaTeX 오류
    예제 사용해 보기

    이어서 읽기

    같은 주제로 들어가는 세 갈래: 표기 자체, 그것을 가르치는 강의, 그리고 실제로 쓰는 글.