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

LaTeXの特殊文字とアクセント:十個の予約文字の出し分け

TeX はファイルを文章として読みません。文字ごとに役割を割り当ててから組版へ渡すため、パーセントもドルも和標識も「そのまま打つ」と別のことになります。この課ではその十の予約文字と各々のエスケープ、本文と数式で挙動が変わる理由、アクセント命令とその下のエンコーディング、そして組版を拒む材料を印刷する三つの方法を扱います。

    TeX にとっての意味があなたより先にある十文字

    TeX はファイルを文章として読みません。入力処理が文字ごとに分類——エスケープ、グループ開始、グループ終了、数式切換、整列位置、行末、パラメータ、上付き、下付き、活性文字、コメント、文字、その他、無効——を行い、ページに字として届くのはその他と文字の二区分だけです。十文字は既定でその他ではなく、それが処置を要する理由の全部です。\ がエスケープ、{ と } がグループ、$ が数式への切換、& が整列位置、# がマクロのパラメータ印、^ と _ が上付きと下付き、~ は活性文字(LaTeX は非分割スペースを与えている)、% がコメントの開始です。

    エスケープの規則は一行分です。文字でないものがバックスラッシュの直後に来ると長さ 2 の制御語となり、「その文字をそのまま」という意味になります。つまり \%、\$、\&、\#、\_、\{、\} は \ に文字自身を足したもので、カーネルに名前が定義されているから動きます。近道が無い三文字は、自己参照や曖昧さが出る組です。バックスラッシュそのものが \textbackslash、チルダが \textasciitilde、キャレットが \textasciicircum、対称性からグレーヴアクセントが \textasciigrave となっています。

    特別だと誤解されるが特別ではない文字、<、>、|、"、?、/、* は素の LaTeX ではその他の分類で、そのまま組まれます。ただし話が二つ。< と > の字形は旧い OT1 フォントエンコーディングに存在せず、黙って何も印字されないことがあります——T1 にはあり、\textless や \textgreater は textcomp 由来でエンコーディングに依存しません。そして " はいくつかの babel の言語オプション(ドイツ語の短縮形、ポーランド語、カタルーニャ語)で活性文字にされるため、英語で通るファイルが他の言語では未定義の短縮形のエラーで落ちます。

    分類は自分で検査できます。推測をやめる最速の方法です。\showthe\catcode`\_ は TeX がその文字に与えている数値を印字し、\catcode`\_=12 はそれを普通の文字へ格下げします——まさに verbatim の仕組みが局所グループでやっていることで、\ も普通の文字に変えるので、ブロックの中にエスケープは残らなくなります。

    \begin{array}{c|l|l}\text{source} & \text{role in TeX} & \text{to print it} \\\hline \texttt{\textbackslash\%} & \text{comment} & \text{percent} \\ \texttt{\textbackslash\$} & \text{math shift} & \text{dollar} \\ \texttt{\textbackslash\&} & \text{alignment tab} & \text{ampersand} \\ \texttt{\textbackslash\#} & \text{parameter} & \text{hash} \\ \texttt{\textbackslash\_} & \text{subscript} & \text{underscore} \\ \texttt{\textasciicircum} & \text{superscript} & \text{caret} \\ \texttt{\textasciitilde} & \text{active} & \text{tilde} \\ \texttt{\textbackslash\{\ \textbackslash\}} & \text{grouping} & \text{braces}\end{array}
    \begin{array}{c|l|l}\text{source} & \text{role in TeX} & \text{to print it} \\\hline \texttt{\textbackslash\%} & \text{comment} & \text{percent} \\ \texttt{\textbackslash\$} & \text{math shift} & \text{dollar} \\ \texttt{\textbackslash\&} & \text{alignment tab} & \text{ampersand} \\ \texttt{\textbackslash\#} & \text{parameter} & \text{hash} \\ \texttt{\textbackslash\_} & \text{subscript} & \text{underscore} \\ \texttt{\textasciicircum} & \text{superscript} & \text{caret} \\ \texttt{\textasciitilde} & \text{active} & \text{tilde} \\ \texttt{\textbackslash\{\ \textbackslash\}} & \text{grouping} & \text{braces}\end{array}
    The ten reserved LaTeX characters, the role TeX gives each and the command that prints it CHAR CATEGORY TeX GIVES IT PRINT IT IN TEXT IN MATHS DAMAGE IF BARE % comment character \% unchanged: it comments rest of the line vanishes $ math shift \$ ends maths everything after it is maths & alignment tab \& only at a tab Misplaced alignment tab # parameter token \# unchanged: still a only legal in a macro body _ subscript (shift in) \_ starts a subscript dropped in text, silent ^ superscript (shift out) \textasciicircum starts a superscript dropped in text, silent ~ active character \textasciitilde nbsp too a space no break may use { begin group \{ \{ is a delimiter an unclosed brace, error later } end group \} \} is a delimiter extra close brace, error now \ escape character \textbackslash same the next token becomes a name THE TWO SILENT ONES ^ and _ do nothing in text mode — no error, no glyph. A file name typed bare simply loses them. NOT RESERVED, THOUGH PEOPLE THINK SO < > | " ? / * are ordinary characters in LaTeX — but < and > have no glyph in the OT1 font encoding. Every escape in the third column is a control symbol: backslash plus one non-letter. That rule cannot cover the backslash itself, and it cannot cover the two characters whose active or math meaning is the useful one — which is why textasciicircum, textasciitilde, textasciigrave and textbackslash exist as named commands instead.
    予約文字、TeX が各々に与える役割、そしてそれを印刷するコマンド。

    同じ文字が数式と本文で違う振る舞いをする理由

    分類コードはモードをまたいで変わりませんが、TeX が反応するものが変わります。^ と _ は数式中だけ解釈され、本文モードでは捨てられます。sum\_i.cfg のようにエスケープなしで打つと、エラーも出さず下線が消えているのはそのためです。整列の & は逆で、両モードで尊重されます。数式では aligned、array、matrix の中、本文では tabular と align の中であり、整列の外では TeX でもっとも素直なメッセージ、Misplaced alignment tab character &. が出ます。

    % は条件なしです。コメント文字は数式中でも表のセルでも引数の内でも、どこであれ行を終えます。十文字の中で未エスケープのパーセントが最も破壊的な理由はこれで、破壊が行の途中で見えないからです。50% of cases はその行の残りを削除し、前に開いた \{ が閉じられないまま TeX は次の行へ進むため、エラーメッセージは全く違う場所を指します。

    $ はモード切換そのものなので、迷ったドルは「間違って印字される」のではなく、その後すべてへの規則を変えます。古典的な症状は数行遅れて出る Math formula か Misplaced } inserted の訴えです。{ と } は両モードでグループを作り、どちらでも印字はされません。数式の中で印字したいなら \{ と \} で、これらは数式開き・数式閉じの区切りコードを持つので \left/\right で伸縮します。記号だけ欲しいなら \lbrace/\rbrace です。

    # はパラメータ印で、定義の中身でのみを期待されます。知っておくべき帰結が二つ。リテラルのシャープは \newcommand の中では ## と打つ必要があること、中身を読んだとき一段剥がされるからです。そして LaTeX の expl3 インターフェースは \csname 風に下線とコロンを詰めた名前を使うので、あれらの文書は全部 \ExplSyntaxOn で包まれています——フォントの話ではなく分類コードの変更です。

    \{\,x \in \mathbb{R} : x_{i}^{2} \le 1\,\} \quad R \sim S \quad a \mapsto b
    \{\,x \in \mathbb{R} : x_{i}^{2} \le 1\,\} \quad R \sim S \quad a \mapsto b
    この式では中括弧が印字されます。\{ と \} が区切り文字だからです。同じソースにあるグループ化の中括弧は見えません。
    \documentclass[10pt]{article}
    \usepackage[T1]{fontenc}
    \usepackage{lmodern}
    \usepackage{textcomp}
    \usepackage{amsmath}
    \usepackage{siunitx}
    \usepackage[utf8]{inputenc}   % harmless today, required on older installs
    
    \begin{document}
    
    \section*{Money, measures and percentages}
    
    The invoice reads \$1{,}250.00 for the item, \texteuro 480.00 for the
    delivery and \pounds 12.50 in tax; \SI{45}{\percent} of the budget is gone.
    The \texttt{siunitx} route keeps quantity and unit together, so a line break
    cannot put the number at the end of one line and the sign on the next.
    
    \section*{Special characters inside maths}
    
    The braces below are \emph{printed}, because \verb|\{| and \verb|\}| carry
    math-open and math-close delimiter codes rather than grouping:
    \[
      \mu(A \cap B) \le \min\{\mu(A), \mu(B)\}, \qquad
      \{x : \lvert x\rvert < 1\}
    \]
    Grouping and printing are different jobs: \verb|{a,b}| on its own would
    vanish, and \verb|\left\{ ... \right\}| sizes the braces to the content.
    
    Subscripts and superscripts are the whole reason \verb|_| and \verb|^| cannot
    be typed in maths, and \verb|\&| is legal only at an alignment tab:
    \[
      \begin{aligned}
        a_{ij}^{2} &= \sum_{k} a_{ik} a_{kj}^{2} \\
        b_{1}      &= 2a_{1} - 3
      \end{aligned}
    \]
    
    \section*{A heading is a moving argument}
    
    % \verb inside \section fails: the argument is written to the .toc file.
    \section{Files named \texttt{final\_v2.tex}}
    To keep verbatim material in a heading you need the \texttt{cprotect}
    package, or you accept \texttt{\textbackslash texttt\{...\}} with an escaped
    underscore, as above.
    
    \end{document}

    アクセント:コマンド、文字、その下のエンコーディング

    アクセント命令は引数を一つ取り、ラテン文字を覆います。\'{e} がアキュート、\`a グレイヴ、\^o サーカムフレックス、\"u ウムラウト、\~n チルダ、\v{c} カロン、\c{c} セディラ、\.{z} 上点、\u{g} ブレーヴェ、\={a} マクロン、\b{o} 下線、\d{n} 下点、\H{o} 二重アキュート、\r{a} 上リング、\k{a} オゴネク、\t{oo} 連結記号。アクセントではない文字には各自のコマンドがあります、\o/\O、\l/\L、\ae/\AE、\oe/\OE、\aa/\AA および \ss、上点を省いた \i と \j は自分で i の上に何かを載せたいときの出番です。大文字のSharp S は Unicode にありますが伝統的な 8bit のテキストフォントにスロットが無い——その文字が XeLaTeX か LuaLaTeX を待つ幾多の理由の一つです。

    これらの命令が一つの字形を出すか合成で組むかを決めるのはフォントエンコーディングで、OT1 と T1 の実用上の差もここです。T1 を \usepackage[T1]{fontenc} で読み、スロットを持つフォントを合わせれば \'e は合成済みの é になります。語の途中でハイフネーションでき、PDF からコピーでき、スモールキャップもアクセント付きの形を得ます。OT1 では文字の上にアクセントを印字時に載せるため、その位置で語は切れず、テキスト抽出は二文字を返し、存在しない形もあります。2018 年リリース以降 UTF-8 が LaTeX の既定入力エンコーディングなので、T1 を読んだ pdfLaTeX でも é を直接打てます。\'e と書いたソースが文字化けに強いので、アクセント命令が使える状態を保っている面もあります。

    字形の欠落は発明されず報告されます。ログの Missing character: There is no ż in font cmr10 はフォントへの訴えで、直す順は T1 もしくはそれを含むエンコーディング、Latin Modern の範囲を広げる \usepackage{lmodern}、8bit 世界の外側の文字なら newtxtext/XCharter/fontspec です。トルコ語とポーランド語が古典的な罠なのは、点付きと点無し I が意味を持つからで(\i、\I、\k{a})、スロットの無いフォントは pdfLaTeX では黙って代替し、XeLaTeX ではしません。

    数式には数式専用の集合が別にあります。\hat、\widehat、\tilde、\widetilde、\bar/\overline、\vec、\dot、\ddot、\check、\breve、\acute、\grave そして \mathring。これらは数式素へのアクセント配置で文字の分類を変えません。\hat{\gamma} は \gamma と、太字にしたり大きさを変えたりする同じ対象にはなりません——数式中のアクセントの参考ページを下にリンクしている理由です。

    \acute{e} \quad \grave{a} \quad \hat{o} \quad \check{c} \quad \tilde{n} \quad \bar{a} \quad \dot{x} \quad \ddot{u} \quad \mathring{a} \quad \breve{g}
    \acute{e} \quad \grave{a} \quad \hat{o} \quad \check{c} \quad \tilde{n} \quad \bar{a} \quad \dot{x} \quad \ddot{u} \quad \mathring{a} \quad \breve{g}
    The LaTeX text accent commands with the glyph each produces and the math accent that looks similar but is a different mechanism TEXT ACCENTS: ONE ARGUMENT, EITHER A LETTER OR A BRACED GROUP COMMAND NAME OUTPUT THE LOOK-ALIKE IN MATHS \'{e} \^{o} \"{u} \~{n} \v{c} \c{c} \.{z} \u{o} \={a} \r{a} acute circumflex diaeresis tilde caron cedilla dot above breve macron ring above é ô ü ñ č ç ż ŏ ā å ´e \acute{e} ô \hat{o} û \ddot{u}? ñ \tilde{n} č \check{c} no math cedilla ẋ \dot{x} ŏ \breve{g} no math macron å \mathring{a} LETTERS THAT ARE NOT ACCENTS \o \O \ae \AE \l \L \ss \aa \AA \oe \i \j \textendash The last pair are the dotless letters, for composing something over an i by hand. ONE MORE SET LIVES IN textcomp \texteuro \textdollar \textsterling \textbar \textless \textgreater Load \usepackage{textcomp} first: these come from the companion font, not cmr. UNDER T1: ONE GLYPH \H{u} -> double acute, ü -> umlaut Precomposed é is a real letter: it hyphenates, searches and copies out of the PDF as one char. UNDER OT1: TWO PLACED PARTS \H{u} -> double acute, ü -> umlaut The accent is printed over the letter at ship-out, so no break inside the word and copy-paste splits it.
    本文のアクセント命令、各が出す字形、そして別の表に住む数式側の対応物。

    特別ではないが正しい形がある句読点

    引用符は初心者の最も目立つ癖です。ASCII の " は真っ直ぐな二重記号で、正しい組版はバックティック二つで開きアポストロフ二つで閉じます。ソースで ``like this'' と書けば曲がった対になり、`like this' なら単数形です。入れ子の引用は片側四文字要ります。覚えられないなら csquotes パッケージの \enquote{...} があり、babel 経由でフランス語やドイツ語の慣習も処理してくれます。

    ダッシュは三つの別の文字です。- は語の中のハイフン、-- は範囲の en ダッシュ(2019--2024、pp.~12--18)と数式のマイナス、--- は挿入句を切る em ダッシュ。ピリオドを三つ打つと文末スペースファクタ付きの三つのピリオドになります。省略記号は本文が \ldots、数式が \dots で、数式では位置が効きます——amsmath の \dotsc、\dotsb、\dotsm、\dotsi は後に続くもの応じて点を軸上か基準線に置きます。

    通貨は技術的に正しいエスケープが組版的に弱い例です。\$100 は本文フォントから ASCII のドル字形を持ってきます。正式な字形は \pounds、\texteuro、\textdollar で、textcomp 由来、構造的な答えは量・空き・記号を一つの対象に保つことで、割合なら \SI{45}{\percent}、金額なら siunitx の通貨サポートを使い、行分割が数字と記号を離さないようにします。

    文末の空きがこの家系の最後で、実在の仕組みです。ピリオドの後 TeX は \spacefactor を上げて続く空きを広くします。文末では正しい挙動ですが Mr. Smith、1. Introduction、i.e. hence では誤りです。直しは制御スペースの明示(Mr.\ Smith)、ピリオドを走査器から隠す中括弧(Mr.{})、語で終わるマクロなら xspace パッケージです。

    組版を拒む材料を印刷する:verb、verbatim、detokenize

    コード断片の中で十二文字を手でエスケープするのは、問題より悪い作業です。\verb|...| が文中の答えで、同じ区切り記号の間のものは空きまで含めてそのまま印字され、エスケープもコマンド解釈も起きません。制限はすべて実装(活性の区切り文字と分類コードの書き換え)の帰結です。| は | を含む文字列の区切りにはできない、\verb は他コマンドの引数に置けない——つまり見出し・キャプション・脚注・動く引数の中に \verb は入れられない、段落をまたげない、です。

    verbatim はそれ自身の環境の中で限界を持ち上げるので \begin{verbatim}...\end{verbatim} は何でも収められます——ただし環境自体がコマンドなので他コマンドの引数に嵌套できず、内容に \end{verbatim} と一字一句同じ行があってはなりません。アスタリスク付きの verbatim* は動く引数に入ったとき材料を .aux から遠ざけ、\begin{verbatim} within a file you plan to \\input は同じ分類コード機構で読まれるので安全です。

    書式を付けたり枠を引いたり行番号を振りたいなら listings が道具です。自前(やや弱い)のエスケープ規則を持ち、空白を保ち、強調もできます。通常の本文での代替は、文字ごとにエスケープした \texttt{}、そして plain TeX の \string\frac で、これは制御語の名前を走らせず文字として印字します——自分の出力を見せるマクロ全部の裏技です。

    三つすべてに繰り返す罠があります。\ は verbatim ブロックの中では普通の文字になってしまうので、選んだ区切り記号をブロックに含められません。より重要なのは、中の \end{verbatim} が環境の終端として読まれることです。リテラルの \end{ を含めなければならないなら、引数の読み方が違う listings を選んでください。

    % Listings of un-typesettable material: the four tools, weakest first.
    \documentclass[11pt]{article}
    \usepackage[T1]{fontenc}
    \usepackage{lmodern}
    \usepackage{amsmath}
    \usepackage{verbatim}
    \usepackage{listings}
    
    \begin{document}
    
    One character or one short run inside a sentence: \verb|a_b#c|, and with a
    different delimiter so that a bar can be printed, \verb+#&_|+.
    
    A whole block, unsanitised:
    \begin{verbatim}
      for i in range(10):        # % $ & _ { } ^ ~ all arrive untouched
          print(f"{i}\t{i^2}")
    \end{verbatim}
    
    The starred form \verb*|trailing space  | is the one that shows blanks as
    visible boxes, which is what you want when the point of the sample is a
    space. The \verb|verbatim| environment cannot appear inside another
    command's argument, and \verb|\verb| cannot appear in a section heading, a
    caption or a footnote: those arguments are moved, and \verb|\protect| does
    not rescue either of them. For code that needs a frame, a language or line
    numbers, use the \texttt{listings} package:
    
    \begin{lstlisting}[basicstyle=\ttfamily\small,frame=single]
      SELECT a, b FROM t WHERE a LIKE '%x_'   -- % and _ stay literal
    \end{lstlisting}
    
    One asymmetry to remember: inside \texttt{lstlisting} the percent sign is
    still just a character, but the environment reads its own closing line, so a
    sample that has to contain \verb|\end{lstlisting}| needs a different tool.
    
    \end{document}

    ラテンを超える文字:エンジン、エンコーディング、必要なのは存在するフォント

    ラテン以外の文字が現れるには三層が合意する必要があります。ファイルのバイト(既定は UTF-8)、フォントが選ばれるエンコーディング、そして実際に字形を持つフォントです。pdfLaTeX でも相当のことできますが、文字体系ごとに自分のエンコーディング表と自分のフォントが要ります——キリル文字なら T2A と lh ファミリ、ギリシャ文字なら LGR と cbgreek フォント——ハイフネーションと綴りも連れて行く babel の言語指定も。\usepackage[T2A,T1]{fontenc} と \usepackage[russian,english]{babel} の後、\foreignlanguage{russian}{\dots} で一部分だけ切り替えられます。

    それ以外——中国語・日本語・韓国語、アラビア文字とヘブライ文字、デヴァナーガリー、IPA、絵文字——では 8bit 通路が行き止まりで、答えは Unicode エンジンの XeLaTeX か LuaLaTeX に fontspec、言語別の振る舞いは polyglossia か現代的な babel です。こちらは指定した OpenType ファイルから名前付きで文字を選び、ハイフネーションと進行方向は言語モジュールが供給、\newfontfamily\cjkfont{Noto Serif CJK SC} はエンコーディング計画の代わりに一行で済みます。双方向テキストも同じ通路が必要で、bidi はあれらエンジン側の機能です。

    失敗はいつも同じ二通りで、どちらもログに出ます。Missing character ... in font はエンコーディングは正しくフォントが不正という意味、より広いフォントか正しい OpenType 範囲を持つフォントを選んでください。Package inputenc Error: Unicode character … not set up for use with LaTeX は逆に、フォントは字形を持つが 8bit 入力層に写像が無い、つまり pdfLaTeX の壁です。越え方はエンジン変更で、もう一つパッケージを追加することではありません。

    混合ケースを一つ挙げておきます。科学文章で最頻品だからです。どの本文フォントにも存在しない記号——チェック印、矢印、推定記号。数式フォントは多くを載せていて(\checkmark の amssymb、\leftarrow たち)、テキストマクロの中に \ensuremath{\checkmark} を置くのが正当なパターンであり、数式が持たないものは pifont の \ding{51} と \ding{55} が用意されているわけです。

    表、そして多くのバグを終らせる三規則

    この節のリストは、通るファイル一つにエスケープ一式をまとめました。八つのバックスラッシュ・エスケープ、\textasci… 系三つのコマンド、縦棒と山括弧に対する text-comp の追加、それぞれのアクセント命令と自分の出力、合字を切る \textcompwordmark、下線とバックスラッシュを生かしたままのファイル名二つを \verb で引用しています。スクロールするページではなく、文書の開始点として置いてください。

    規則の一、文字が消えたり化けたり行をコメントアウトするなら、十文字のどれかで、直しはパッケージではなくその文字のエスケープです。規則の二、文字のエラーがフォントか分類コードを話すなら十文字のどれでもなく、直しはエンコーディングかエンジンかフォント。規則の三、本文ではなくコードなら、そもそもエスケープしない——\verb、verbatim、listings のどれかを、引数の内に住む必要があるかで決めます。

    これらの規則を無視した結果の二大失敗が mistakes 一覧に出ています。行末を食べて二行先でエラーを出すパーセント、TeX に本物の要求として解釈される未エスケープの & か # です——前者は列数違いを報告する表のセルの中、後者はシャープの倍返しを要求する \newcommand の中身。

    そして、通貨・アクセント付きの姓名・コード断片・キリル文字の引用が混在する文書なら、UTF-8 で書き、T1 を lmodern と合わせ、amssymb と textcomp を使えるようにして latexmk でビルドしてください。このページの三つのリストはすべてその導言部で完結し、打ったまま通ります。

    The ten reserved characters read side by side in text mode and in maths mode, with what each one does and the command that prints it SAME CHARACTER, TWO CONTRACTS — THE CATEGORY IS FIXED, WHICH MODE ACTS ON IT IS NOT CHAR TEXT MODE — PROSE, A TABULAR CELL MATHS MODE — $..$, \[..\], ALIGN TO PRINT IT % $ & # _ { } ~ ^ \ ends the line — the rest of it is gone opens maths: every rule after it changes the tab of tabular; elsewhere, an error Misplaced # — it marks a macro parameter dropped in silence: a file name loses it group; they print nothing at all active: a space no line break may use dropped in silence, exactly like _ escape: the next token becomes a name identical — it comments in every mode closes the group and reopens the text rules the tab of aligned, array, matrix only same; in a \newcommand body write ## instead starts a subscript; a second one is an error group too — escaped they are delimiters the same tie; the relation is \sim starts a superscript: Double superscript same; a doubled one ends the line there \% \$ \textdollar \& \# \_ \text{\_} \{ \} \lbrace \textasciitilde \textasciicircum \textbackslash{} amber = the damage is invisible: a bare % eats the rest of the line, and bare ^ _ simply vanish from the page THE CATCODE IS NOT A FONT CHOICE TeX sorts each character into a category once, as it reads the file; the modes \catcode`\_=12 % ordinary again PRINTING ONE FROM INSIDE MATHS the backslash forms are text commands, so from maths you go back through text first: \text{\_} \text{\%} \text{\&} CODE NEEDS NO ESCAPES AT ALL \verb|a_b#c| verbatim they make the whole set ordinary by catcode — but not inside a heading.
    本文モードと数式モードは同じ十文字へ違う仕事をさせる。エスケープと印字結果は本文側の表に属します。
    \documentclass[11pt]{article}
    \usepackage[T1]{fontenc}       % precomposed accented glyphs, hyphenation works
    \usepackage{lmodern}           % a vector font with the T1 glyphs filled in
    \usepackage{textcomp}          % \texteuro, \textbar, \textless, \textcompwordmark
    \usepackage{amssymb}           % \checkmark
    \usepackage{hyperref}
    
    % UTF-8 input has been the default since the 2018 LaTeX release, so this
    % document can contain é, ç, ø and 漢字 directly; the \accent forms below are
    % shown because they still work and because diffs are easier to read.
    
    \begin{document}
    
    \section*{The escapes}
    
    100\% of the invoice is \$100; the contract is between Smith \& Jones; the
    file \verb|report_final.pdf| lives in
    \texttt{C:\textbackslash tmp\textbackslash work}. Note the caret
    \textasciicircum, the tilde \textasciitilde, the grave \textasciigrave, the
    bar \textbar{} and the angle brackets \textless x \textgreater.
    
    Curly braces need a backslash: \{ \} --- and the em dash is three hyphens,
    the en dash two. Quotation marks open with two backticks and close with two
    apostrophes: ``like this'', and a single quote is `like this'.
    
    \section*{Accents, both routes to the same glyph}
    
    The left column is quoted with \verb|\verb| so that it prints as source; the
    right column is the same characters let through by the encoding layer.
    
    \begin{tabular}{@{}ll@{\hspace{2em}}ll@{}}
      \textbf{Source}   & \textbf{Result} & \textbf{Source}   & \textbf{Result} \\
      \verb|\'e|        & \'e             & \verb|\={a}|       & \={a} \\
      \verb|\`a|        & \`a             & \verb|\b{o}|       & \b{o} \\
      \verb|\^o|        & \^o             & \verb|\d{n}|       & \d{n} \\
      \verb|\"u|        & \"u             & \verb|\H{o}|       & \H{o} \\
      \verb|\~n|        & \~n             & \verb|\r{a}|       & \r{a} \\
      \verb|\v{c}|      & \v{c}           & \verb|\u{g}|       & \u{g} \\
      \verb|\c{c}|      & \c{c}           & \verb|\k{a}|       & \k{a} \\
      \verb|\.z|        & \.z             & \verb|\t{oo}|      & \t{oo} \\
      \verb|\o|         & \o              & \verb|\L|          & \L{} \\
      \verb|\ss|        & \ss             & \verb|\ae|         & \ae \\
      \verb|é ç ø í| & UTF-8 input   & \verb|f\textcompwordmark i| & no ligature: f\textcompwordmark i \\
    \end{tabular}
    
    \section*{Where they bite}
    
    A percent sign with no backslash comments out the rest of the line, so an
    unclosed brace two lines later is the error you get: % this is invisible now
    
    Ampersands belong to tables: \& in prose, and in maths only inside an
    alignment. A hash sign outside a macro definition is never what you meant,
    and inside a \verb|\newcommand| body a literal hash has to be doubled.
    
    \end{document}

    このレッスンの早見表

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

    セクション キーコード
    \begin{array}{c|l|l}\text{source} & \text{role in TeX} & \text{to print it} \\\hline \texttt{\textbackslash\%} & \text{comment} & \text{percent} \\ \texttt{\textbackslash\$} & \text{math shift} & \text{dollar} \\ \texttt{\textbackslash\&} & \text{alignment tab} & \text{ampersand} \\ \texttt{\textbackslash\#} & \text{parameter} & \text{hash} \\ \texttt{\textbackslash\_} & \text{subscript} & \text{underscore} \\ \texttt{\textasciicircum} & \text{superscript} & \text{caret} \\ \texttt{\textasciitilde} & \text{active} & \text{tilde} \\ \texttt{\textbackslash\{\ \textbackslash\}} & \text{grouping} & \text{braces}\end{array}
    \documentclass[10pt]{article}
    \acute{e} \quad \grave{a} \quad \hat{o} \quad \check{c} \quad \tilde{n} \quad \bar{a} \quad \dot{x} \quad \ddot{u} \quad \mathring{a} \quad \breve{g}
    \documentclass[11pt]{article}

    よくある間違い

    • 本文でも数式でも % を素で打つこと。行の残り全部がコメントになり、TeX が報告するのは後の行の閉じられていない中括弧です。\% と書いてください。
    • 文章の中で & と打って、Misplaced alignment tab character & を表のバグとして読むこと。整列の外では不正な文字なので、答えは \& です。
    • # の定義の中にリテラルの \newcommand を入れること。中身はパラメータ語として走査されるので、シャープは ## と二つ重ねます。通常の文章中なら \# です。
    • \_ が数式の中でも効くと期待すること。本文モードのコマンドなので、数式中の印刷は \text{\_}(amsmath)でやることになります。逆方向も同じで、本文に素で打った _ は下線として組まれず黙って消えます。
    • \verb|...| を見出し・キャプション・脚注に置くこと。動く引数では頑丈でないので、エスケープした \texttt{} か、cprotect パッケージのどちらかになります。
    • 引用符を "this"、ダッシュを - と打ち、あとはフォントのせいにすること。``this''、--、--- が意図された形です。
    • 新しいインストールでアクセント付き文字の問題を直すのに \usepackage[utf8]{inputenc} を足すこと。UTF-8 はすでに既定で、実欠けはたいていフォントエンコーディング——\usepackage[T1]{fontenc} に lmodern の組み合わせです。

    この課に関する質問

    パーセント記号は打つ?
    バックスラッシュを添えます。50\% と書けば 50% が出ます。素の % は TeX のコメント文字で、どのモードでもその行の残りを消すため、エラーは普通二行先に出て、正しく閉じたはずの中括弧を文句言います。
    アンダースコアは打つ?
    本文では \_ が文字を出します。バックスラッシュ無しだと下線は捨てられます——数式の中だけの意味だからです。数式では _ が下付きを開始するので、印刷形は本文モードから持ってくる必要があり、amsmath と共に \text{\_} と書きます。^ と \textasciicircum も同じ非対称です。
    Misplaced alignment tab character が出るのは?
    TeX が整列を作っていない箇所で未エスケープの & が出ています。& は tabular、array、align、matrix の列区切りで、文章の中では \& にしなければなりません。メッセージは文字名を挙げるので、直しは環境全体ではなくその一語です。
    é や ñ のアクセント文字は?
    直接打つか、文字を引数に取るアクセント命令を使うかです。UTF-8 入力は既定で、T1 の fontenc と字形を持つフォントがあれば pdfLaTeX でも通ります。命令形は \'{e} がアキュート、\`a グレイヴ、\^o サーカムフレックス、\"u ウムラウト、\~n チルダ、\v{c} カロン、\c{c} セディラ、\r{a} リング、\u{g} ブレーヴェ、\={a} マクロン。XeLaTeX か LuaLaTeX ならどんな Unicode 文字でも動き、数式のアクセントは \hat、\tilde、\bar、\vec、\dot、\ddot、\check、\breve、\mathring の別系統です。
    LaTeX の中で LaTeX のコードは印刷する?
    短い断片は \verb|...|、塊は verbatim 環境、枠や行番号や言語強調が要るものは listings。覚えておく制約は \verb が他コマンドの引数に置けないことで、見出し・キャプション・脚注はエスケープした \texttt{...} のほうになります。
    PDF で小なり記号が消えるのは?
    < と > は普通の文字ですが旧い OT1 フォントエンコーディングにスロットが無く、何も印字されないためです。\usepackage[T1]{fontenc} に lmodern を合わせるか、textcomp のエンコーディングに依存しない \textless と \textgreater を使ってください。数式中なら \lt と \text{<} で、生の文字ではありません。

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

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

    KaTeX エラー
    例を試す

    続けて読む

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