1---2name: latex3description: Write LaTeX documents with correct syntax, packages, and compilation workflow.4---5
6## Special Characters
7
8- Reserved chars need escape: `\# \$ \% \& \_ \{ \} \textbackslash`
9- Tilde as character: `\textasciitilde` not `\~` (that's an accent)
10- Caret: `\textasciicircum` not `\^`
11- Backslash in text: `\textbackslash` not `\\` (that's line break)
12
13## Quotes & Dashes
14
15- Opening quotes: ``` `` ``` not `"`; closing: `''`—never use straight `"` quotes
16- Hyphen `-`, en-dash `--` (ranges: 1--10), em-dash `---` (punctuation)
17- Minus in math mode: `$-1$` not `-1` in text
18
19## Math Mode
20
21- Inline: `$...$` or `\(...\)`; display: `\[...\]` or `equation` environment
22- Text inside math: `$E = mc^2 \text{ where } m \text{ is mass}$`
23- Multiline equations: `align` environment, not multiple `equation`s
24- `\left( ... \right)` for auto-sizing delimiters—must be paired
25
26## Spacing
27
28- Command followed by text needs `{}` or `\ `: `\LaTeX{}` or `\LaTeX\ is`
29- Non-breaking space: `~` between number and unit: `5~km`
30- Force space in math: `\,` thin, `\:` medium, `\;` thick, `\quad` `\qquad`
31
32## Packages
33
34- `\usepackage` order matters—`hyperref` almost always last
35- `inputenc` + `fontenc` for UTF-8: `\usepackage[utf8]{inputenc}` `\usepackage[T1]{fontenc}`
36- `graphicx` for images, `booktabs` for professional tables, `amsmath` for advanced math
37- `microtype` for better typography—load early, subtle but significant improvement
38
39## Floats (Figures & Tables)
40
41- `[htbp]` suggests placement: here, top, bottom, page—not commands
42- LaTeX may move floats far from source—use `[H]` from `float` package to force
43- Always use `\centering` inside float, not `center` environment
44- Caption before `\label`—label references the last numbered element
45
46## References
47
48- Compile twice to resolve `\ref` and `\pageref`—first pass collects, second uses
49- `\label` immediately after `\caption` or inside environment being labeled
50- For bibliography: latex → bibtex → latex → latex (4 passes)
51- `hyperref` makes refs clickable—but can break with some packages
52
53## Tables
54
55- `tabular` for inline, `table` float for numbered with caption
56- Use `booktabs`: `\toprule`, `\midrule`, `\bottomrule`—no vertical lines
57- `@{}` removes padding: `\begin{tabular}{@{}lll@{}}`
58- Multicolumn: `\multicolumn{2}{c}{Header}`; multirow needs `multirow` package
59
60## Images
61
62- Path relative to main file or set with `\graphicspath{{./images/}}`
63- Prefer PDF/EPS for pdflatex/latex; PNG/JPG for photos
64- `\includegraphics[width=0.8\textwidth]{file}`—no extension often better
65
66## Common Errors
67
68- Overfull hbox: line too long—rephrase, add `\-` hyphenation hints, or allow `\sloppy`
69- Missing `$`: math command used in text mode
70- Undefined control sequence: typo or missing package
71- `\include` adds page break, `\input` doesn't—use `\input` for fragments
72
73## Document Structure
74
75- Preamble before `\begin{document}`—all `\usepackage` and settings
76- `\maketitle` after `\begin{document}` if using `\title`, `\author`, `\date`
77- `article` for short docs, `report` for chapters without parts, `book` for full books