<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>Yueswater Blog</title>
<link>https://yueswater.com/articles/index-en.html</link>
<atom:link href="https://yueswater.com/articles/index-en.xml" rel="self" type="application/rss+xml"/>
<description>Tutorials and technical notes on Python, R, and LaTeX by Anthony — written for learners at all levels.</description>
<image>
<url>https://yueswater.com/images/og-image.png</url>
<title>Yueswater Blog</title>
<link>https://yueswater.com/articles/index-en.html</link>
<height>81</height>
<width>144</width>
</image>
<generator>quarto-1.8.24</generator>
<lastBuildDate>Sun, 10 May 2026 00:00:00 GMT</lastBuildDate>
<item>
  <title>LaTeX Tutorial Series: Mathematical Typesetting</title>
  <dc:creator>Anthony </dc:creator>
  <link>https://yueswater.com/articles/posts/latex/math/math-en.html</link>
  <description><![CDATA[ 




<p>After learning the basic layout settings in LaTeX, it is time to move to its strongest feature: mathematical typesetting.</p>
<p>In many technical fields, formulas are not optional. We use them to define models, report results, and explain proofs. Word processors can handle simple equations, but they often become hard to manage when formulas get long or structured. LaTeX solves this problem with a full math system that is precise and consistent.</p>
<section id="basic-math-setup" class="level2">
<h2 class="anchored" data-anchor-id="basic-math-setup">Basic Math Setup</h2>
<p>Before writing advanced formulas, we should load the right packages. The most common set is the <strong>AMS package family</strong>:</p>
<div id="tbl-ams-packages-en" class="quarto-float quarto-figure quarto-figure-center anchored" data-tbl-colwidths="[25,75]">
<figure class="quarto-float quarto-float-tbl figure">
<figcaption class="quarto-float-caption-top quarto-float-caption quarto-float-tbl" id="tbl-ams-packages-en-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;1: AMS package family
</figcaption>
<div aria-describedby="tbl-ams-packages-en-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<table class="caption-top table">
<colgroup>
<col style="width: 25%">
<col style="width: 75%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Package</th>
<th style="text-align: left;">Main purpose</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;"><a href="https://mirror.ctan.org/macros/latex/required/amsmath/amsldoc.pdf"><code>amsmath</code></a></td>
<td style="text-align: left;">Extra math environments: alignment, multi-line equations, cases, matrices</td>
</tr>
<tr class="even">
<td style="text-align: left;"><a href="https://mirror.ctan.org/fonts/amsfonts/doc/amssymb.pdf"><code>amssymb</code></a></td>
<td style="text-align: left;">More math symbols</td>
</tr>
<tr class="odd">
<td style="text-align: left;"><a href="https://mirror.ctan.org/fonts/amsfonts/doc/amsfonts.pdf"><code>amsfonts</code></a></td>
<td style="text-align: left;">Extra math fonts</td>
</tr>
<tr class="even">
<td style="text-align: left;"><a href="https://mirror.ctan.org/macros/latex/required/amscls/doc/amsthdoc.pdf"><code>amsthm</code></a></td>
<td style="text-align: left;">Theorem-style environments</td>
</tr>
</tbody>
</table>
</div>
</figure>
</div>
<section id="inline-vs-display-math" class="level3">
<h3 class="anchored" data-anchor-id="inline-vs-display-math">Inline vs Display Math</h3>
<p>LaTeX has two main math modes:</p>
<ul>
<li><strong>Inline math</strong>: appears inside normal text</li>
<li><strong>Display math</strong>: shown on its own line</li>
</ul>
<section id="inline-math" class="level4">
<h4 class="anchored" data-anchor-id="inline-math">Inline math</h4>
<p>Common syntax:</p>
<div id="tbl-inline-math-en" class="quarto-float quarto-figure quarto-figure-center anchored" data-tbl-colwidths="[40,60]">
<figure class="quarto-float quarto-float-tbl figure">
<figcaption class="quarto-float-caption-top quarto-float-caption quarto-float-tbl" id="tbl-inline-math-en-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;2: Inline math syntax
</figcaption>
<div aria-describedby="tbl-inline-math-en-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<table class="caption-top table">
<colgroup>
<col style="width: 40%">
<col style="width: 60%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Syntax</th>
<th style="text-align: left;">Note</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;"><code>$...$</code></td>
<td style="text-align: left;">Most common</td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>\(...\)</code></td>
<td style="text-align: left;">Native LaTeX form</td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>\begin{math}...\end{math}</code></td>
<td style="text-align: left;">Longer form</td>
</tr>
</tbody>
</table>
</div>
</figure>
</div>
<div class="latex-runner" data-engine="pdflatex"><pre class="latex-source">\documentclass{article}
\usepackage{amsmath}

\begin{document}

$a^2 + b^2 = c^2$
\( a^2 + b^2 = c^2 \)
\begin{math} a^2 + b^2 = c^2 \end{math}

\end{document}</pre></div>
</section>
<section id="display-math" class="level4">
<h4 class="anchored" data-anchor-id="display-math">Display math</h4>
<p>Common syntax:</p>
<div id="tbl-display-math-en" class="quarto-float quarto-figure quarto-figure-center anchored" data-tbl-colwidths="[40,60]">
<figure class="quarto-float quarto-float-tbl figure">
<figcaption class="quarto-float-caption-top quarto-float-caption quarto-float-tbl" id="tbl-display-math-en-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;3: Display math syntax
</figcaption>
<div aria-describedby="tbl-display-math-en-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<table class="caption-top table">
<colgroup>
<col style="width: 40%">
<col style="width: 60%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Syntax</th>
<th style="text-align: left;">Note</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;"><code>\[...\]</code></td>
<td style="text-align: left;">Simple unnumbered display</td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>\begin{displaymath}...\end{displaymath}</code></td>
<td style="text-align: left;">Same idea, longer form</td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>\begin{equation}...\end{equation}</code></td>
<td style="text-align: left;">Numbered equation</td>
</tr>
</tbody>
</table>
</div>
</figure>
</div>
<p>If you need references like “Eq. (3)”, use <code>equation</code>.</p>
</section>
</section>
</section>
<section id="symbols-and-math-fonts" class="level2">
<h2 class="anchored" data-anchor-id="symbols-and-math-fonts">Symbols and Math Fonts</h2>
<section id="greek-letters" class="level3">
<h3 class="anchored" data-anchor-id="greek-letters">Greek letters</h3>
<p>Greek letters are used everywhere in STEM writing: <img src="https://latex.codecogs.com/png.latex?%5Calpha">, <img src="https://latex.codecogs.com/png.latex?%5Cbeta">, <img src="https://latex.codecogs.com/png.latex?%5Csigma">, <img src="https://latex.codecogs.com/png.latex?%5Cpi">, and so on.</p>
<div id="tbl-greek-en" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-tbl figure">
<figcaption class="quarto-float-caption-top quarto-float-caption quarto-float-tbl" id="tbl-greek-en-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;4: Common Greek letters
</figcaption>
<div aria-describedby="tbl-greek-en-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<table class="caption-top table">
<thead>
<tr class="header">
<th style="text-align: left;">Command</th>
<th style="text-align: center;">Output</th>
<th style="text-align: left;">Command</th>
<th style="text-align: center;">Output</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;"><code>\alpha</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Calpha"></td>
<td style="text-align: left;"><code>\beta</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Cbeta"></td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>\gamma</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Cgamma"></td>
<td style="text-align: left;"><code>\delta</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Cdelta"></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>\theta</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Ctheta"></td>
<td style="text-align: left;"><code>\lambda</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Clambda"></td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>\sigma</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Csigma"></td>
<td style="text-align: left;"><code>\omega</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Comega"></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>\Gamma</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5CGamma"></td>
<td style="text-align: left;"><code>\Sigma</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5CSigma"></td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>\Pi</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5CPi"></td>
<td style="text-align: left;"><code>\Omega</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5COmega"></td>
</tr>
</tbody>
</table>
</div>
</figure>
</div>
</section>
<section id="common-operators-and-relations" class="level3">
<h3 class="anchored" data-anchor-id="common-operators-and-relations">Common operators and relations</h3>
<div id="tbl-operators-en" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-tbl figure">
<figcaption class="quarto-float-caption-top quarto-float-caption quarto-float-tbl" id="tbl-operators-en-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;5: Operators and relations
</figcaption>
<div aria-describedby="tbl-operators-en-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<table class="caption-top table">
<thead>
<tr class="header">
<th style="text-align: left;">Command</th>
<th style="text-align: center;">Symbol</th>
<th style="text-align: left;">Command</th>
<th style="text-align: center;">Symbol</th>
<th style="text-align: left;">Command</th>
<th style="text-align: center;">Symbol</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;"><code>\times</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Ctimes"></td>
<td style="text-align: left;"><code>\leq</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Cleq"></td>
<td style="text-align: left;"><code>\approx</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Capprox"></td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>\div</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Cdiv"></td>
<td style="text-align: left;"><code>\geq</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Cgeq"></td>
<td style="text-align: left;"><code>\equiv</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Cequiv"></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>\pm</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Cpm"></td>
<td style="text-align: left;"><code>\neq</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Cneq"></td>
<td style="text-align: left;"><code>\sim</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Csim"></td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>\cdot</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Ccdot"></td>
<td style="text-align: left;"><code>\ll</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Cll"></td>
<td style="text-align: left;"><code>\propto</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Cpropto"></td>
</tr>
</tbody>
</table>
</div>
</figure>
</div>
</section>
<section id="sets-and-logic" class="level3">
<h3 class="anchored" data-anchor-id="sets-and-logic">Sets and logic</h3>
<div id="tbl-sets-en" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-tbl figure">
<figcaption class="quarto-float-caption-top quarto-float-caption quarto-float-tbl" id="tbl-sets-en-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;6: Sets and logic symbols
</figcaption>
<div aria-describedby="tbl-sets-en-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<table class="caption-top table">
<colgroup>
<col style="width: 14%">
<col style="width: 18%">
<col style="width: 14%">
<col style="width: 18%">
<col style="width: 14%">
<col style="width: 18%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Command</th>
<th style="text-align: center;">Symbol</th>
<th style="text-align: left;">Command</th>
<th style="text-align: center;">Symbol</th>
<th style="text-align: left;">Command</th>
<th style="text-align: center;">Symbol</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;"><code>\in</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Cin"></td>
<td style="text-align: left;"><code>\cup</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Ccup"></td>
<td style="text-align: left;"><code>\forall</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Cforall"></td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>\notin</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Cnotin"></td>
<td style="text-align: left;"><code>\cap</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Ccap"></td>
<td style="text-align: left;"><code>\exists</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Cexists"></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>\subseteq</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Csubseteq"></td>
<td style="text-align: left;"><code>\emptyset</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Cemptyset"></td>
<td style="text-align: left;"><code>\Rightarrow</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5CRightarrow"></td>
</tr>
</tbody>
</table>
</div>
</figure>
</div>
</section>
<section id="math-font-commands" class="level3">
<h3 class="anchored" data-anchor-id="math-font-commands">Math font commands</h3>
<div id="tbl-mathfonts-en" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-tbl figure">
<figcaption class="quarto-float-caption-top quarto-float-caption quarto-float-tbl" id="tbl-mathfonts-en-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;7: Common math font commands
</figcaption>
<div aria-describedby="tbl-mathfonts-en-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<table class="caption-top table">
<thead>
<tr class="header">
<th style="text-align: left;">Command</th>
<th style="text-align: center;">Output</th>
<th style="text-align: left;">Typical use</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;"><code>\mathcal{N}</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Cmathcal%7BN%7D"></td>
<td style="text-align: left;">Distribution or family notation</td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>\mathbb{R}</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Cmathbb%7BR%7D"></td>
<td style="text-align: left;">Number sets</td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>\mathbf{v}</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Cmathbf%7Bv%7D"></td>
<td style="text-align: left;">Vectors and matrices</td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>\mathrm{d}</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Cmathrm%7Bd%7D"></td>
<td style="text-align: left;">Differential operator</td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>\boldsymbol{\alpha}</code></td>
<td style="text-align: center;"><img src="https://latex.codecogs.com/png.latex?%5Cboldsymbol%7B%5Calpha%7D"></td>
<td style="text-align: left;">Bold Greek symbols</td>
</tr>
</tbody>
</table>
</div>
</figure>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p><code>\mathbb</code> and <code>\mathcal</code> are typically used with <code>amssymb</code>/<code>amsfonts</code>, and <code>\boldsymbol</code> needs <code>amsmath</code>.</p>
</div>
</div>
</section>
</section>
<section id="practical-advanced-skills" class="level2">
<h2 class="anchored" data-anchor-id="practical-advanced-skills">Practical Advanced Skills</h2>
<section id="fractions-and-roots" class="level3">
<h3 class="anchored" data-anchor-id="fractions-and-roots">Fractions and roots</h3>
<p>Use <code>\frac{a}{b}</code> for fractions and <code>\sqrt{...}</code> for square roots.</p>
<div class="latex-runner" data-engine="pdflatex"><pre class="latex-source">\[
x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}
\]

\[
\sqrt[3]{8} = 2, \quad \sqrt{\sqrt{x}} = x^{1/4}
\]</pre></div>
</section>
<section id="spacing-and-text-in-math-mode" class="level3">
<h3 class="anchored" data-anchor-id="spacing-and-text-in-math-mode">Spacing and text in math mode</h3>
<p>LaTeX ignores normal spaces in math mode. Use spacing commands when needed:</p>
<div id="tbl-spacing-en" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-tbl figure">
<figcaption class="quarto-float-caption-top quarto-float-caption quarto-float-tbl" id="tbl-spacing-en-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;8: Math spacing commands
</figcaption>
<div aria-describedby="tbl-spacing-en-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<table class="caption-top table">
<thead>
<tr class="header">
<th style="text-align: left;">Command</th>
<th style="text-align: left;">Space size</th>
<th style="text-align: left;">Typical use</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;"><code>\,</code></td>
<td style="text-align: left;">thin</td>
<td style="text-align: left;">before <code>\mathrm{d}x</code>, unit spacing</td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>\:</code></td>
<td style="text-align: left;">medium</td>
<td style="text-align: left;">slightly wider operator spacing</td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>\;</code></td>
<td style="text-align: left;">thick</td>
<td style="text-align: left;">stronger separation</td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>\quad</code></td>
<td style="text-align: left;">1em</td>
<td style="text-align: left;">visible separation</td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>\qquad</code></td>
<td style="text-align: left;">2em</td>
<td style="text-align: left;">larger separation</td>
</tr>
</tbody>
</table>
</div>
</figure>
</div>
<p>If you need normal words in formulas, use <code>\text{...}</code>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode latex code-with-copy"><code class="sourceCode latex"><span id="cb1-1"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">\[</span></span>
<span id="cb1-2"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f(x) = x^2 </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\quad</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\text</span>{where}<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\quad</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> x </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\in</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\mathbb</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">{R}</span></span>
<span id="cb1-3"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">\]</span></span></code></pre></div></div>
</section>
<section id="brackets-and-delimiters" class="level3">
<h3 class="anchored" data-anchor-id="brackets-and-delimiters">Brackets and delimiters</h3>
<p>Use <code>\left</code> and <code>\right</code> for auto-sized brackets:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode latex code-with-copy"><code class="sourceCode latex"><span id="cb2-1"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">\[</span></span>
<span id="cb2-2"><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\left</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">( </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\frac</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">{a}{b} + </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\frac</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">{c}{d} </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\right</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">)^2</span></span>
<span id="cb2-3"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">\]</span></span></code></pre></div></div>
<p>For one-sided delimiters, use an invisible boundary like <code>\left.</code>.</p>
</section>
<section id="alignment-align" class="level3">
<h3 class="anchored" data-anchor-id="alignment-align">Alignment (<code>align</code>)</h3>
<p>Use <code>align</code> for multi-line derivations with aligned symbols:</p>
<div class="latex-runner" data-engine="pdflatex"><pre class="latex-source">\begin{align}
(a+b)^2 &amp;= a^2 + 2ab + b^2 \\
(a-b)^2 &amp;= a^2 - 2ab + b^2
\end{align}</pre></div>
<p>Use <code>align*</code> when you do not want equation numbers.</p>
</section>
<section id="matrices" class="level3">
<h3 class="anchored" data-anchor-id="matrices">Matrices</h3>
<div id="tbl-matrix-en" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-tbl figure">
<figcaption class="quarto-float-caption-top quarto-float-caption quarto-float-tbl" id="tbl-matrix-en-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Table&nbsp;9: Matrix environments
</figcaption>
<div aria-describedby="tbl-matrix-en-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<table class="caption-top table">
<thead>
<tr class="header">
<th style="text-align: left;">Environment</th>
<th style="text-align: left;">Bracket style</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;"><code>matrix</code></td>
<td style="text-align: left;">none</td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>pmatrix</code></td>
<td style="text-align: left;"><code>( )</code></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>bmatrix</code></td>
<td style="text-align: left;"><code>[ ]</code></td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>vmatrix</code></td>
<td style="text-align: left;"><code>| |</code></td>
</tr>
</tbody>
</table>
</div>
</figure>
</div>
</section>
<section id="piecewise-functions" class="level3">
<h3 class="anchored" data-anchor-id="piecewise-functions">Piecewise functions</h3>
<p>Use <code>cases</code>:</p>
<div class="latex-runner" data-engine="pdflatex"><pre class="latex-source">\[
f(x) = \begin{cases}
  x^2,  &amp; \text{if } x \ge 0 \\
 -x^2,  &amp; \text{if } x &lt; 0
\end{cases}
\]</pre></div>
</section>
<section id="custom-operators" class="level3">
<h3 class="anchored" data-anchor-id="custom-operators">Custom operators</h3>
<p>For operators like <code>var</code>, <code>cov</code>, <code>rank</code>, define them properly:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode latex code-with-copy"><code class="sourceCode latex"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\DeclareMathOperator</span>{<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\var</span>}{var}</span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\DeclareMathOperator</span>{<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\cov</span>}{cov}</span>
<span id="cb3-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\DeclareMathOperator*</span>{<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\argmax</span>}{arg<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\,</span>max}</span></code></pre></div></div>
<p>This gives better spacing and correct limit placement.</p>
</section>
</section>
<section id="equation-environments" class="level2">
<h2 class="anchored" data-anchor-id="equation-environments">Equation Environments</h2>
<section id="equation" class="level3">
<h3 class="anchored" data-anchor-id="equation"><code>equation</code></h3>
<p>Use <code>equation</code> for one centered formula with one number:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode latex code-with-copy"><code class="sourceCode latex"><span id="cb4-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\begin</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">equation</span>}</span>
<span id="cb4-2"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  e^{i</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\pi</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">} + 1 = 0</span></span>
<span id="cb4-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\end</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">equation</span>}</span></code></pre></div></div>
</section>
<section id="long-equations-with-split" class="level3">
<h3 class="anchored" data-anchor-id="long-equations-with-split">Long equations with <code>split</code></h3>
<p>If one equation is too long, nest <code>split</code> inside <code>equation</code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode latex code-with-copy"><code class="sourceCode latex"><span id="cb5-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\begin</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">equation</span>}</span>
<span id="cb5-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\begin</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">split</span>}</span>
<span id="cb5-3"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">(a+b+c)^2 &amp;= a^2+b^2+c^2 </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span></span>
<span id="cb5-4"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">          &amp;</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\quad</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> + 2ab + 2bc + 2ca</span></span>
<span id="cb5-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\end</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">split</span>}</span>
<span id="cb5-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\end</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">equation</span>}</span></code></pre></div></div>
</section>
<section id="multiple-centered-equations-with-gather" class="level3">
<h3 class="anchored" data-anchor-id="multiple-centered-equations-with-gather">Multiple centered equations with <code>gather</code></h3>
<p>Use <code>gather</code> when lines are independent and do not need alignment.</p>
</section>
</section>
<section id="theorem-environments-amsthm" class="level2">
<h2 class="anchored" data-anchor-id="theorem-environments-amsthm">Theorem Environments (<code>amsthm</code>)</h2>
<section id="numbered-theorem-environments" class="level3">
<h3 class="anchored" data-anchor-id="numbered-theorem-environments">Numbered theorem environments</h3>
<p>Define with <code>\newtheorem</code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode latex code-with-copy"><code class="sourceCode latex"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\newtheorem</span>{theorem}{Theorem}[section]</span>
<span id="cb6-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\newtheorem</span>{lemma}[theorem]{Lemma}</span></code></pre></div></div>
</section>
<section id="unnumbered-environments" class="level3">
<h3 class="anchored" data-anchor-id="unnumbered-environments">Unnumbered environments</h3>
<p>Use a star version:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode latex code-with-copy"><code class="sourceCode latex"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\newtheorem*</span>{remark}{Remark}</span></code></pre></div></div>
</section>
<section id="styles" class="level3">
<h3 class="anchored" data-anchor-id="styles">Styles</h3>
<ul>
<li><code>plain</code>: bold heading + italic body (default)</li>
<li><code>definition</code>: bold heading + normal body</li>
<li><code>remark</code>: italic heading + normal body</li>
</ul>
<p>Switch style with <code>\theoremstyle{...}</code> before defining environments.</p>
</section>
<section id="proof-environment" class="level3">
<h3 class="anchored" data-anchor-id="proof-environment">Proof environment</h3>
<p><code>proof</code> is built in by <code>amsthm</code> and ends with a QED symbol automatically.</p>
</section>
</section>
<section id="tools-for-practice-and-conversion" class="level2">
<h2 class="anchored" data-anchor-id="tools-for-practice-and-conversion">Tools for Practice and Conversion</h2>
<section id="texnique-a-latex-equation-typesetting-game" class="level3">
<h3 class="anchored" data-anchor-id="texnique-a-latex-equation-typesetting-game">TeXnique: A LaTeX Equation Typesetting Game</h3>
<p><a href="https://texnique.xyz/">TeXnique</a> is better understood as a LaTeX equation typesetting game, not just a syntax reference page. You see a target formula, then try to reproduce it with correct LaTeX commands. That loop trains your reflexes: after enough practice, you start to “see” LaTeX when you look at math.</p>
<p>When I first wrote technical reports, the hardest part was not advanced math ideas. It was small formatting details: where to place <code>&amp;</code> in <code>align</code>, when to use <code>\left...\right</code>, and why two similar fraction commands can produce very different visual results. TeXnique works well as a warm-up space for these details. You can test quickly without rebuilding a full document.</p>
<p>If your goal is to build typing fluency, not just memorize commands, TeXnique is a strong starting point. It reduces friction and lets you focus on mathematical meaning instead of syntax anxiety.</p>
<p>The video below gives a quick overview of how people use it in real practice:</p>
<div class="video-embed ratio ratio-16x9">
<p><iframe src="https://www.youtube.com/embed/ZtxdTd6o9Fg" title="TeXnique Tutorial" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen=""></iframe></p>
</div>
<p>After watching, try a small drill: pick three formulas you often use (for example, one limit, one matrix, and one piecewise function), and retype them until you can do it smoothly. This kind of short repetition creates long-term speed gains.</p>
</section>
<section id="mathpix-snip-convert-formula-images-to-latex" class="level3">
<h3 class="anchored" data-anchor-id="mathpix-snip-convert-formula-images-to-latex">Mathpix Snip: Convert Formula Images to LaTeX</h3>
<p><a href="https://mathpix.com/">Mathpix Snip</a> helps you convert formula images into LaTeX quickly. It is especially useful when your source is not editable text: lecture slides, scanned PDFs, whiteboard photos, or handwritten notes.</p>
<p>In one of my own note-cleaning sessions, I had a page full of matrix derivations and conditional expectation expressions. Typing everything manually would take a long time. With Mathpix, I got a usable draft much faster, then spent my time on review instead of raw typing.</p>
<p>That is the key: treat OCR output as a first draft, not a final answer. A practical check sequence is:</p>
<ol type="1">
<li>Similar characters (<code>l</code>, <code>1</code>, <code>I</code>, <code>O</code>, <code>0</code>)</li>
<li>Superscript/subscript nesting</li>
<li>Bracket and delimiter matching</li>
</ol>
<p>The video below shows the basic Mathpix Snip workflow:</p>
<div class="video-embed ratio ratio-16x9">
<p><iframe src="https://www.youtube.com/embed/Jki2FcW8Bw8" title="Mathpix Snip Tutorial" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen=""></iframe></p>
</div>
<p>A robust workflow is: convert in batch first, then do a focused manual pass in your LaTeX document to unify style and correctness. In short, TeXnique trains your intuition, and Mathpix saves your input time. Using both gives the best balance of speed and quality.</p>
</section>
</section>
<section id="exercises" class="level2">
<h2 class="anchored" data-anchor-id="exercises">Exercises</h2>
<div class="exercises">
<section id="question-1" class="level3 exercise" data-answer="C">
<h3 class="anchored" data-anchor-id="question-1">Question 1</h3>
<p>Which environment is best for a single equation that needs numbering and cross-reference?</p>
<div class="options">
<ul>
<li>A. <code>\[...\]</code></li>
<li>B. <code>displaymath</code></li>
<li>C. <code>equation</code></li>
<li>D. <code>math</code></li>
</ul>
</div>
<div class="answer">
<p><strong>Answer: C — <code>equation</code></strong></p>
<p><code>equation</code> provides an automatic equation number, which is necessary for references like Eq. (1).</p>
</div>
</section>
<section id="question-2" class="level3 exercise" data-answer="B">
<h3 class="anchored" data-anchor-id="question-2">Question 2</h3>
<p>In the <code>align</code> environment, which symbol marks the alignment point?</p>
<div class="options">
<ul>
<li>A. <code>\\</code></li>
<li>B. <code>&amp;</code></li>
<li>C. <code>%</code></li>
<li>D. <code>#</code></li>
</ul>
</div>
<div class="answer">
<p><strong>Answer: B — <code>&amp;</code></strong></p>
<p><code>&amp;</code> marks the alignment anchor (usually before <code>=</code>), while <code>\\</code> is used for line breaks.</p>
</div>
</section>
<section id="question-3" class="level3 exercise" data-answer="D">
<h3 class="anchored" data-anchor-id="question-3">Question 3</h3>
<p>If you want to insert normal words such as “if” or “where” inside math mode, what should you use?</p>
<div class="options">
<ul>
<li>A. <code>\mathrm{}</code></li>
<li>B. <code>\operatorname{}</code></li>
<li>C. <code>\textbf{}</code></li>
<li>D. <code>\text{}</code></li>
</ul>
</div>
<div class="answer">
<p><strong>Answer: D — <code>\text{}</code></strong></p>
<p><code>\text{}</code> is designed for normal text inside formulas.</p>
</div>
</section>
<section id="question-4" class="level3 exercise">
<h3 class="anchored" data-anchor-id="question-4">Question 4</h3>
<p>Write a LaTeX snippet for the following piecewise function:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0Af(x)=%0A%5Cbegin%7Bcases%7D%0Ax%5E2,%20&amp;%20x%20%5Cge%200%5C%5C%0A-x%5E2,%20&amp;%20x%20%3C%200%0A%5Cend%7Bcases%7D%0A"></p>
<div class="answer">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode latex code-with-copy"><code class="sourceCode latex"><span id="cb8-1"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">\[</span></span>
<span id="cb8-2"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f(x)=</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\begin</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">cases</span>}</span>
<span id="cb8-3"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  x^2,  &amp; x </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\ge</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> 0 </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\\</span></span>
<span id="cb8-4"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> -x^2,  &amp; x &lt; 0</span></span>
<span id="cb8-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\end</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">cases</span>}</span>
<span id="cb8-6"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">\]</span></span></code></pre></div></div>
<p><code>cases</code> is the standard environment for piecewise definitions.</p>
</div>
</section>
<section id="question-5" class="level3 exercise">
<h3 class="anchored" data-anchor-id="question-5">Question 5</h3>
<p>Declare an <code>argmax</code> operator in the preamble and typeset:</p>
<p><img src="https://latex.codecogs.com/png.latex?%0A%5Carg%5Cmax_%7Bx%20%5Cin%20%5Cmathbb%7BR%7D%7D%20f(x)%0A"></p>
<div class="answer">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode latex code-with-copy"><code class="sourceCode latex"><span id="cb9-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\usepackage</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">amsmath</span>}</span>
<span id="cb9-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\DeclareMathOperator*</span>{<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\argmax</span>}{arg<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\,</span>max}</span>
<span id="cb9-3"></span>
<span id="cb9-4"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">\[</span></span>
<span id="cb9-5"><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\argmax</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">_{x </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\in</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\mathbb</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">{R}} f(x)</span></span>
<span id="cb9-6"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">\]</span></span></code></pre></div></div>
<p><code>\DeclareMathOperator*</code> lets limits appear above/below in display mode.</p>
</div>
</section>
</div>
</section>
<section id="chapter-summary" class="level2">
<h2 class="anchored" data-anchor-id="chapter-summary">Chapter Summary</h2>
<p>In this chapter, you learned the full workflow of LaTeX math typesetting: choosing the right AMS packages, switching between inline and display math, using symbols and math fonts correctly, and handling advanced details like spacing, scalable brackets, alignment, matrices, and piecewise functions.</p>
<p>You also learned how to structure formal mathematical writing with <code>amsthm</code>, including theorem, definition, remark, and proof environments. With these tools, you can produce clear and professional math writing for assignments, reports, and papers.</p>
<p>A good next step is to practice equation labels (<code>\label</code>) and references (<code>\ref</code>) in longer documents.</p>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>LaTeX</category>
  <guid>https://yueswater.com/articles/posts/latex/math/math-en.html</guid>
  <pubDate>Sun, 10 May 2026 00:00:00 GMT</pubDate>
  <media:content url="https://yueswater.com/articles/posts/cover/LaTeX_logo.png" medium="image" type="image/png" height="81" width="144"/>
</item>
<item>
  <title>Python Tutorial Series: lambda, filter, map, and reduce</title>
  <dc:creator>Anthony </dc:creator>
  <link>https://yueswater.com/articles/posts/python/lambda-filter-map-reduce/lambda-filter-map-reduce-en.html</link>
  <description><![CDATA[ 







<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>Python</category>
  <category>Programming Languages</category>
  <guid>https://yueswater.com/articles/posts/python/lambda-filter-map-reduce/lambda-filter-map-reduce-en.html</guid>
  <pubDate>Sun, 10 May 2026 00:00:00 GMT</pubDate>
  <media:content url="https://yueswater.com/articles/posts/cover/Python_logo.png" medium="image" type="image/png" height="81" width="144"/>
</item>
<item>
  <title>Python Tutorial Series: Cleaner and Safer Loops with zip and enumerate</title>
  <dc:creator>Anthony </dc:creator>
  <link>https://yueswater.com/articles/posts/python/zip-enum/python-zip-enum-en.html</link>
  <description><![CDATA[ 




<p>Loops are central to almost every Python project. The syntax is straightforward, and for simple cases, a plain <code>for</code> loop does the job cleanly. Problems arise when you need to track the current index while iterating, or when you need to process two related sequences at the same time. Many beginners reach for <code>range(len())</code> in both situations — it works, but it introduces unnecessary complexity.</p>
<p>Suppose you have the following data:</p>
<pre class="plain-text"><code>Name         Score
Wang Daming   95
Chen Xiaomei  71
Lin Xiaohua   82</code></pre>
<p>You could store this as a dictionary:</p>
<div id="92aaf412" class="cell" data-execution_count="1">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1">scores <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb2-2">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Wang Daming"</span>:  <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">95</span>,</span>
<span id="cb2-3">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Chen Xiaomei"</span>: <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">71</span>,</span>
<span id="cb2-4">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Lin Xiaohua"</span>:  <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">82</span>,</span>
<span id="cb2-5">}</span></code></pre></div></div>
</div>
<p>Or split it into two lists:</p>
<div id="60a533f3" class="cell" data-execution_count="2">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1">names  <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Wang Daming"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Chen Xiaomei"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Lin Xiaohua"</span>]</span>
<span id="cb3-2">grades <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">95</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">71</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">82</span>]</span></code></pre></div></div>
</div>
<p>The traditional approach uses <code>range(len())</code> to generate indices:</p>
<div id="6326abc6" class="cell" data-execution_count="3">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(names)):</span>
<span id="cb4-2">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Name: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>names[i]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  Score: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>grades[i]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Name: Wang Daming  Score: 95
Name: Chen Xiaomei  Score: 71
Name: Lin Xiaohua  Score: 82</code></pre>
</div>
</div>
<p>This works, but it is harder to read than it needs to be, and it carries a hidden risk: if <code>names</code> and <code>grades</code> ever have different lengths, an <code>IndexError</code> will be thrown at runtime with no advance warning.</p>
<p>Python has two built-in functions that eliminate both problems: <code>enumerate()</code> and <code>zip()</code>. Used well, they make loops shorter, safer, and considerably easier to read.</p>
<section id="enumerate-automatic-index-tracking" class="level2">
<h2 class="anchored" data-anchor-id="enumerate-automatic-index-tracking"><code>enumerate()</code>: Automatic Index Tracking</h2>
<p>The <a href="https://docs.python.org/3/library/functions.html#enumerate">official Python documentation</a> defines <code>enumerate()</code> as:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(iterable, start<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span></code></pre></div></div>
<p>It takes any iterable and attaches a counter to each element, returning <code>(index, value)</code> tuples on each iteration.</p>
<section id="using-it-in-a-loop" class="level3">
<h3 class="anchored" data-anchor-id="using-it-in-a-loop">Using It in a Loop</h3>
<p>Rather than explaining it in the abstract, let us step through the behaviour directly. First, wrap <code>names</code> with <code>enumerate()</code>:</p>
<div id="763e67f2" class="cell" data-execution_count="4">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(names)</span></code></pre></div></div>
<div class="cell-output cell-output-display" data-execution_count="4">
<pre><code>&lt;enumerate at 0x10c7576a0&gt;</code></pre>
</div>
</div>
<p>Converting to a list reveals the structure:</p>
<div id="6aec76f1" class="cell" data-execution_count="5">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(names))</span></code></pre></div></div>
<div class="cell-output cell-output-display" data-execution_count="5">
<pre><code>[(0, 'Wang Daming'), (1, 'Chen Xiaomei'), (2, 'Lin Xiaohua')]</code></pre>
</div>
</div>
<p>Each element is a <code>(index, value)</code> tuple. Because the <code>enumerate</code> object itself is iterable, it can go straight into a <code>for</code> loop. <strong>Unpacking</strong> assigns the two values to separate variables in a single step:</p>
<div id="609c02f4" class="cell" data-execution_count="6">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i, name <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(names):</span>
<span id="cb11-2">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"#</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>i<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>name<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#0: Wang Daming
#1: Chen Xiaomei
#2: Lin Xiaohua</code></pre>
</div>
</div>
<p>Compared to <code>range(len(names))</code>, this version states its intent clearly: “give me both the index and the value.” There is no manual subscripting required.</p>
</section>
<section id="adjusting-the-start-value" class="level3">
<h3 class="anchored" data-anchor-id="adjusting-the-start-value">Adjusting the Start Value</h3>
<p>By default, the counter starts at <code>0</code>. Pass <code>start=1</code> to match the one-based numbering that most readers expect:</p>
<div id="0c9b1e43" class="cell" data-execution_count="7">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i, name <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(names, start<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>):</span>
<span id="cb13-2">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"#</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>i<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>name<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#1: Wang Daming
#2: Chen Xiaomei
#3: Lin Xiaohua</code></pre>
</div>
</div>
</section>
<section id="equivalent-implementation" class="level3">
<h3 class="anchored" data-anchor-id="equivalent-implementation">Equivalent Implementation</h3>
<p>The Python documentation also provides an equivalent implementation, which makes the internal logic transparent:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb15-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(iterable, start<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>):</span>
<span id="cb15-2">    n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> start               <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># counter, defaults to 0</span></span>
<span id="cb15-3">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> elem <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> iterable:</span>
<span id="cb15-4">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">yield</span> n, elem       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># produce an (index, element) pair</span></span>
<span id="cb15-5">        n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>              <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># increment the counter</span></span></code></pre></div></div>
<p>The key is the <code>yield</code> statement: on each step, the current counter and element are produced together, then the counter advances. This also explains why <code>enumerate()</code> returns a <em>lazy</em> iterator rather than a fully computed list — it generates each pair only when the loop asks for it, which keeps memory usage low regardless of the input size.</p>
</section>
</section>
<section id="zip-parallel-iteration-over-multiple-sequences" class="level2">
<h2 class="anchored" data-anchor-id="zip-parallel-iteration-over-multiple-sequences"><code>zip()</code>: Parallel Iteration Over Multiple Sequences</h2>
<p>Where <code>enumerate()</code> adds an index to a single sequence, <code>zip()</code> goes further: it lets you iterate over <strong>multiple</strong> sequences simultaneously. The <a href="https://docs.python.org/3/library/functions.html#zip">official definition</a>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb16-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">zip</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>iterables, strict<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span></code></pre></div></div>
<p>The <code>*iterables</code> signature means you can pass any number of iterables. On each iteration, <code>zip()</code> takes one element from each and bundles them into a tuple.<sup>1</sup></p>
<section id="using-it-in-a-loop-1" class="level3">
<h3 class="anchored" data-anchor-id="using-it-in-a-loop-1">Using It in a Loop</h3>
<p>Following the same approach as before, wrap <code>names</code> and <code>grades</code> with <code>zip()</code>:</p>
<div id="9843c4a1" class="cell" data-execution_count="8">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb17-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">zip</span>(names, grades)</span></code></pre></div></div>
<div class="cell-output cell-output-display" data-execution_count="8">
<pre><code>&lt;zip at 0x10c810d80&gt;</code></pre>
</div>
</div>
<p>Convert to a list to inspect the output — a sequence of <code>(name, score)</code> pairs:</p>
<div id="2270f3e7" class="cell" data-execution_count="9">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb19-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">zip</span>(names, grades))</span></code></pre></div></div>
<div class="cell-output cell-output-display" data-execution_count="9">
<pre><code>[('Wang Daming', 95), ('Chen Xiaomei', 71), ('Lin Xiaohua', 82)]</code></pre>
</div>
</div>
<p>In a <code>for</code> loop, unpack the tuple directly. No index required:</p>
<div id="aaa0405a" class="cell" data-execution_count="10">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb21-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> name, grade <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">zip</span>(names, grades):</span>
<span id="cb21-2">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Name: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>name<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  Score: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>grade<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Name: Wang Daming  Score: 95
Name: Chen Xiaomei  Score: 71
Name: Lin Xiaohua  Score: 82</code></pre>
</div>
</div>
<p>Comparing this to the <code>range(len())</code> version at the top of this article, the difference is stark. The <code>zip()</code> version reads almost like a plain English sentence.</p>
</section>
<section id="when-lengths-differ" class="level3">
<h3 class="anchored" data-anchor-id="when-lengths-differ">When Lengths Differ</h3>
<p><code>zip()</code> stops as soon as the shortest sequence is exhausted. Any remaining elements in longer sequences are silently discarded:</p>
<div id="c23d3325" class="cell" data-execution_count="11">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb23-1">names_extra <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Wang Daming"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Chen Xiaomei"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Lin Xiaohua"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Zhang Dawei"</span>]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># one extra</span></span>
<span id="cb23-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> name, grade <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">zip</span>(names_extra, grades):</span>
<span id="cb23-3">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Name: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>name<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  Score: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>grade<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>Name: Wang Daming  Score: 95
Name: Chen Xiaomei  Score: 71
Name: Lin Xiaohua  Score: 82</code></pre>
</div>
</div>
<p>Zhang Dawei never appears. Silent truncation is convenient in many cases, but it can also hide data quality issues when two sequences are supposed to be the same length. Python 3.10 introduced <code>strict=True</code> to address this: if the lengths do not match, a <code>ValueError</code> is raised immediately:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb25-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> name, grade <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">zip</span>(names_extra, grades, strict<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>):</span>
<span id="cb25-2">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Name: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>name<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  Score: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>grade<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb25-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ValueError: zip() argument 2 is shorter than argument 1</span></span></code></pre></div></div>
</section>
<section id="equivalent-implementation-1" class="level3">
<h3 class="anchored" data-anchor-id="equivalent-implementation-1">Equivalent Implementation</h3>
<p>The equivalent implementation for <code>zip()</code> is slightly more involved than <code>enumerate()</code>, but the logic is still straightforward:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb26-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">zip</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>iterables):</span>
<span id="cb26-2">    iterators <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">iter</span>(it) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> it <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> iterables]   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># convert each input to an iterator</span></span>
<span id="cb26-3">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>:</span>
<span id="cb26-4">        result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb26-5">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> it <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> iterators:</span>
<span id="cb26-6">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb26-7">                result.append(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">next</span>(it))           <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pull one element from each iterator</span></span>
<span id="cb26-8">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">StopIteration</span>:</span>
<span id="cb26-9">                <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span>                            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># stop if any iterator is exhausted</span></span>
<span id="cb26-10">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">yield</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">tuple</span>(result)                       <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># produce the combined tuple</span></span></code></pre></div></div>
<p>Each iteration calls <code>next()</code> on every iterator in turn. The moment any iterator raises <code>StopIteration</code>, the function returns — that is exactly where the “shortest sequence wins” behaviour comes from.</p>
</section>
</section>
<section id="advanced-patterns" class="level2">
<h2 class="anchored" data-anchor-id="advanced-patterns">Advanced Patterns</h2>
<p>Once you are comfortable with both functions individually, combining them opens up a few useful patterns.</p>
<section id="mixing-zip-and-enumerate" class="level3">
<h3 class="anchored" data-anchor-id="mixing-zip-and-enumerate">Mixing <code>zip</code> and <code>enumerate</code></h3>
<p>If you need an index <em>and</em> want to iterate over multiple sequences at once, wrap the <code>zip()</code> result in <code>enumerate()</code>:</p>
<div id="c2364a28" class="cell" data-execution_count="12">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb27-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i, (name, grade) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">zip</span>(names, grades), start<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>):</span>
<span id="cb27-2">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"#</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>i<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>name<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> — </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>grade<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>#1: Wang Daming — 95
#2: Chen Xiaomei — 71
#3: Lin Xiaohua — 82</code></pre>
</div>
</div>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>The inner parentheses matter
</div>
</div>
<div class="callout-body-container callout-body">
<p>In <code>for i, (name, grade) in ...</code>, the parentheses around <code>name, grade</code> are not optional. They tell Python that this position holds a tuple that needs to be unpacked further. Without them — <code>for i, name, grade in ...</code> — Python expects each element to unpack into exactly three variables, but <code>enumerate()</code> produces <code>(i, (name, grade))</code>, which is only two levels deep, so a <code>ValueError</code> is raised.</p>
</div>
</div>
</section>
<section id="unzipping-with-zip..." class="level3">
<h3 class="anchored" data-anchor-id="unzipping-with-zip...">Unzipping with <code>zip(*...)</code></h3>
<p><code>zip()</code> pairs sequences together; the same function can reverse the operation. Passing a list of tuples with <code>*</code> unpacks them so <code>zip()</code> can re-group them by position:</p>
<div id="ce3cab77" class="cell" data-execution_count="13">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb29-1">pairs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Wang Daming"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">95</span>), (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Chen Xiaomei"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">71</span>), (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Lin Xiaohua"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">82</span>)]</span>
<span id="cb29-2"></span>
<span id="cb29-3">names_unzipped, grades_unzipped <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">zip</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>pairs)</span>
<span id="cb29-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(names_unzipped)</span>
<span id="cb29-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(grades_unzipped)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>('Wang Daming', 'Chen Xiaomei', 'Lin Xiaohua')
(95, 71, 82)</code></pre>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Note</span>The result is a tuple, not a list
</div>
</div>
<div class="callout-body-container callout-body">
<p><code>zip(*pairs)</code> returns tuples. If you need lists, wrap each with <code>list()</code>.</p>
</div>
</div>
</section>
<section id="building-a-dictionary-in-one-line" class="level3">
<h3 class="anchored" data-anchor-id="building-a-dictionary-in-one-line">Building a Dictionary in One Line</h3>
<p>Combining <code>zip()</code> with <code>dict()</code> is a common Python idiom for creating a key–value mapping from two parallel sequences:</p>
<div id="8cca4f5b" class="cell" data-execution_count="14">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb31" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb31-1">scores_dict <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">dict</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">zip</span>(names, grades))</span>
<span id="cb31-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(scores_dict)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>{'Wang Daming': 95, 'Chen Xiaomei': 71, 'Lin Xiaohua': 82}</code></pre>
</div>
</div>
<p>You will encounter this pattern frequently when reading other people’s Python code.</p>
</section>
</section>
<section id="exercises" class="level2">
<h2 class="anchored" data-anchor-id="exercises">Exercises</h2>
<div class="exercises">
<section id="question-1" class="level3 exercise" data-answer="C">
<h3 class="anchored" data-anchor-id="question-1">Question 1</h3>
<p>What does <code>zip(["a", "b", "c"], [1, 2])</code> produce?</p>
<div class="options">
<ul>
<li>A. <code>[("a", 1), ("b", 2), ("c", None)]</code></li>
<li>B. A <code>ValueError</code> is raised</li>
<li>C. <code>[("a", 1), ("b", 2)]</code></li>
<li>D. <code>[("a", 1), ("b", 2), ("c",)]</code></li>
</ul>
</div>
<div class="answer">
<p><strong>Answer: C</strong></p>
<p><code>zip()</code> stops at the shortest sequence. The element <code>"c"</code> has no matching value and is silently dropped. To raise a <code>ValueError</code> when lengths differ, pass <code>strict=True</code> (Python 3.10+).</p>
</div>
</section>
<section id="question-2" class="level3 exercise" data-answer="B">
<h3 class="anchored" data-anchor-id="question-2">Question 2</h3>
<p>Which of the following correctly prints an index (starting from 1), a name, and a score on each line?</p>
<div class="options">
<ul>
<li>A. <code>for i, name, grade in enumerate(zip(names, grades), start=1):</code></li>
<li>B. <code>for i, (name, grade) in enumerate(zip(names, grades), start=1):</code></li>
<li>C. <code>for i, name, grade in zip(enumerate(names), grades):</code></li>
<li>D. <code>for i, (name, grade) in zip(enumerate(names, start=1), grades):</code></li>
</ul>
</div>
<div class="answer">
<p><strong>Answer: B</strong></p>
<p><code>enumerate(zip(names, grades), start=1)</code> produces <code>(i, (name, grade))</code> on each step. The inner parentheses in <code>for i, (name, grade)</code> tell Python to unpack that nested tuple. Option A is missing the inner parentheses and will raise a <code>ValueError</code>; options C and D have incorrect structure.</p>
</div>
</section>
<section id="question-3" class="level3 exercise" data-answer="C">
<h3 class="anchored" data-anchor-id="question-3">Question 3</h3>
<p><code>dict(zip(keys, values))</code> is equivalent to which of the following?</p>
<div class="options">
<ul>
<li>A. <code>{k: v for k, v in zip(keys, values)}</code></li>
<li>B. <code>{keys[i]: values[i] for i in range(len(keys))}</code></li>
<li>C. Both A and B are equivalent</li>
<li>D. Neither A nor B is equivalent</li>
</ul>
</div>
<div class="answer">
<p><strong>Answer: C</strong></p>
<p>All three produce the same dictionary. The differences are stylistic: <code>dict(zip(...))</code> is the most concise; the dict comprehension is the most flexible (conditions can be added); <code>range(len())</code> is the most verbose and least Pythonic.</p>
</div>
</section>
<section id="question-4" class="level3 exercise">
<h3 class="anchored" data-anchor-id="question-4">Question 4</h3>
<p>Given the list of tuples below, use <code>zip(*...)</code> to unzip it into two separate lists stored in <code>cities</code> and <code>populations</code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb33" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb33-1">data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Taipei"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2600000</span>), (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Taichung"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2800000</span>), (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Kaohsiung"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2700000</span>)]</span></code></pre></div></div>
<div class="answer">
<div id="2e55dd13" class="cell" data-execution_count="15">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb34" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb34-1">data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Taipei"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2600000</span>), (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Taichung"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2800000</span>), (<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Kaohsiung"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2700000</span>)]</span>
<span id="cb34-2"></span>
<span id="cb34-3">cities, populations <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">zip</span>(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>data)</span>
<span id="cb34-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>(cities))</span>
<span id="cb34-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>(populations))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>['Taipei', 'Taichung', 'Kaohsiung']
[2600000, 2800000, 2700000]</code></pre>
</div>
</div>
<p><code>zip(*data)</code> expands <code>data</code> into <code>zip(("Taipei", 2600000), ("Taichung", 2800000), ("Kaohsiung", 2700000))</code>. <code>zip()</code> then groups the first elements together and the second elements together, effectively transposing the structure.</p>
</div>
</section>
</div>
</section>
<section id="chapter-summary" class="level2">
<h2 class="anchored" data-anchor-id="chapter-summary">Chapter Summary</h2>
<p>This article started from the limitations of <code>range(len())</code> and introduced two built-in functions that handle the common loop patterns more cleanly.</p>
<section id="enumerate" class="level3">
<h3 class="anchored" data-anchor-id="enumerate"><code>enumerate()</code></h3>
<ul>
<li>Attaches an automatic index to each element, returning <code>(index, value)</code> tuples</li>
<li>The <code>start</code> parameter adjusts the initial counter value</li>
<li>Returns a lazy iterator: elements are produced on demand, not all at once</li>
</ul>
</section>
<section id="zip" class="level3">
<h3 class="anchored" data-anchor-id="zip"><code>zip()</code></h3>
<ul>
<li>Iterates over multiple sequences in parallel, producing one tuple per step</li>
<li>Stops at the shortest sequence by default; use <code>strict=True</code> (Python 3.10+) to raise an error on length mismatch</li>
<li>Works in reverse with <code>zip(*pairs)</code> to unzip a list of tuples</li>
<li>Combines with <code>dict()</code> to build a mapping from two sequences in one line</li>
</ul>
</section>
<section id="combining-both" class="level3">
<h3 class="anchored" data-anchor-id="combining-both">Combining Both</h3>
<p>Wrapping a <code>zip()</code> result in <code>enumerate()</code> gives you indices and multiple values at once. Remember the inner parentheses: <code>for i, (a, b) in enumerate(zip(...))</code>.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Which one should you use?
</div>
</div>
<div class="callout-body-container callout-body">
<table class="caption-top table">
<thead>
<tr class="header">
<th>Situation</th>
<th>Recommended</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Need an index while iterating</td>
<td><code>enumerate()</code></td>
</tr>
<tr class="even">
<td>Need to iterate two or more sequences together</td>
<td><code>zip()</code></td>
</tr>
<tr class="odd">
<td>Need both</td>
<td><code>enumerate(zip(...))</code></td>
</tr>
<tr class="even">
<td>Need to reverse a list of pairs</td>
<td><code>zip(*pairs)</code></td>
</tr>
</tbody>
</table>
</div>
</div>


</section>
</section>


<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>The name comes from the everyday meaning of <em>zip</em> — a fastener that interlocks two rows of teeth one pair at a time. <code>zip()</code> does the same thing with sequences: it pairs up elements from each input, one position at a time.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>Python</category>
  <category>Programming Languages</category>
  <guid>https://yueswater.com/articles/posts/python/zip-enum/python-zip-enum-en.html</guid>
  <pubDate>Sat, 09 May 2026 00:00:00 GMT</pubDate>
  <media:content url="https://yueswater.com/articles/posts/cover/Python_logo.png" medium="image" type="image/png" height="81" width="144"/>
</item>
<item>
  <title>LaTeX Tutorial Series: Basic Settings</title>
  <dc:creator>Anthony </dc:creator>
  <link>https://yueswater.com/articles/posts/latex/basic-setting/latex-basic-setting-en.html</link>
  <description><![CDATA[ 




<p>In the previous article, <a href="../../../../articles/posts/latex/first-doc/latex-first-doc.html">Your First LaTeX Document</a>, we created a document that compiles successfully and learned about the basic structure of a document class, preamble, and body. However, a document that “runs” is still quite far from one that “looks good” — the default LaTeX output has noticeable limitations in terms of fonts, Chinese support, and page margins.</p>
<p>In this article, you will learn how to use a few common packages and settings to make your document more practical:</p>
<ul>
<li>Setting the page layout (margins and paper size)</li>
<li>Loading Chinese language support</li>
<li>Adjusting fonts and line spacing</li>
<li>Getting familiar with some essential packages</li>
<li>Setting up headers and footers</li>
</ul>
<section id="the-tower-of-babel-in-the-tex-world" class="level2">
<h2 class="anchored" data-anchor-id="the-tower-of-babel-in-the-tex-world">The Tower of Babel in the TeX World</h2>
<p>Like many programming languages, TeX was invented by people in the Western world, so it is not very friendly to users from East Asian countries by default — much like the Tower of Babel in the Bible, where each language group is separated from the others. As computers became more widely used, demand from non-English users grew, and engineers developed different compilation systems that support various languages.</p>
<table class="caption-top table">
<caption>Different TeX compilation systems</caption>
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th>Compilation System</th>
<th>Advantages</th>
<th>Disadvantages</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>pdfLaTeX</td>
<td>Stable and widely supported, produces PDF directly</td>
<td>Poor support for non-Latin scripts and OpenType fonts</td>
</tr>
<tr class="even">
<td>XeLaTeX</td>
<td>Supports Unicode and OpenType fonts, works with multiple languages</td>
<td>Compilation may be slower</td>
</tr>
<tr class="odd">
<td>LuaLaTeX</td>
<td>Supports Unicode and OpenType, with powerful Lua extensions</td>
<td>Compilation may be slower</td>
</tr>
<tr class="even">
<td>LaTeX</td>
<td>Can produce DVI output, highly flexible</td>
<td>Requires extra steps to convert DVI to PS or PDF</td>
</tr>
<tr class="odd">
<td>TeX</td>
<td>Highly flexible and controllable</td>
<td>More complex syntax, less convenient than LaTeX</td>
</tr>
<tr class="even">
<td>ConTeXt</td>
<td>Good for multilingual and complex typesetting, actively developed</td>
<td>Less compatible with LaTeX, requires learning new syntax</td>
</tr>
<tr class="odd">
<td>OpTeX</td>
<td>Simple and practical, modern syntax design</td>
<td>Smaller user community, fewer resources available</td>
</tr>
<tr class="even">
<td>AMS-TeX</td>
<td>Excellent math typesetting, designed specifically for mathematicians</td>
<td>More limited in scope compared to LaTeX</td>
</tr>
</tbody>
</table>
<p>If you have been following the examples in the previous articles, you may have noticed that they were all written in English. What if your document is in Chinese? If you have been thinking about this, you are ahead of many beginners — the first section of this article will explain how to set up Chinese fonts.</p>
</section>
<section id="packages" class="level2">
<h2 class="anchored" data-anchor-id="packages">Packages</h2>
<p>If you have played online role-playing games, you probably know that you need equipment to unlock certain skills or abilities. LaTeX works in a similar way — you need packages to access specific features, such as the Chinese support we are about to discuss. This section will show you how to install and use packages, and where to look for help when you are not sure how to configure one.</p>
<section id="installing-and-using-packages" class="level3">
<h3 class="anchored" data-anchor-id="installing-and-using-packages">Installing and Using Packages</h3>
<p>To use a LaTeX package, you use the <code>\usepackage[options]{package-name}</code> command in the preamble to tell the compiler which package you need.<sup>1</sup> If you are running LaTeX locally (with a compiler already installed), the compiler will usually ask whether you want to install a missing package before it can be used.<sup>2</sup> Make sure the package is actually installed before trying to compile.</p>
<p>At the end of the previous article, you may have noticed some unfamiliar non-English characters in the example. That text is the well-known <strong><a href="https://en.wikipedia.org/wiki/Lorem_ipsum">Lorem ipsum</a></strong> (often called Lipsum) — a piece of placeholder text derived from a work by the ancient Roman writer Cicero. It is commonly used in design and typesetting to fill space and test layout appearance. The following examples use the <code>lipsum</code> and <code>blindtext</code> packages to demonstrate this.<sup>3</sup></p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode latex code-with-copy"><code class="sourceCode latex"><span id="cb1-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\documentclass</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">article</span>}</span>
<span id="cb1-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\usepackage</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">lipsum</span>}</span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\begin</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">document</span>}</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\lipsum</span>[1-2]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% generates paragraphs 1 to 2 of Lorem ipsum</span></span>
<span id="cb1-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\end</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">document</span>}</span>
<span id="cb1-7"></span>
<span id="cb1-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\documentclass</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">article</span>}</span>
<span id="cb1-9"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\usepackage</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">blindtext</span>}</span>
<span id="cb1-10"></span>
<span id="cb1-11"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\begin</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">document</span>}</span>
<span id="cb1-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\blinddocument</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% generates a sample document with headings, paragraphs, lists, etc.</span></span>
<span id="cb1-13"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\end</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">document</span>}</span></code></pre></div></div>
</section>
<section id="the-manual-ctan" class="level3">
<h3 class="anchored" data-anchor-id="the-manual-ctan">The Manual: CTAN</h3>
<p>If you buy something but do not know how to use it, the item is not very useful to you. That is why manufacturers provide user manuals — to clearly explain how to operate the product and how to deal with common problems.</p>
<p>LaTeX packages each have their own documentation, and these are stored in a repository called <a href="https://ctan.org/?lang=en">CTAN</a>.<sup>4</sup> For example, to look up the documentation for the <code>lipsum</code> package, search for “CTAN lipsum” in your browser. The first result is usually the correct one. Clicking into it will show the package page on CTAN, where the documentation is typically listed under “Package documentation.”</p>
</section>
</section>
<section id="text-settings" class="level2">
<h2 class="anchored" data-anchor-id="text-settings">Text Settings</h2>
<p>This is the section most readers have been waiting for. Before we begin, I should mention that because this tutorial is connected to TeXLive.net, it is not possible to upload local font files or images to TeXLive. The examples below are therefore adapted from those available on the TeXLive.net website.</p>
<section id="font-size" class="level3">
<h3 class="anchored" data-anchor-id="font-size">Font Size</h3>
<p>In LaTeX, there are two main ways to change font size: using predefined size commands, or using the <code>\selectfont</code> command. The predefined commands are simpler and easier to use:</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th style="text-align: center;">Command</th>
<th style="text-align: center;">Size (pt)</th>
<th style="text-align: center;">Command</th>
<th style="text-align: center;">Size (pt)</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: center;"><code>\tiny</code></td>
<td style="text-align: center;">5</td>
<td style="text-align: center;"><code>\large</code></td>
<td style="text-align: center;">12</td>
</tr>
<tr class="even">
<td style="text-align: center;"><code>\scriptsize</code></td>
<td style="text-align: center;">7</td>
<td style="text-align: center;"><code>\Large</code></td>
<td style="text-align: center;">14</td>
</tr>
<tr class="odd">
<td style="text-align: center;"><code>\footnotesize</code></td>
<td style="text-align: center;">8</td>
<td style="text-align: center;"><code>\LARGE</code></td>
<td style="text-align: center;">17</td>
</tr>
<tr class="even">
<td style="text-align: center;"><code>\small</code></td>
<td style="text-align: center;">9</td>
<td style="text-align: center;"><code>\huge</code></td>
<td style="text-align: center;">20</td>
</tr>
<tr class="odd">
<td style="text-align: center;"><code>\normalsize</code></td>
<td style="text-align: center;">10</td>
<td style="text-align: center;"><code>\Huge</code></td>
<td style="text-align: center;">25</td>
</tr>
</tbody>
</table>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode latex code-with-copy"><code class="sourceCode latex"><span id="cb2-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\documentclass</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">article</span>}</span>
<span id="cb2-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\begin</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">document</span>}</span>
<span id="cb2-3"></span>
<span id="cb2-4">This is <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\tiny</span> tiny text.</span>
<span id="cb2-5">This is <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\large</span> large text.</span>
<span id="cb2-6">This is <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\Huge</span> the largest text.</span>
<span id="cb2-7"></span>
<span id="cb2-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\end</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">document</span>}</span></code></pre></div></div>
<p>The second approach is to use <code>\fontsize{size}{baselineskip}\selectfont</code>. Here, <code>size</code> is the font size and <code>baselineskip</code> is the line spacing. For example, to set the font to 25pt with a 14pt line spacing, you would write <code>{\fontsize{25}{14}\selectfont text to resize}</code>.</p>
<p>Notice the curly braces <code>{}</code> — they create a <strong>local scope</strong>, so the settings inside only affect the content within the braces and do not change anything outside.</p>
</section>
<section id="font-families" class="level3">
<h3 class="anchored" data-anchor-id="font-families">Font Families</h3>
<p>The table below shows how to set the three main font families in LaTeX: serif, sans serif, and typewriter (monospace). Each has a corresponding command and a shorthand alternative:</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th style="text-align: center;">Font Family</th>
<th style="text-align: center;">Command</th>
<th style="text-align: center;">Alternative</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: center;">serif (roman)</td>
<td style="text-align: center;"><code>\textrm{Sample Text 0123}</code></td>
<td style="text-align: center;"><code>\rmfamily</code></td>
</tr>
<tr class="even">
<td style="text-align: center;">sans serif</td>
<td style="text-align: center;"><code>\textsf{Sample Text 0123}</code></td>
<td style="text-align: center;"><code>\sffamily</code></td>
</tr>
<tr class="odd">
<td style="text-align: center;">typewriter (monospace)</td>
<td style="text-align: center;"><code>\texttt{Sample Text 0123}</code></td>
<td style="text-align: center;"><code>\ttfamily</code></td>
</tr>
</tbody>
</table>
<p>In Word, you select a block of text and choose a font from a dropdown menu. This is straightforward but inconvenient — if the font file is not installed on a particular computer, it will fall back to a default font. LaTeX handles this more cleanly with the <code>fontspec</code> package, which provides a reliable way to set and change fonts, especially when compiling with XeLaTeX or LuaLaTeX.</p>
<p>To use it, load <code>fontspec</code> in the preamble, then use <code>\setmainfont</code>, <code>\setsansfont</code>, and <code>\setmonofont</code> to specify your fonts. In general, font files should be stored in a <code>fonts</code> subfolder with the path specified, so the compiler knows where to find them.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode latex code-with-copy"><code class="sourceCode latex"><span id="cb3-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\documentclass</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">article</span>}</span>
<span id="cb3-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\usepackage</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">fontspec</span>}</span>
<span id="cb3-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\setmainfont</span>{Times New Roman}</span>
<span id="cb3-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\setsansfont</span>{Arial}</span>
<span id="cb3-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\setmonofont</span>{Courier New}</span>
<span id="cb3-6"></span>
<span id="cb3-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\begin</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">document</span>}</span>
<span id="cb3-8">This is Serif.</span>
<span id="cb3-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\textsf</span>{This is Sans Serif.}</span>
<span id="cb3-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\texttt</span>{This is Mono.}</span>
<span id="cb3-11"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\end</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">document</span>}</span></code></pre></div></div>
</section>
<section id="font-styles" class="level3">
<h3 class="anchored" data-anchor-id="font-styles">Font Styles</h3>
<p>In LaTeX, bold, italic, and bold italic each serve a different purpose. <strong>Bold</strong> is commonly used to emphasize important words, headings, or subheadings. <em>Italic</em> is typically used for book titles, article names, or foreign-language terms. <strong><em>Bold italic</em></strong> is used when even stronger visual emphasis is needed.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode latex code-with-copy"><code class="sourceCode latex"><span id="cb4-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\documentclass</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">article</span>}</span>
<span id="cb4-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\begin</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">document</span>}</span>
<span id="cb4-3">This is normal text.</span>
<span id="cb4-4">This is <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\textbf</span>{bold} text.</span>
<span id="cb4-5">This is <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\textit</span>{italic} text.</span>
<span id="cb4-6">This is <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\textbf</span>{<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\textit</span>{bold italic}} text.</span>
<span id="cb4-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\end</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">document</span>}</span></code></pre></div></div>
</section>
<section id="setting-up-chinese" class="level3">
<h3 class="anchored" data-anchor-id="setting-up-chinese">Setting Up Chinese</h3>
<p>To use Chinese in LaTeX, you need a special package to handle font rendering and character encoding. The most common choice is <code>xeCJK</code>, which requires the XeLaTeX compiler.<sup>5</sup> First, make sure XeLaTeX is installed on your system. Then load both <code>xeCJK</code> and <code>fontspec</code> in your preamble — <code>xeCJK</code> handles the Chinese characters, while <code>fontspec</code> manages the font settings. Here is a minimal example:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode latex code-with-copy"><code class="sourceCode latex"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% !TeX xelatex</span></span>
<span id="cb5-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\documentclass</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">article</span>}</span>
<span id="cb5-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\usepackage</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">fontspec</span>}</span>
<span id="cb5-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\setmainfont</span>{FandolSong}</span>
<span id="cb5-5"></span>
<span id="cb5-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\begin</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">document</span>}</span>
<span id="cb5-7">你好，這是一份測試文件。</span>
<span id="cb5-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\end</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">document</span>}</span></code></pre></div></div>
<p>For local setups where you want to use your own font files, create a <code>fonts</code> folder in the same directory as your <code>.tex</code> file and place your Chinese font files there (<code>.ttf</code>, <code>.TTF</code>, or <code>.otf</code> formats are all supported). The <a href="https://github.com/l10n-tw/cwtex-q-fonts">cwTeX fonts</a> are a recommended option. Here is how to set them up:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode latex code-with-copy"><code class="sourceCode latex"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% !TeX xelatex</span></span>
<span id="cb6-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\documentclass</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">article</span>}</span>
<span id="cb6-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\usepackage</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">xeCJK</span>}</span>
<span id="cb6-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\usepackage</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">fontspec</span>}</span>
<span id="cb6-5"></span>
<span id="cb6-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% Set Chinese fonts</span></span>
<span id="cb6-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\setCJKmainfont</span>[Path=./fonts/]{cwTeXQMing-Medium.ttf}</span>
<span id="cb6-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\setCJKfamilyfont</span>{kai}[Path=./fonts/]{cwTeXQKai-Medium.ttf}</span>
<span id="cb6-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\newcommand</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">\kai</span>}[1]{{<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\CJKfamily</span>{kai}#1}}</span>
<span id="cb6-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\setCJKfamilyfont</span>{hei}[Path=./fonts/]{cwTeXQHei-Bold.ttf}</span>
<span id="cb6-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\newcommand</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">\hei</span>}[1]{{<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\CJKfamily</span>{hei}#1}}</span>
<span id="cb6-12"></span>
<span id="cb6-13"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\begin</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">document</span>}</span>
<span id="cb6-14">你好，這是一份測試文件。</span>
<span id="cb6-15"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\end</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">document</span>}</span></code></pre></div></div>
<p>In this example, <code>\setCJKmainfont</code> sets the <strong>default</strong> Chinese font — the one that appears when no other font is specified. <code>\setCJKfamilyfont</code> registers a named font family, such as <code>kai</code> for the Kai (regular script) style. The <code>Path=./fonts/</code> option tells the compiler where to look for the file, and the filename comes last.</p>
<p>Registering a font family is not enough on its own — you also need a command to invoke it. This is done with <code>\newcommand</code>. The syntax is <code>\newcommand{command name}[number of arguments]{definition}</code>.<sup>6</sup> In the example above, <code>\newcommand{\kai}[1]{{\CJKfamily{kai}#1}}</code> creates a <code>\kai</code> command that wraps any text you pass to it in the Kai font. The same pattern applies to <code>\hei</code> and any other font families you register.</p>
</section>
</section>
<section id="page-settings" class="level2">
<h2 class="anchored" data-anchor-id="page-settings">Page Settings</h2>
<p>With the text configuration covered, we can now move on to other parts of the document. This section will show you how to adjust margins, section heading styles, and headers and footers.</p>
<section id="margin-settings" class="level3">
<h3 class="anchored" data-anchor-id="margin-settings">Margin Settings</h3>
<p>Just like in Word, LaTeX allows you to set the top, bottom, left, and right margins of a page. The <code>geometry</code> package makes this straightforward. After loading the package, use <code>\geometry{}</code> to configure the settings. Here is an example that sets top and bottom margins to 2 cm and left and right margins to 2.5 cm:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode latex code-with-copy"><code class="sourceCode latex"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% !TeX xelatex</span></span>
<span id="cb7-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\documentclass</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">article</span>}</span>
<span id="cb7-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\usepackage</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">fontspec</span>}</span>
<span id="cb7-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\setmainfont</span>{FandolSong}</span>
<span id="cb7-5"></span>
<span id="cb7-6"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\usepackage</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">geometry</span>}</span>
<span id="cb7-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\geometry</span>{a4paper, margin=2.5cm}</span>
<span id="cb7-8"></span>
<span id="cb7-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\title</span>{My First <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\LaTeX\ </span>Document}</span>
<span id="cb7-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\author</span>{Author Name}</span>
<span id="cb7-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\date</span>{<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\today</span>}</span>
<span id="cb7-12"></span>
<span id="cb7-13"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\begin</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">document</span>}</span>
<span id="cb7-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\maketitle</span></span>
<span id="cb7-15">Hello, this is a test document.</span>
<span id="cb7-16"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\end</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">document</span>}</span></code></pre></div></div>
<p>To set all margins to the same value, use <code>\geometry{margin=2cm}</code>. To set only one side, use <code>\geometry{left=3cm}</code> — and similarly for <code>top</code>, <code>right</code>, and <code>bottom</code>.</p>
<p>The <code>geometry</code> package also supports other options such as paper size, page orientation, and binding offset. Use <code>a4paper</code>, <code>letterpaper</code>, and similar options for paper size, or add <code>landscape</code> to switch to horizontal orientation.<sup>7</sup> If you need extra space on the binding side, use the <code>bindingoffset</code> parameter. For example, an A3 landscape document with 2.5 cm margins and a 1 cm binding offset would look like this:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode latex code-with-copy"><code class="sourceCode latex"><span id="cb8-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\usepackage</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">geometry</span>}</span>
<span id="cb8-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\geometry</span>{a3paper, margin=2.5cm, landscape, bindingoffset=1cm}</span></code></pre></div></div>
</section>
<section id="section-heading-format" class="level3">
<h3 class="anchored" data-anchor-id="section-heading-format">Section Heading Format</h3>
<p>In Word, formatting section headings requires selecting each heading and applying a style manually.<sup>8</sup> This is inconvenient and can break easily when new sections are added.</p>
<p>In LaTeX, the <code>titlesec</code> package handles this. After loading it, use the <code>\titleformat{heading}[shape]{format}{label}{sep}{before-code}[after-code]</code> command to define the heading style. Each part of this command does the following:</p>
<ol type="1">
<li><strong>heading</strong>: The heading command you want to style, such as <code>\section</code> or <code>\subsection</code></li>
<li><strong>shape</strong>: An optional parameter for the heading shape, such as <code>block</code> or <code>display</code>. Can be omitted if not needed</li>
<li><strong>format</strong>: Sets the font, size, color, and other styling for the heading text</li>
<li><strong>label</strong>: Defines how the heading number is displayed; use <code>\thesection</code>, <code>\thesubsection</code>, etc., or leave it empty <code>{}</code> for no numbering</li>
<li><strong>sep</strong>: The horizontal space between the number and the heading text</li>
<li><strong>before-code</strong>: Commands to run before the heading text, such as inserting a rule or icon</li>
<li><strong>after-code</strong>: Optional commands to run after the heading text</li>
</ol>
<p>Here is a working example:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode latex code-with-copy"><code class="sourceCode latex"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% !TeX xelatex</span></span>
<span id="cb9-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\documentclass</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">article</span>}</span>
<span id="cb9-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\usepackage</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">fontspec</span>}</span>
<span id="cb9-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\usepackage</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">xeCJK, CJKnumb</span>}</span>
<span id="cb9-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\setmainfont</span>{FandolSong}</span>
<span id="cb9-6"></span>
<span id="cb9-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\usepackage</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">zhlipsum</span>}</span>
<span id="cb9-8"></span>
<span id="cb9-9"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\usepackage</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">titlesec</span>}</span>
<span id="cb9-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\titleformat</span>{<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\section</span>}</span>
<span id="cb9-11">  {<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\normalfont\Large\bfseries</span>}</span>
<span id="cb9-12">  {<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\CJKnumber</span>{<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\thesection</span>}、}</span>
<span id="cb9-13">  {1em}</span>
<span id="cb9-14">  {}</span>
<span id="cb9-15"></span>
<span id="cb9-16"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\usepackage</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">geometry</span>}</span>
<span id="cb9-17"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\geometry</span>{a4paper, margin=2.5cm}</span>
<span id="cb9-18"></span>
<span id="cb9-19"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\title</span>{My First <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\LaTeX\ </span>Document}</span>
<span id="cb9-20"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\author</span>{Author Name}</span>
<span id="cb9-21"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\date</span>{<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\today</span>}</span>
<span id="cb9-22"></span>
<span id="cb9-23"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\begin</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">document</span>}</span>
<span id="cb9-24"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\maketitle</span></span>
<span id="cb9-25"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\section</span>{Introduction}</span>
<span id="cb9-26"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\zhlipsum</span>[1-3]</span>
<span id="cb9-27"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\end</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">document</span>}</span></code></pre></div></div>
<p>Two things worth noting in this example:</p>
<ol type="1">
<li><code>\usepackage{xeCJK, CJKnumb}</code> loads two packages at once using a comma. If you do not need to pass any options, you can load multiple packages this way in a single line.</li>
<li>Just as <code>lipsum</code> generates English placeholder text, <code>zhlipsum</code> does the same for Chinese. The syntax is the same: <code>\zhlipsum[start-end]</code>.</li>
</ol>
</section>
<section id="header-and-footer-settings" class="level3">
<h3 class="anchored" data-anchor-id="header-and-footer-settings">Header and Footer Settings</h3>
<p>Headers and footers in Word can be complicated to configure — you need to handle odd and even pages, page numbers, header text, and more. LaTeX offers the <code>fancyhdr</code> package, which lets you define everything in the preamble so you do not need to adjust each page individually.</p>
<p>One thing to note in the example below: you will see both <code>\fancyfoot</code> and shorthand commands like <code>\cfoot</code>, <code>\lfoot</code>, and <code>\rfoot</code>. These are not the same thing. <code>\fancyfoot</code> is the full command with more configuration options; its position parameters include <code>L</code> (left), <code>C</code> (center), <code>R</code> (right), <code>E</code> (even pages), and <code>O</code> (odd pages). The shorthand commands <code>\cfoot</code>, <code>\lfoot</code>, and <code>\rfoot</code> are convenience aliases provided by <code>fancyhdr</code> for quickly setting the center, left, and right footer content.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode latex code-with-copy"><code class="sourceCode latex"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% !TeX xelatex</span></span>
<span id="cb10-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\documentclass</span>[twoside]{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">article</span>}</span>
<span id="cb10-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\usepackage</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">fontspec</span>}</span>
<span id="cb10-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\usepackage</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">xeCJK, CJKnumb</span>}</span>
<span id="cb10-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\setmainfont</span>{FandolSong}</span>
<span id="cb10-6"></span>
<span id="cb10-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\usepackage</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">zhlipsum</span>}</span>
<span id="cb10-8"></span>
<span id="cb10-9"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\usepackage</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">titlesec</span>}</span>
<span id="cb10-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\titleformat</span>{<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\section</span>}</span>
<span id="cb10-11">  {<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\normalfont\Large\bfseries</span>}</span>
<span id="cb10-12">  {<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\CJKnumber</span>{<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\thesection</span>}、}</span>
<span id="cb10-13">  {1em}</span>
<span id="cb10-14">  {}</span>
<span id="cb10-15"></span>
<span id="cb10-16"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\usepackage</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">geometry</span>}</span>
<span id="cb10-17"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\geometry</span>{a4paper, margin=2.5cm}</span>
<span id="cb10-18"></span>
<span id="cb10-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% Header and footer settings</span></span>
<span id="cb10-20"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\usepackage</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">fancyhdr</span>}</span>
<span id="cb10-21"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\pagestyle</span>{fancy}</span>
<span id="cb10-22"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\fancyhf</span>{}</span>
<span id="cb10-23"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\lhead</span>{<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\normalsize</span> Top-left text <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\\</span> Bottom-left text}</span>
<span id="cb10-24"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\rhead</span>{<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\normalsize</span> Top-right text <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\\</span> Bottom-right text}</span>
<span id="cb10-25"></span>
<span id="cb10-26"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\fancyfoot</span>[C]{}</span>
<span id="cb10-27"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\fancyfoot</span>[LE,RO]{Page~<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\thepage</span>}</span>
<span id="cb10-28"></span>
<span id="cb10-29"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\title</span>{My First <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\LaTeX\ </span>Document}</span>
<span id="cb10-30"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\author</span>{Author Name}</span>
<span id="cb10-31"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\date</span>{<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\today</span>}</span>
<span id="cb10-32"></span>
<span id="cb10-33"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\begin</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">document</span>}</span>
<span id="cb10-34"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\maketitle</span></span>
<span id="cb10-35"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\section</span>{Introduction}</span>
<span id="cb10-36"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\zhlipsum</span>[1-10]</span>
<span id="cb10-37"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\section</span>{Methodology}</span>
<span id="cb10-38"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\zhlipsum</span>[11-20]</span>
<span id="cb10-39"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\end</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">document</span>}</span></code></pre></div></div>
<p>Before using <code>fancyhdr</code>, it is recommended to call <code>\fancyhf{}</code> to clear any default header and footer settings. Then use <code>\lhead</code> and <code>\rhead</code> to set the left and right header content respectively. The <code>\\</code> inside the braces creates a line break within the header cell.</p>
<p>For the footer, <code>\fancyfoot[C]{}</code> clears the center footer, and <code>\fancyfoot[LE,RO]{Page~\thepage}</code> places the page number on the left side of even pages and the right side of odd pages. The <code>~</code> character is a non-breaking space, and <code>\thepage</code> outputs the current page number.</p>
</section>
</section>
<section id="practice-exercises" class="level2">
<h2 class="anchored" data-anchor-id="practice-exercises">Practice Exercises</h2>
<div class="exercises">
<section id="question-1" class="level3 exercise" data-answer="B">
<h3 class="anchored" data-anchor-id="question-1">Question 1</h3>
<p>Which compiler supports both Unicode and OpenType fonts, making it suitable for Chinese documents?</p>
<div class="options">
<ul>
<li>A. pdfLaTeX</li>
<li>B. XeLaTeX</li>
<li>C. TeX</li>
<li>D. AMS-TeX</li>
</ul>
</div>
<div class="answer">
<p><strong>Answer: B — XeLaTeX</strong></p>
<p>XeLaTeX natively supports Unicode and OpenType fonts and can directly use system fonts, making it the most common choice for Chinese documents. pdfLaTeX has poor support for non-Latin scripts, while TeX and AMS-TeX are not well-suited for this purpose.</p>
</div>
</section>
<section id="question-2" class="level3 exercise" data-answer="C">
<h3 class="anchored" data-anchor-id="question-2">Question 2</h3>
<p>Which package is used to set page margins and paper size?</p>
<div class="options">
<ul>
<li>A. <code>fontspec</code></li>
<li>B. <code>fancyhdr</code></li>
<li>C. <code>geometry</code></li>
<li>D. <code>titlesec</code></li>
</ul>
</div>
<div class="answer">
<p><strong>Answer: C — <code>geometry</code></strong></p>
<p>The <code>geometry</code> package handles page layout settings, including <code>margin</code>, <code>a4paper</code>, <code>landscape</code>, and <code>bindingoffset</code>. The <code>fontspec</code> package manages fonts, <code>fancyhdr</code> sets headers and footers, and <code>titlesec</code> formats section headings.</p>
</div>
</section>
<section id="question-3" class="level3 exercise" data-answer="A">
<h3 class="anchored" data-anchor-id="question-3">Question 3</h3>
<p>Which command sets the local font size to 20pt with a line spacing of 24pt?</p>
<div class="options">
<ul>
<li>A. <code>{\fontsize{20}{24}\selectfont text}</code></li>
<li>B. <code>\fontsize[20][24]{text}</code></li>
<li>C. <code>\setfontsize{20pt}{24pt}</code></li>
<li>D. <code>{\huge text}</code></li>
</ul>
</div>
<div class="answer">
<p><strong>Answer: A</strong></p>
<p><code>\fontsize{size}{baselineskip}\selectfont</code> is the correct syntax. It must be placed inside curly braces to create a local scope. While <code>\huge</code> increases font size, it cannot specify an exact value or control line spacing at the same time.</p>
</div>
</section>
<section id="question-4" class="level3 exercise">
<h3 class="anchored" data-anchor-id="question-4">Question 4</h3>
<p>Write a minimal XeLaTeX document that:</p>
<ol type="1">
<li>Loads the <code>xeCJK</code> and <code>fontspec</code> packages</li>
<li>Sets the main Chinese font to <code>FandolSong</code></li>
<li>Outputs “你好，LaTeX！” in the document body</li>
</ol>
<div class="answer">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode latex code-with-copy"><code class="sourceCode latex"><span id="cb11-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% !TeX xelatex</span></span>
<span id="cb11-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\documentclass</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">article</span>}</span>
<span id="cb11-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\usepackage</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">xeCJK</span>}</span>
<span id="cb11-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\usepackage</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">fontspec</span>}</span>
<span id="cb11-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\setmainfont</span>{FandolSong}</span>
<span id="cb11-6"></span>
<span id="cb11-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\begin</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">document</span>}</span>
<span id="cb11-8">你好，LaTeX！</span>
<span id="cb11-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\end</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">document</span>}</span></code></pre></div></div>
<p><code>% !TeX xelatex</code> is a magic comment that tells the editor to compile using XeLaTeX. <code>\usepackage{xeCJK}</code> handles Chinese character rendering, and <code>fontspec</code> combined with <code>\setmainfont</code> sets the font.</p>
</div>
</section>
<section id="question-5" class="level3 exercise">
<h3 class="anchored" data-anchor-id="question-5">Question 5</h3>
<p>Add <code>fancyhdr</code> settings in the preamble so that the footer center of every page displays a page number in the format “Page N.”</p>
<div class="answer">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode latex code-with-copy"><code class="sourceCode latex"><span id="cb12-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\usepackage</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">fancyhdr</span>}</span>
<span id="cb12-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\pagestyle</span>{fancy}</span>
<span id="cb12-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\fancyhf</span>{}</span>
<span id="cb12-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\cfoot</span>{Page~<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\thepage</span>}</span></code></pre></div></div>
<p><code>\fancyhf{}</code> clears all default content first. <code>\cfoot</code> is the shorthand command for setting the center footer. <code>\thepage</code> outputs the current page number, and <code>~</code> is a non-breaking space.</p>
</div>
</section>
</div>
</section>
<section id="chapter-summary" class="level2">
<h2 class="anchored" data-anchor-id="chapter-summary">Chapter Summary</h2>
<p>This article covered some of the most fundamental and important settings in LaTeX typesetting. We first looked at the characteristics of different compilers and confirmed that XeLaTeX is the main choice for Chinese documents. We then learned how to load packages using <code>\usepackage</code> and how to find documentation on CTAN. After that, we went through font size, font families, font styles, and Chinese font configuration. Finally, we used three packages — <code>geometry</code>, <code>titlesec</code>, and <code>fancyhdr</code> — to control margins, section heading formats, and headers and footers.</p>
<p>With these settings in place, you should now be able to produce a well-formatted and professional-looking LaTeX document in Chinese. The next article will move into mathematical typesetting, which is one of the most well-known strengths of LaTeX.</p>


</section>


<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>If you are not sure about the difference between <code>[]</code> and <code>{}</code>, please refer to the section on parameters in the introductory article.↩︎</p></li>
<li id="fn2"><p>The author only has experience with MiKTeX, so it is unclear whether other compilers behave the same way. One useful feature of MiKTeX is that it can be configured in MiKTeX Console to install missing packages automatically, without asking each time.↩︎</p></li>
<li id="fn3"><p>The main difference between <code>lipsum</code> and <code>blindtext</code> is that <code>lipsum</code> only generates Lorem ipsum text, while <code>blindtext</code> can produce test content in various formats, including headings and subheadings.↩︎</p></li>
<li id="fn4"><p>CTAN documentation is written in English, but with AI tools like ChatGPT now widely available, you can easily paste the content and have it translated.↩︎</p></li>
<li id="fn5"><p>Other options include <code>CTEX</code>, <code>CJK</code>, and <code>CJKutf8</code>, but <code>xeCJK</code> is the most widely used and easiest to set up. CJK stands for Chinese, Japanese, and Korean.↩︎</p></li>
<li id="fn6"><p>The definition of a new command describes what you want it to do. If the command accepts arguments, you reference them inside the definition using <code>#1</code>, <code>#2</code>, and so on, where <code>#1</code> refers to the first argument.↩︎</p></li>
<li id="fn7"><p>Portrait orientation is the default, so you do not need to specify it explicitly.↩︎</p></li>
<li id="fn8"><p>See the Word documentation on heading styles for more detail.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>LaTeX</category>
  <guid>https://yueswater.com/articles/posts/latex/basic-setting/latex-basic-setting-en.html</guid>
  <pubDate>Tue, 14 Apr 2026 00:00:00 GMT</pubDate>
  <media:content url="https://yueswater.com/articles/posts/cover/LaTeX_logo.png" medium="image" type="image/png" height="81" width="144"/>
</item>
<item>
  <title>LaTeX Tutorial Series: Your First LaTeX Document</title>
  <dc:creator>Anthony </dc:creator>
  <link>https://yueswater.com/articles/posts/latex/first-doc/latex-first-doc-en.html</link>
  <description><![CDATA[ 




<p>In the previous article, <a href="../../../../articles/posts/latex/intro/latex-intro-en.html">Introduction to LaTeX</a>, we covered how to choose a compiler and editor, along with the basic operations of LaTeX — entering commands, writing comments, opening environments, and setting section depth. In this article, you will learn:</p>
<ul>
<li>Setting up a LaTeX document</li>
<li>Configuring the LaTeX document environment</li>
</ul>
<section id="setting-up-a-latex-document" class="level2">
<h2 class="anchored" data-anchor-id="setting-up-a-latex-document">Setting Up a LaTeX Document</h2>
<p>A LaTeX working folder can be compared to a factory: blueprints, raw materials, and a production line — all are essential. The <code>.tex</code> file serves as the blueprint, describing the final product; fonts, images, and other assets are the raw materials used during production; the compilation process is the assembly line that arranges all the materials according to the blueprint and ultimately outputs a <code>.pdf</code> file. As for references (such as <code>.bib</code> files), they are supplementary materials that ensure the product is complete — without them, a report loses its citation foundation.</p>
<p>Before you start writing a LaTeX document, in addition to creating the <code>.tex</code> file, it is recommended to create additional folders to organise your assets by category — for example, images in <code>images/</code> and fonts in <code>fonts/</code>. This has two advantages: first, it makes management easier, as you only need to browse the subfolders to see all your assets at a glance; second, it makes it easier for LaTeX to reference these files — if assets are scattered around, paths become complicated and errors are more likely to occur.</p>
<section id="folder-setup" class="level3">
<h3 class="anchored" data-anchor-id="folder-setup">Folder Setup</h3>
<p>The following is a recommended LaTeX project structure. The subfolders are not mandatory and can be adjusted based on your needs:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">my-project/</span></span>
<span id="cb1-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">├──</span> main.tex          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Main document</span></span>
<span id="cb1-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">├──</span> references.bib    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># References</span></span>
<span id="cb1-4"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">├──</span> images/           <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Image assets</span></span>
<span id="cb1-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   ├── figure1.png</span>
<span id="cb1-6"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   └── figure2.pdf</span>
<span id="cb1-7"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">├──</span> fonts/            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Custom fonts</span></span>
<span id="cb1-8"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   └── custom.ttf</span>
<span id="cb1-9"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">└──</span> output/           <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compilation output</span></span>
<span id="cb1-10">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">└──</span> main.pdf</span></code></pre></div></div>
<p>Within this structure:</p>
<ul>
<li><p><strong><code>main.tex</code></strong>: The main document — the starting point of everything. LaTeX begins compilation here, where the document class (<code>\documentclass{}</code>), packages (<code>\usepackage{}</code>), and the overall structure are defined. For longer documents such as theses, individual chapters can be split into separate <code>.tex</code> files and imported into the main file using <code>\input{}</code> or <code>\include{}</code><sup>1</sup>.</p></li>
<li><p><strong><code>references.bib</code></strong>: A database file that stores your reference entries. Each entry records information such as the author, title, and year in a structured format. When used with BibTeX or BibLaTeX, you simply cite sources in the text using <code>\cite{}</code>, and the bibliography is automatically generated at the end of the document. Citation styles such as APA and MLA can also be applied as needed.</p></li>
<li><p><strong><code>images/</code></strong>: A centralised folder for all image assets. LaTeX supports raster images (<code>.png</code>, <code>.jpg</code>) as well as vector graphics (<code>.pdf</code>, <code>.eps</code>). Vector graphics do not lose quality when scaled and are the preferred format for academic documents<sup>2</sup>. It is recommended to use descriptive filenames (e.g., <code>fig-result-comparison.pdf</code>) for easier maintenance.</p></li>
<li><p><strong><code>fonts/</code></strong>: Stores custom font files (<code>.ttf</code>, <code>.otf</code>, etc.). This folder is only needed when compiling with XeLaTeX or LuaLaTeX, as these compilers can directly access system fonts or custom font files. If you are using the traditional pdfLaTeX, the font mechanism works differently and this folder is generally not required<sup>3</sup>.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Font Recommendations by System
</div>
</div>
<div class="callout-body-container callout-body">
<ul>
<li><strong>Windows</strong>: Fonts are typically installed in <code>C:\Windows\Fonts\</code> and can be referenced directly by name in XeLaTeX — no need to copy them into the project folder.</li>
<li><strong>macOS</strong>: Fonts are located in <code>/Library/Fonts/</code> or <code>~/Library/Fonts/</code> and can also be referenced directly by name.</li>
<li><strong>Linux</strong>: Font paths vary by distribution but are commonly found in <code>/usr/share/fonts/</code>. If fonts are not recognised, run <code>fc-cache -fv</code> to refresh the font cache.</li>
</ul>
</div>
</div></li>
<li><p><strong><code>output/</code></strong>: Stores all files generated during compilation, including the final <code>.pdf</code> and intermediate files such as <code>.aux</code> (cross-reference data), <code>.log</code> (compilation log), and <code>.toc</code> (table of contents data). Keeping all output here prevents these intermediate files from cluttering the project root directory<sup>4</sup>.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Output Configuration by System
</div>
</div>
<div class="callout-body-container callout-body">
<ul>
<li><strong>VS Code (LaTeX Workshop)</strong>: In <code>settings.json</code>, set <code>"latex-workshop.latex.outDir": "%DIR%/output"</code> to redirect all output to the <code>output/</code> folder.</li>
<li><strong>Overleaf</strong>: The cloud platform manages output automatically — no configuration needed.</li>
<li><strong>Command line</strong>: Use <code>xelatex -output-directory=output main.tex</code> to specify the output directory.</li>
</ul>
</div>
</div></li>
</ul>
</section>
<section id="viewing-the-folder-structure" class="level3">
<h3 class="anchored" data-anchor-id="viewing-the-folder-structure">Viewing the Folder Structure</h3>
<p>If you want to quickly inspect the directory structure of your LaTeX project, you can use the <code>tree</code> command. Navigate to the project folder and run:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb2-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">tree</span></span></code></pre></div></div>
<p>The terminal will display the entire folder hierarchy in a tree format, for example:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb3-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">my-project/</span></span>
<span id="cb3-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">├──</span> main.tex</span>
<span id="cb3-3"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">├──</span> references.bib</span>
<span id="cb3-4"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">├──</span> images/</span>
<span id="cb3-5"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   ├── figure1.png</span>
<span id="cb3-6"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   └── figure2.pdf</span>
<span id="cb3-7"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">├──</span> fonts/</span>
<span id="cb3-8"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">│</span>   └── custom.ttf</span>
<span id="cb3-9"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">└──</span> output/</span>
<span id="cb3-10">    <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">└──</span> main.pdf</span></code></pre></div></div>
<p>To display only directories (without listing files), add the <code>-d</code> flag:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb4-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">tree</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-d</span></span></code></pre></div></div>
<p>To limit the depth of the output — for example, showing only two levels — use the <code>-L</code> flag:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb5-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">tree</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-L</span> 2</span></code></pre></div></div>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>Installation by System
</div>
</div>
<div class="callout-body-container callout-body">
<p><code>tree</code> is pre-installed on most Linux distributions. On macOS and Windows, it needs to be installed separately:</p>
<ul>
<li><p><strong>macOS</strong>: Install via Homebrew</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb6-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">brew</span> install tree</span></code></pre></div></div></li>
<li><p><strong>Windows</strong>: Install via Scoop:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb7-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">scoop</span> install tree</span></code></pre></div></div>
<p>Or use the built-in PowerShell <code>tree</code> command<sup>5</sup></p></li>
</ul>
</div>
</div>
</section>
</section>
<section id="hands-on-practice" class="level2">
<h2 class="anchored" data-anchor-id="hands-on-practice">Hands-On Practice</h2>
<p>Now we will take the commands and techniques from the previous article, put them into action in the editor, and compile them into a <code>.tex</code> file that outputs as a <code>.pdf</code>. One thing to note: this section introduces the <code>report</code> class in advance to cover the concept of section depth<sup>6</sup>.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode latex code-with-copy"><code class="sourceCode latex"><span id="cb8-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\documentclass</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">report</span>}</span>
<span id="cb8-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\begin</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">document</span>}</span>
<span id="cb8-3"></span>
<span id="cb8-4">This is my first {<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\LaTeX</span>} typesetting example.<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\\</span></span>
<span id="cb8-5">This is my first <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\LaTeX</span>{} typesetting example.<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\\</span></span>
<span id="cb8-6">This is my first <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\LaTeX\ </span>typesetting example.<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\\</span></span>
<span id="cb8-7">I am Mr. Edward G.J. Lee, G.J. is a abbreviation of my name.<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\\</span></span>
<span id="cb8-8">I am Mr.<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\ </span>Edward G.J. Lee, G.J. is a abbreviation of my name.<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\\</span></span>
<span id="cb8-9">Please see Appendix A. We will be there soon.<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\\</span></span>
<span id="cb8-10">Please see Appendix A<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\null</span>. We will be there soon.</span>
<span id="cb8-11"></span>
<span id="cb8-12"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\end</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">document</span>}</span></code></pre></div></div>
<section id="document-class-setup" class="level3">
<h3 class="anchored" data-anchor-id="document-class-setup">Document Class Setup</h3>
<p>In LaTeX, the document class determines the overall appearance and format of the document. Different classes are suited to different purposes. The three most commonly used standard classes are <code>article</code>, <code>report</code>, and <code>book</code>.</p>
<p>Every document must begin with the <code>\documentclass</code> command to specify the class and its options, for example:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode latex code-with-copy"><code class="sourceCode latex"><span id="cb9-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\documentclass</span>[12pt, a4paper, twoside]{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">article</span>}</span></code></pre></div></div>
<p>This command sets the font size to 12pt, the paper size to A4, and enables double-sided printing. Besides the three main classes, LaTeX also supports classes such as <code>letter</code> and <code>standalone</code>, as well as options like <code>landscape</code> for a horizontal layout. For a deeper look at the differences between document classes, refer to <a href="https://texblog.org/2013/02/13/latex-documentclass-options-illustrated/">LaTeX documentclass options illustrated</a>.</p>
</section>
<section id="about-indentation" class="level3">
<h3 class="anchored" data-anchor-id="about-indentation">About Indentation</h3>
<p>In the example above, you may notice that the first line of the document is automatically indented. This happens because LaTeX treats the content before the first line break as an introductory paragraph when no chapter or section has been defined yet. There are two ways to address this:</p>
<ul>
<li>Add <code>\noindent</code> at the start of the first line to tell LaTeX not to indent that specific line. However, this only takes effect where the command is placed — other paragraphs will still follow the default indentation rules.</li>
<li>Add <code>\parindent=0pt</code> in the preamble to set the indentation to zero throughout the entire document, effectively removing all paragraph indentation.</li>
</ul>
<p>Note that if your document requires every paragraph to be indented (such as an academic report), neither of the above methods is ideal, and you will need to adjust based on your specific requirements<sup>7</sup>.</p>
</section>
<section id="the-documents-identity-card-title-page-information" class="level3">
<h3 class="anchored" data-anchor-id="the-documents-identity-card-title-page-information">The Document’s Identity Card: Title Page Information</h3>
<p>The title page is the centrepiece of the entire document — it can be thought of as the document’s “identity card.” It records the title (the document’s name), the author (who created it), and the date (the document’s “birthday,” or the moment it first said “Hello, World!”).</p>
<p>In the <code>report</code> class, the title page appears as the first page of the document. In the standard LaTeX format, it includes the title, author, date, and acknowledgements. Let us look at the following example:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode latex code-with-copy"><code class="sourceCode latex"><span id="cb10-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\documentclass</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">report</span>}</span>
<span id="cb10-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\title</span>{Report File}</span>
<span id="cb10-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\author</span>{Noname<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\thanks</span>{Thanks to the readers.}}</span>
<span id="cb10-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\date</span>{<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\today</span>}</span>
<span id="cb10-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\begin</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">document</span>}</span>
<span id="cb10-6"></span>
<span id="cb10-7">This is the first sentence in my <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\LaTeX\ </span>file.</span>
<span id="cb10-8"></span>
<span id="cb10-9"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\chapter</span>{First Chapter}</span>
<span id="cb10-10"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\section</span>{Section}</span>
<span id="cb10-11">...</span>
<span id="cb10-12"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\end</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">document</span>}</span></code></pre></div></div>
<p>After compiling, if you find that there is no title page at all, you have spotted the issue! Even after setting the title page information in the preamble, you must add <code>\maketitle</code> between <code>\begin{document}</code> and <code>\end{document}</code> for LaTeX to actually render that information in the document:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode latex code-with-copy"><code class="sourceCode latex"><span id="cb11-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\documentclass</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">report</span>}</span>
<span id="cb11-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\title</span>{Report File}</span>
<span id="cb11-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\author</span>{Noname<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\thanks</span>{Thanks to the readers.}}</span>
<span id="cb11-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\date</span>{<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\today</span>}</span>
<span id="cb11-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\begin</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">document</span>}</span>
<span id="cb11-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\maketitle</span></span>
<span id="cb11-7"></span>
<span id="cb11-8">This is the first sentence in my <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\LaTeX\ </span>file.</span>
<span id="cb11-9"></span>
<span id="cb11-10"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\chapter</span>{First Chapter}</span>
<span id="cb11-11"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\section</span>{Section}</span>
<span id="cb11-12">...</span>
<span id="cb11-13"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\end</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">document</span>}</span></code></pre></div></div>
</section>
<section id="adding-chapter-headings" class="level3">
<h3 class="anchored" data-anchor-id="adding-chapter-headings">Adding Chapter Headings</h3>
<p>Adding chapter headings in LaTeX is very straightforward — there is no need to adjust font sizes manually; you simply use the corresponding command<sup>8</sup>. In the <code>report</code> class, <code>\chapter{}</code> adds a chapter, while <code>\section{}</code> and <code>\subsection{}</code> correspond to sections and subsections respectively. By default, LaTeX uses <strong>Arabic numerals</strong> for chapter numbering. If you do not want a number, add an asterisk (<code>*</code>) after the command — for example, <code>\chapter*{}</code> or <code>\section*{}</code> — and LaTeX will omit the number and display only the heading text.</p>
<div class="latex-runner" data-engine="pdflatex"><pre class="latex-source">\documentclass{report}          % Use the report class
\begin{document}                % Document begins

\chapter{Introduction}          % Numbered chapter

\section{Background Theories}   % Section

\subsection{Purpose}            % Subsection

\chapter*{Acknowledgements}     % Unnumbered chapter

\end{document}                  % Document ends</pre></div>
<p>LaTeX automatically handles numbering, table of contents updates, and font sizing — the author only needs to focus on the content itself. The table below shows the full hierarchy of section commands:</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th style="text-align: center;">Depth</th>
<th>Command</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: center;">-1</td>
<td><code>\part{}</code></td>
<td>Part</td>
</tr>
<tr class="even">
<td style="text-align: center;">0</td>
<td><code>\chapter{}</code></td>
<td>Chapter (not available in <code>article</code> class)</td>
</tr>
<tr class="odd">
<td style="text-align: center;">1</td>
<td><code>\section{}</code></td>
<td>Section</td>
</tr>
<tr class="even">
<td style="text-align: center;">2</td>
<td><code>\subsection{}</code></td>
<td>Subsection</td>
</tr>
<tr class="odd">
<td style="text-align: center;">3</td>
<td><code>\subsubsection{}</code></td>
<td>Sub-subsection</td>
</tr>
<tr class="even">
<td style="text-align: center;">4</td>
<td><code>\paragraph{}</code></td>
<td>Paragraph</td>
</tr>
<tr class="odd">
<td style="text-align: center;">5</td>
<td><code>\subparagraph{}</code></td>
<td>Subparagraph</td>
</tr>
</tbody>
</table>
</section>
<section id="adding-an-abstract" class="level3">
<h3 class="anchored" data-anchor-id="adding-an-abstract">Adding an Abstract</h3>
<p>To add an abstract, use the <code>abstract</code> environment:</p>
<div class="latex-runner" data-engine="pdflatex"><pre class="latex-source">\documentclass{report}          % Use the report class
\title{Report File}             % Document title
% Author name; \thanks generates a footnote acknowledgement
\author{Noname\thanks{Thanks to the readers.}}
\date{\today}                   % Automatically inserts today's date
\begin{document}                % Document begins
\maketitle                      % Renders the title page

\begin{abstract}                % Abstract environment begins
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent varius neque vel
commodo aliquet. Integer ornare hendrerit ligula et ultricies...
\end{abstract}                  % Abstract environment ends

\chapter{Introduction}          % Chapter 1

\section{Background Theories}   % Section

\subsection{Purpose}            % Subsection

\chapter*{Acknowledgements}     % Unnumbered chapter

\end{document}                  % Document ends</pre></div>
<p>Text inside the <code>abstract</code> environment is automatically indented on both sides. Note that only the <code>article</code> and <code>report</code> classes support <code>abstract</code> — the <code>book</code> class cannot use this environment. Additionally, in the <code>report</code> class, the abstract appears on its own page, has no page number, and is not included in the table of contents. This may differ from the formatting requirements of some academic journals or institutions, so please keep this in mind.</p>
</section>
<section id="adding-footnotes" class="level3">
<h3 class="anchored" data-anchor-id="adding-footnotes">Adding Footnotes</h3>
<p>LaTeX supports two types of notes: <strong>footnotes</strong> and <strong>marginal notes</strong>. Footnotes are numbered using Arabic numerals by default and appear at the bottom of the page. In the <code>report</code>/<code>book</code> class, numbering resets at each new chapter; in the <code>article</code> class, numbering is continuous throughout the document. Marginal notes have no number and use the normal font size. Since marginal notes involve package configuration and page margin settings, they are not covered in this article — only footnotes are introduced here.</p>
<p>To add a footnote, simply append <code>\footnote{your footnote content}</code> after the relevant text:</p>
<div class="latex-runner" data-engine="pdflatex"><pre class="latex-source">\documentclass{report}          % Use the report class
\title{Report File}             % Document title
% Author name; \thanks generates a footnote acknowledgement
\author{Noname\thanks{Thanks to the readers.}}
\date{\today}                   % Automatically inserts today's date
\begin{document}                % Document begins
\maketitle                      % Renders the title page

\begin{abstract}                % Abstract environment begins
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
\end{abstract}                  % Abstract environment ends

\chapter{Introduction}          % Chapter 1

Proin sed lorem sed est malesuada dictum...

\section{Background Theories}   % Section

Vivamus ut consequat nisl...

\subsection{Purpose}            % Subsection

% \footnote{} inserts a footnote directly after the text
Sed sagittis quis est in mollis.\footnote{Curabitur vel tortor a lorem commodo volutpat eget nec dui.}

\chapter*{Acknowledgements}     % Unnumbered chapter

Cras elementum dui a nulla volutpat...

\end{document}                  % Document ends</pre></div>
</section>
</section>
<section id="exercises" class="level2">
<h2 class="anchored" data-anchor-id="exercises">Exercises</h2>
<div class="exercises">
<div class="exercise" data-answer="A">
<p>Which document class does <strong>not</strong> support the <code>\chapter{}</code> command?</p>
<div class="options">
<ul>
<li><code>article</code></li>
<li><code>report</code></li>
<li><code>book</code></li>
<li><code>beamer</code></li>
</ul>
</div>
<div class="answer">
<p><strong><code>article</code></strong> does not support <code>\chapter{}</code>; its highest-level sectioning command is <code>\section{}</code>. Both <code>report</code> and <code>book</code> include <code>\chapter{}</code>. <code>beamer</code> also lacks <code>\chapter{}</code>, but it is a presentation class with an entirely different structure. This is why this chapter uses <code>report</code> to demonstrate the full range of sectioning depth.</p>
</div>
</div>
<div class="exercise" data-answer="C">
<p>After setting <code>\title{}</code>, <code>\author{}</code>, and <code>\date{}</code> in the preamble, which command must appear in the document body to actually output the title page?</p>
<div class="options">
<ul>
<li><code>\begin{title}</code></li>
<li><code>\titlepage</code></li>
<li><code>\maketitle</code></li>
<li><code>\printtitle</code></li>
</ul>
</div>
<div class="answer">
<p>You must call <strong><code>\maketitle</code></strong> inside <code>\begin{document}</code> to render the title information set in the preamble. A common beginner mistake is to define <code>\title{}</code> and friends but forget <code>\maketitle</code>, resulting in a document with no title page at all.</p>
</div>
</div>
<div class="exercise" data-answer="C">
<p>The <code>abstract</code> environment is <strong>not</strong> available in which document class?</p>
<div class="options">
<ul>
<li><code>article</code></li>
<li><code>report</code></li>
<li><code>book</code></li>
<li>All three support it</li>
</ul>
</div>
<div class="answer">
<p><strong><code>book</code></strong> does not support the <code>abstract</code> environment — attempting to use it will throw an error. Both <code>article</code> and <code>report</code> support it, though with slightly different behaviour: in <code>article</code>, the abstract appears directly below the title; in <code>report</code>, it occupies its own unnumbered page and does not appear in the table of contents.</p>
</div>
</div>
<div class="exercise">
<p>Using the <code>report</code> class, write a LaTeX document that includes: a title <code>My Report</code>, an author <code>Your Name</code>, today’s date, a short abstract, and a chapter named <code>Introduction</code>.</p>
<div class="answer">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode latex code-with-copy"><code class="sourceCode latex"><span id="cb12-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\documentclass</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">report</span>}</span>
<span id="cb12-2"></span>
<span id="cb12-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\title</span>{My Report}</span>
<span id="cb12-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\author</span>{Your Name}</span>
<span id="cb12-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\date</span>{<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\today</span>}</span>
<span id="cb12-6"></span>
<span id="cb12-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\begin</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">document</span>}</span>
<span id="cb12-8"></span>
<span id="cb12-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\maketitle</span></span>
<span id="cb12-10"></span>
<span id="cb12-11"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\begin</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">abstract</span>}</span>
<span id="cb12-12">This is the abstract. It typically summarises the purpose, methods, and conclusions of the work.</span>
<span id="cb12-13"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\end</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">abstract</span>}</span>
<span id="cb12-14"></span>
<span id="cb12-15"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\chapter</span>{Introduction}</span>
<span id="cb12-16"></span>
<span id="cb12-17">This is the body of the first chapter.</span>
<span id="cb12-18"></span>
<span id="cb12-19"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\end</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">document</span>}</span></code></pre></div></div>
<p>Key points: <code>\maketitle</code> must come after <code>\begin{document}</code>; the <code>abstract</code> environment follows immediately after <code>\maketitle</code>; <code>\chapter{}</code> is only available in the <code>report</code> and <code>book</code> classes.</p>
</div>
</div>
<div class="exercise">
<p>Add a footnote to the following sentence, with the content “This is a sample footnote.”:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode latex code-with-copy"><code class="sourceCode latex"><span id="cb13-1">LaTeX is a typesetting system.</span></code></pre></div></div>
<div class="answer">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode latex code-with-copy"><code class="sourceCode latex"><span id="cb14-1">LaTeX is a typesetting system<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\footnote</span>{This is a sample footnote.}.</span></code></pre></div></div>
<p><code>\footnote{}</code> is inserted directly after the word or phrase being annotated. LaTeX automatically numbers and typesets it at the bottom of the page. Note the punctuation placement: the full stop goes <strong>after</strong> <code>\footnote{}</code>, not before it.</p>
</div>
</div>
</div>
</section>
<section id="chapter-summary" class="level2">
<h2 class="anchored" data-anchor-id="chapter-summary">Chapter Summary</h2>
<p>This chapter started with setting up the project folder and concluded with a fully structured LaTeX document. Here is a summary of the key points covered:</p>
<ul>
<li><strong>Project structure</strong>: A well-organised LaTeX project should separate <code>.tex</code> files, images, fonts, and output into their own folders. The <code>tree</code> command can be used to inspect the folder structure at any time.</li>
<li><strong>Document class</strong>: The <code>\documentclass[]{}</code> command determines the basic format of the document. <code>article</code>, <code>report</code>, and <code>book</code> are the three most commonly used classes.</li>
<li><strong>Indentation control</strong>: Use <code>\noindent</code> for a single line, or set <code>\parindent=0pt</code> in the preamble to disable indentation document-wide. Choose based on your needs.</li>
<li><strong>Title page</strong>: Set <code>\title{}</code>, <code>\author{}</code>, and <code>\date{}</code> in the preamble, then call <code>\maketitle</code> in the document body for the title page to appear.</li>
<li><strong>Chapter structure</strong>: Commands such as <code>\chapter{}</code>, <code>\section{}</code>, and <code>\subsection{}</code> let LaTeX handle numbering and layout automatically. Adding <code>*</code> removes the numbering.</li>
<li><strong>Abstract</strong>: Use the <code>abstract</code> environment — available only in the <code>article</code> and <code>report</code> classes.</li>
<li><strong>Footnotes</strong>: <code>\footnote{}</code> is inserted inline in the text, and LaTeX automatically formats it at the bottom of the page.</li>
</ul>
<p>The next article will cover LaTeX document environment configuration, including page layout, font selection, and package usage.</p>


</section>


<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p><code>\input{}</code> directly embeds the content of the target file, similar to copy-paste. <code>\include{}</code> starts the content on a new page and supports <code>\includeonly{}</code> for selective compilation, which is useful for speeding up builds in large documents.↩︎</p></li>
<li id="fn2"><p>Vector graphics are typically exported as <code>.pdf</code> files from tools such as Inkscape or Illustrator, or drawn directly in LaTeX using packages like TikZ or PGFPlots.↩︎</p></li>
<li id="fn3"><p>pdfLaTeX uses <code>.tfm</code> and <code>.pfb</code> font formats managed through packages such as <code>fontenc</code> and <code>inputenc</code>, which is a different font handling approach from XeLaTeX and LuaLaTeX.↩︎</p></li>
<li id="fn4"><p>Some editors or compilation tools output intermediate files to the root directory by default. You may need to adjust the settings manually to redirect them to <code>output/</code>.↩︎</p></li>
<li id="fn5"><p>The built-in Windows <code>tree</code> command (<code>tree /F</code>) has a slightly different format from the Unix version but can also display the full directory structure.↩︎</p></li>
<li id="fn6"><p>The reason for using the <code>report</code> class is simple: the <code>article</code> class does not have <code>\chapter</code>, which makes it impossible to fully demonstrate section depth. Therefore, <code>report</code> is used here.↩︎</p></li>
<li id="fn7"><p>Personal note: I personally dislike first-paragraph indentation and prefer the <strong>asymmetry</strong> that comes from indenting only subsequent paragraphs. That said, if your professor or supervisor has specific requirements, it is always safer to follow their instructions.↩︎</p></li>
<li id="fn8"><p>In Word, you would need to manually adjust font sizes for each heading, which can easily lead to formatting issues. If you are unfamiliar with Word’s heading styles, refer to <a href="http://tul.blog.ntu.edu.tw/archives/21623">Word Layout Tips: Styles, Hierarchy, Table of Contents, Headers &amp; Footers, Paragraphs</a>.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>LaTeX</category>
  <guid>https://yueswater.com/articles/posts/latex/first-doc/latex-first-doc-en.html</guid>
  <pubDate>Tue, 14 Apr 2026 00:00:00 GMT</pubDate>
  <media:content url="https://yueswater.com/articles/posts/cover/LaTeX_logo.png" medium="image" type="image/png" height="81" width="144"/>
</item>
<item>
  <title>Python Tutorial Series: The Nature of Variables Through id()</title>
  <dc:creator>Anthony </dc:creator>
  <link>https://yueswater.com/articles/posts/python/id-function/python-id-function-en.html</link>
  <description><![CDATA[ 




<p>At first glance, <code>id()</code> looks like a tiny utility: it just returns an integer. But once you start using it to inspect real code, you quickly realize something deeper: many bugs and “weird” behaviors come from misunderstanding what a Python variable actually is.</p>
<p>This article uses runnable examples to explain one core idea: in Python, variables are names, and objects are the real entities. Along the way, we will cover small integer caching, constant pooling, string interning, <code>is</code> vs <code>==</code>, and a few classic pitfalls that appear in production code.</p>
<section id="a-counter-intuitive-start" class="level2">
<h2 class="anchored" data-anchor-id="a-counter-intuitive-start">A Counter-Intuitive Start</h2>
<p>Let us begin with a famous example:</p>
<div id="c89dc6df" class="cell" data-execution_count="1">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1">a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span></span>
<span id="cb1-2">b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span></span>
<span id="cb1-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(a <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> b)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expected: True</span></span>
<span id="cb1-4"></span>
<span id="cb1-5">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">257</span></span>
<span id="cb1-6">y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">257</span></span>
<span id="cb1-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(x <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> y)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expected: may be True or False</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>True
False</code></pre>
</div>
</div>
<p>Many developers see this and think Python is inconsistent. It is not. The behavior is usually consistent <em>within a given implementation and execution context</em>, but the result can vary across contexts (script vs REPL) and across implementations.</p>
<p>To understand why, we first need to be precise about what <code>id()</code> means.</p>
</section>
<section id="what-id-actually-means" class="level2">
<h2 class="anchored" data-anchor-id="what-id-actually-means">What <code>id()</code> Actually Means</h2>
<p>According to the official Python docs, <code>id(object)</code> returns an integer that is unique and constant <strong>during the object’s lifetime</strong>.</p>
<p>That statement has two important parts:</p>
<ol type="1">
<li>As long as an object is alive, its <code>id()</code> does not change.</li>
<li>Two different live objects cannot share the same <code>id()</code>.</li>
</ol>
<p>In CPython (the most widely used implementation), this value is often the memory address of the object.</p>
<div id="44c34da3" class="cell" data-execution_count="2">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1">s <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"python"</span></span>
<span id="cb3-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span>(s))             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expected: an integer</span></span>
<span id="cb3-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span>(s) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span>(s))    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expected: True</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>4368988176
True</code></pre>
</div>
</div>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Warning
</div>
</div>
<div class="callout-body-container callout-body">
<p>In CPython, <code>id()</code> is often the memory address, but this is an implementation detail, not a language-level guarantee. PyPy, Jython, and other implementations may use different strategies.</p>
</div>
</div>
<p>So <code>id()</code> is useful for debugging and learning internals, but you should not rely on CPython-specific behavior as business logic.</p>
</section>
<section id="lifetime-unique-not-globally-unique" class="level2">
<h2 class="anchored" data-anchor-id="lifetime-unique-not-globally-unique">Lifetime-Unique, Not Globally Unique</h2>
<p>A common misunderstanding is: “If <code>id()</code> is unique, it should never repeat.” That is incorrect.</p>
<p>The docs say unique <strong>during the object’s lifetime</strong>. After an object is destroyed, its old <code>id()</code> may be reused.</p>
<div id="81273c1e" class="cell" data-execution_count="3">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">object</span>()))</span>
<span id="cb5-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">object</span>()))</span>
<span id="cb5-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">object</span>()))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># May repeat previous values</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>4500198144
4500198400
4500198144</code></pre>
</div>
</div>
<p>These temporary objects are created and discarded immediately, so CPython may reuse the same memory slot.</p>
<p>If you want to compare identities safely, keep references alive:</p>
<div id="fb29d340" class="cell" data-execution_count="4">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1">a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">object</span>()</span>
<span id="cb7-2">b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">object</span>()</span>
<span id="cb7-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span>(a), <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span>(b))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expected: two different integers</span></span>
<span id="cb7-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(a <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> b)        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expected: False</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>4500198496 4500198144
False</code></pre>
</div>
</div>
</section>
<section id="small-integer-cache" class="level2">
<h2 class="anchored" data-anchor-id="small-integer-cache">Small Integer Cache</h2>
<p>Now back to <code>256</code> vs <code>257</code>.</p>
<p>CPython pre-allocates a range of small integer objects and reuses them. In many tutorials you will see <code>-5..256</code> as the typical range. In newer CPython branches, related constants are internal and version-dependent, so avoid treating one exact range as a language guarantee.</p>
<div id="8540dc25" class="cell" data-execution_count="5">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1">a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span></span>
<span id="cb9-2">b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">256</span></span>
<span id="cb9-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(a <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> b)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expected: usually True in CPython</span></span>
<span id="cb9-4"></span>
<span id="cb9-5">c <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span></span>
<span id="cb9-6">d <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span></span>
<span id="cb9-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(c <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> d)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expected: usually True in CPython</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>True
True</code></pre>
</div>
</div>
<p>For values outside the commonly cached range:</p>
<div id="c02d1ff5" class="cell" data-execution_count="6">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">257</span></span>
<span id="cb11-2">y <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">257</span></span>
<span id="cb11-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(x <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> y)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expected: may be True or False depending on context</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>False</code></pre>
</div>
</div>
<p>The key message is not the exact boundary. The key message is that identity behavior for literals can be affected by implementation-level reuse.</p>
</section>
<section id="constant-pooling-and-compilation-context" class="level2">
<h2 class="anchored" data-anchor-id="constant-pooling-and-compilation-context">Constant Pooling and Compilation Context</h2>
<p>If <code>257</code> is not always from the small-int cache, why is <code>x is y</code> sometimes <code>True</code> anyway?</p>
<p>Because equal literals in the same code object may be loaded from the same constant slot.</p>
<div id="c41b7362" class="cell" data-execution_count="7">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> dis</span>
<span id="cb13-2"></span>
<span id="cb13-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> f():</span>
<span id="cb13-4">    a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">257</span></span>
<span id="cb13-5">    b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">257</span></span>
<span id="cb13-6">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> a <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> b</span>
<span id="cb13-7"></span>
<span id="cb13-8">dis.dis(f)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  3           0 RESUME                   0

  4           2 LOAD_CONST               1 (257)
              4 STORE_FAST               0 (a)

  5           6 LOAD_CONST               1 (257)
              8 STORE_FAST               1 (b)

  6          10 LOAD_FAST                0 (a)
             12 LOAD_FAST                1 (b)
             14 IS_OP                    0
             16 RETURN_VALUE</code></pre>
</div>
</div>
<p>You may see both assignments load the same constant index (for example, <code>LOAD_CONST 1</code>). That means both names refer to the same constant object in that compiled unit.</p>
<p>This is also why script files and REPL input can differ:</p>
<ul>
<li>In a <code>.py</code> file, both lines are often compiled together.</li>
<li>In REPL, each input is usually a separate compile unit.</li>
</ul>
<p>So “same text” does not always mean “same compiled context.”</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>When <code>is</code> changes across contexts, it often reflects constant reuse strategy, not value semantics. For value comparison, use <code>==</code>.</p>
</div>
</div>
</section>
<section id="string-interning" class="level2">
<h2 class="anchored" data-anchor-id="string-interning">String Interning</h2>
<p>Strings can also be reused through <strong>string interning</strong>.</p>
<p>A simple way to think about it: if two strings are identical, Python may keep one shared copy in memory.</p>
<div id="c1063ed8" class="cell" data-execution_count="8">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb15-1">a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hello"</span></span>
<span id="cb15-2">b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hello"</span></span>
<span id="cb15-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(a <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> b)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expected: often True</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>True</code></pre>
</div>
</div>
<p>For strings with spaces or symbols, identity is less predictable:</p>
<div id="393d0433" class="cell" data-execution_count="9">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb17-1">s1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hello world"</span></span>
<span id="cb17-2">s2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hello world"</span></span>
<span id="cb17-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(s1 <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> s2)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expected: may be True or False</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>False</code></pre>
</div>
</div>
<p>You can force interning with <code>sys.intern()</code>:</p>
<div id="2f952c44" class="cell" data-execution_count="10">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb19-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> sys</span>
<span id="cb19-2"></span>
<span id="cb19-3">raw1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"user_id"</span></span>
<span id="cb19-4">raw2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"user_"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"id"</span></span>
<span id="cb19-5"></span>
<span id="cb19-6">i1 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sys.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">intern</span>(raw1)</span>
<span id="cb19-7">i2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sys.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">intern</span>(raw2)</span>
<span id="cb19-8"></span>
<span id="cb19-9"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(i1 <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> i2)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expected: True</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>True</code></pre>
</div>
</div>
<p>Practical use case: repeated short strings (CSV field names, status labels, protocol tokens) can consume less memory when interned carefully.</p>
<div id="35ecec7b" class="cell" data-execution_count="11">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb21-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> sys</span>
<span id="cb21-2"></span>
<span id="cb21-3">rows <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OK"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OK"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"FAIL"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"OK"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"FAIL"</span>]</span>
<span id="cb21-4">rows <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [sys.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">intern</span>(x) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> x <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> rows]</span>
<span id="cb21-5"></span>
<span id="cb21-6"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(rows[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> rows[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>])  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expected: True</span></span>
<span id="cb21-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(rows[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>] <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> rows[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>])  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expected: True</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>True
True</code></pre>
</div>
</div>
</section>
<section id="is-vs-practical-rules" class="level2">
<h2 class="anchored" data-anchor-id="is-vs-practical-rules"><code>is</code> vs <code>==</code>: Practical Rules</h2>
<p>The difference is fundamental:</p>
<ul>
<li><code>is</code> compares identity (same object)</li>
<li><code>==</code> compares value (via <code>__eq__</code>)</li>
</ul>
<div id="0a7490ee" class="cell" data-execution_count="12">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb23-1">a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb23-2">b <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb23-3"></span>
<span id="cb23-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> b)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expected: True</span></span>
<span id="cb23-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(a <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> b)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expected: False</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>True
False</code></pre>
</div>
</div>
<p>One strong rule in real projects: use <code>is None</code>, not <code>== None</code>.</p>
<div id="e26d29d9" class="cell" data-execution_count="13">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb25-1">x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb25-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(x <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expected: True</span></span>
<span id="cb25-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expected: True, but not recommended</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>True
True</code></pre>
</div>
</div>
<p>Why is <code>is None</code> better?</p>
<ol type="1">
<li><code>None</code> is a singleton, so identity is the correct semantic check.</li>
<li><code>==</code> calls <code>__eq__</code>, which may have custom behavior.</li>
</ol>
<p>For example, with NumPy arrays:</p>
<div id="29820a89" class="cell" data-execution_count="14">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb27-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> numpy <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> np</span>
<span id="cb27-2"></span>
<span id="cb27-3">arr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> np.array([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>])</span>
<span id="cb27-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(arr <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expected: array([False, False, False])</span></span>
<span id="cb27-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(arr <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expected: False</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[False False False]
False</code></pre>
</div>
</div>
<p><code>is None</code> is both safer and clearer.</p>
</section>
<section id="mutable-objects-and-identity-changes" class="level2">
<h2 class="anchored" data-anchor-id="mutable-objects-and-identity-changes">Mutable Objects and Identity Changes</h2>
<p><code>id()</code> is also a great tool to understand in-place mutation vs rebinding.</p>
<p><code>append</code> mutates a list in place; <code>+</code> creates a new list:</p>
<div id="4d68d89a" class="cell" data-execution_count="15">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb29-1">lst <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb29-2">old_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span>(lst)</span>
<span id="cb29-3"></span>
<span id="cb29-4">lst.append(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb29-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span>(lst) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> old_id)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expected: True</span></span>
<span id="cb29-6"></span>
<span id="cb29-7">lst <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> lst <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]</span>
<span id="cb29-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span>(lst) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> old_id)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expected: False</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>True
False</code></pre>
</div>
</div>
<p><code>+=</code> has type-specific behavior:</p>
<div id="267a3f03" class="cell" data-execution_count="16">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb31" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb31-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># list: usually in-place</span></span>
<span id="cb31-2">l <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>]</span>
<span id="cb31-3">l_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span>(l)</span>
<span id="cb31-4">l <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]</span>
<span id="cb31-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span>(l) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> l_id)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expected: True</span></span>
<span id="cb31-6"></span>
<span id="cb31-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># tuple: creates a new tuple and rebinds</span></span>
<span id="cb31-8">t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb31-9">t_id <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span>(t)</span>
<span id="cb31-10">t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> (<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>,)</span>
<span id="cb31-11"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span>(t) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> t_id)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expected: False</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>True
False</code></pre>
</div>
</div>
<p>Classic trap:</p>
<div id="1c10d7e9" class="cell" data-execution_count="17">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb33" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb33-1">t <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ([<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>], [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>])</span>
<span id="cb33-2"></span>
<span id="cb33-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb33-4">    t[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>]</span>
<span id="cb33-5"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">TypeError</span> <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> e:</span>
<span id="cb33-6">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">type</span>(e).<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">__name__</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expected: TypeError</span></span>
<span id="cb33-7"></span>
<span id="cb33-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(t)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expected: ([1, 2, 5], [3, 4])</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>TypeError
([1, 2, 5], [3, 4])</code></pre>
</div>
</div>
<p>Why does it mutate even though it errors?</p>
<ol type="1">
<li><code>t[0]</code> is a list, so in-place addition happens first.</li>
<li>Python then attempts to assign the result back.</li>
<li>Tuple item assignment is illegal, so a <code>TypeError</code> is raised.</li>
</ol>
<p>The mutation already happened before the exception.</p>
</section>
<section id="functions-classes-and-modules-also-have-id" class="level2">
<h2 class="anchored" data-anchor-id="functions-classes-and-modules-also-have-id">Functions, Classes, and Modules Also Have <code>id()</code></h2>
<p>Everything in Python is an object, including functions, classes, and modules.</p>
<div id="b9b14598" class="cell" data-execution_count="18">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb35" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb35-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> sys</span>
<span id="cb35-2"></span>
<span id="cb35-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> foo():</span>
<span id="cb35-4">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pass</span></span>
<span id="cb35-5"></span>
<span id="cb35-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> Bar:</span>
<span id="cb35-7">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pass</span></span>
<span id="cb35-8"></span>
<span id="cb35-9"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span>(foo))</span>
<span id="cb35-10"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span>(Bar))</span>
<span id="cb35-11"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span>(sys))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>4501764864
36446986256
4368951200</code></pre>
</div>
</div>
<p>Modules often behave like singletons due to import caching in <code>sys.modules</code>:</p>
<div id="75f4aeec" class="cell" data-execution_count="19">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb37" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb37-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> math</span>
<span id="cb37-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> math <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> m2</span>
<span id="cb37-3"></span>
<span id="cb37-4"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(math <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> m2)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expected: True</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>True</code></pre>
</div>
</div>
</section>
<section id="practical-use-and-real-pitfalls" class="level2">
<h2 class="anchored" data-anchor-id="practical-use-and-real-pitfalls">Practical Use and Real Pitfalls</h2>
<p>Understanding identity is useful, but misuse is dangerous.</p>
<p>A bad pattern is using <code>id()</code> as a long-term tracking key:</p>
<div id="3b2c8841" class="cell" data-execution_count="20">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb39" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb39-1">obj <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">object</span>()</span>
<span id="cb39-2">tracking <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">id</span>(obj): <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"alive"</span>}</span>
<span id="cb39-3"></span>
<span id="cb39-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">del</span> obj</span>
<span id="cb39-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Later, a new object may reuse the same id</span></span></code></pre></div></div>
</div>
<p>This can create false matches in long-running programs.</p>
<p>A better approach is <code>weakref</code>:</p>
<div id="79191f26" class="cell" data-execution_count="21">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb40" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb40-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> weakref</span>
<span id="cb40-2"></span>
<span id="cb40-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> Node:</span>
<span id="cb40-4">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pass</span></span>
<span id="cb40-5"></span>
<span id="cb40-6">n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Node()</span>
<span id="cb40-7">meta <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> weakref.WeakKeyDictionary()</span>
<span id="cb40-8">meta[n] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"alive"</span></span>
<span id="cb40-9"></span>
<span id="cb40-10"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(meta))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Expected: 1</span></span>
<span id="cb40-11"></span>
<span id="cb40-12"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">del</span> n</span>
<span id="cb40-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># After garbage collection, key-value can disappear automatically</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>1</code></pre>
</div>
</div>
<p>This tracks object relationships safely without relying on reusable integer identities.</p>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p><code>id()</code> helps you see Python’s object model in action: variables are names, objects hold state and behavior.</p>
<p>Once you internalize that model, many confusing behaviors become predictable: <code>is</code> vs <code>==</code>, mutable vs immutable operations, interning, constant pooling, and identity reuse.</p>
<p>At the same time, keep this boundary clear: many examples here are CPython implementation details. They are excellent for debugging and performance tuning, but they should not become hard assumptions in application logic.</p>
<p>In the next article, we will go deeper into mutable vs immutable objects, especially how they affect function arguments, default values, and side effects.</p>
</section>
<section id="references" class="level2">
<h2 class="anchored" data-anchor-id="references">References</h2>
<ul>
<li>Python Built-in Functions: <code>id()</code> <a href="https://docs.python.org/3/library/functions.html#id" class="uri">https://docs.python.org/3/library/functions.html#id</a></li>
<li>CPython source (<code>Objects/longobject.c</code>) <a href="https://github.com/python/cpython/blob/main/Objects/longobject.c" class="uri">https://github.com/python/cpython/blob/main/Objects/longobject.c</a></li>
<li>CPython internal runtime constants <a href="https://github.com/python/cpython/blob/main/Include/internal/pycore_runtime_structs.h" class="uri">https://github.com/python/cpython/blob/main/Include/internal/pycore_runtime_structs.h</a></li>
<li>Python <code>sys.intern()</code> <a href="https://docs.python.org/3/library/sys.html#sys.intern" class="uri">https://docs.python.org/3/library/sys.html#sys.intern</a></li>
<li>Python <code>weakref</code> <a href="https://docs.python.org/3/library/weakref.html" class="uri">https://docs.python.org/3/library/weakref.html</a></li>
</ul>


</section>

<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a> ]]></description>
  <category>Python</category>
  <category>Programming Languages</category>
  <guid>https://yueswater.com/articles/posts/python/id-function/python-id-function-en.html</guid>
  <pubDate>Tue, 14 Apr 2026 00:00:00 GMT</pubDate>
  <media:content url="https://yueswater.com/articles/posts/cover/Python_logo.png" medium="image" type="image/png" height="81" width="144"/>
</item>
<item>
  <title>LaTeX Tutorial Series: Introduction to LaTeX</title>
  <dc:creator>Anthony </dc:creator>
  <link>https://yueswater.com/articles/posts/latex/intro/latex-intro-en.html</link>
  <description><![CDATA[ 




<p>Many people, whether during their student years or in their professional careers, have experienced the painful process of writing reports. Those who frequently use Microsoft Word or PowerPoint may have noticed that inserting images and placing them exactly where you want is surprisingly time-consuming. On one hand, every image has a different size, requiring individual adjustment after importing. On the other hand, Word and PowerPoint are not particularly smart<sup>1</sup> when it comes to arranging text and images together.</p>
<section id="the-history-of-latex" class="level2">
<h2 class="anchored" data-anchor-id="the-history-of-latex">The History of LaTeX</h2>
<p>LaTeX is a powerful typesetting system that is widely used for producing documents in science, technology, engineering, and mathematics. Its predecessor is the <a href="https://svn.tug.org:8369/texlive/">TeX system</a>, invented by <a href="https://www-cs-faculty.stanford.edu/~knuth/">Donald Knuth</a> in 1978. Knuth, a renowned computer scientist, designed TeX himself in order to address the limitations of typesetting systems at the time, with the goal of producing high-quality documents and books.</p>
<p>However, despite its powerful capabilities, TeX was not very friendly to ordinary users — it required writing a large amount of code and configuring many settings in advance. To solve this problem, <a href="https://lamport.org/">Leslie Lamport</a> developed <a href="https://latex-project.org/">LaTeX</a> in 1984, building on top of TeX. LaTeX simplified the document writing process significantly: users can focus entirely on the content they are writing, without worrying about the specific details of typesetting. This is exactly what LaTeX is most proud of — <em>What You See Is What You Get</em>.</p>
</section>
<section id="prerequisites-compilers-and-editors" class="level2">
<h2 class="anchored" data-anchor-id="prerequisites-compilers-and-editors">Prerequisites: Compilers and Editors</h2>
<blockquote class="blockquote">
<p><em>“To do a good job, one must first sharpen one’s tools.”</em></p>
</blockquote>
<p>Just as writing has always required tools — paper and pen in the old days, computers or tablets nowadays — LaTeX also needs two essential components before you can get started: a <strong>compiler</strong> and an <strong>editor</strong><sup>2</sup>.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://yueswater.com/articles/posts/latex/intro/images/latex-file-flow.png" class="img-fluid figure-img"></p>
<figcaption>LaTeX Compilation Process<sup>3</sup></figcaption>
</figure>
</div>
<section id="compilers" class="level3">
<h3 class="anchored" data-anchor-id="compilers">Compilers</h3>
<p>Before entering the world of LaTeX, the most important step is to set up your environment. You must follow one golden rule: <strong>always install the compiler before the editor!</strong> This is because the editor is simply a place for typing — it relies on the backend compiler engine to handle all the typesetting logic. If you do it in the wrong order, you may run into issues where the editor cannot detect the compiler’s path.</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th style="text-align: left;">Operating System</th>
<th style="text-align: left;">Recommended Compiler</th>
<th style="text-align: left;">Features &amp; Advantages</th>
<th style="text-align: left;">Best For</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;"><strong>Windows</strong></td>
<td style="text-align: left;"><a href="https://miktex.org/">MiKTeX</a></td>
<td style="text-align: left;">Lightweight, with an “auto-install missing packages” feature — it only downloads what you need.</td>
<td style="text-align: left;">Beginners with limited disk space who prefer a clean interface.</td>
</tr>
<tr class="even">
<td style="text-align: left;"></td>
<td style="text-align: left;"><a href="https://tug.org/texlive/">TeX Live</a></td>
<td style="text-align: left;">The international standard, installed as a complete package at once, offering the highest compatibility and stability.</td>
<td style="text-align: left;">Power users who want a one-time setup and have sufficient disk space.</td>
</tr>
<tr class="odd">
<td style="text-align: left;"><strong>macOS</strong></td>
<td style="text-align: left;"><a href="https://www.tug.org/mactex/">MacTeX</a></td>
<td style="text-align: left;">A macOS-optimised version of TeX Live, with integrated Mac-specific font and path management tools.</td>
<td style="text-align: left;">All macOS users (the preferred choice to avoid system path conflicts).</td>
</tr>
<tr class="even">
<td style="text-align: left;"><strong>Linux</strong></td>
<td style="text-align: left;"><strong>TeX Live</strong></td>
<td style="text-align: left;">Available in the official repositories of most distributions, with tight integration into the system kernel.</td>
<td style="text-align: left;">Open-source enthusiasts and developers who need a highly customisable environment.</td>
</tr>
</tbody>
</table>
<p>If you do not want to install a large software package on your local machine, or if you need to collaborate with others, <a href="https://www.overleaf.com/">Overleaf</a> is a highly recommended option. Overleaf is a powerful web-based platform that runs entirely in your browser, with the latest TeX Live environment built in — no installation required. It is a great solution for those who prefer a lightweight approach. However, Overleaf occasionally goes under maintenance, so it is still worth learning how to set up a local compiler and editor.</p>
</section>
<section id="editors" class="level3">
<h3 class="anchored" data-anchor-id="editors">Editors</h3>
<p>Compared to compilers, where there are strict recommendations depending on your operating system, the choice of editor is much more flexible. An editor is essentially a “text input interface” — as long as it can produce a <code>.tex</code> file, even your computer’s default <strong>Notepad</strong> could technically serve as your LaTeX tool.</p>
<p>That said, choosing a professional editor with features such as syntax highlighting, auto-completion, and live preview will make your writing experience significantly more efficient.</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th style="text-align: left;">Editor</th>
<th style="text-align: left;">Features &amp; Advantages</th>
<th style="text-align: left;">Best For</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;"><a href="https://www.texstudio.org/">TeXstudio</a></td>
<td style="text-align: left;">The most feature-complete IDE available, with a built-in mathematical symbol panel and real-time syntax checking.</td>
<td style="text-align: left;">Users coming from Word who need visual tools to guide them.</td>
</tr>
<tr class="even">
<td style="text-align: left;"><a href="https://www.xm1math.net/texmaker/">TeXmaker</a></td>
<td style="text-align: left;">Clean interface, excellent cross-platform support, and very intuitive configuration — a classic choice for many experienced users.</td>
<td style="text-align: left;">Users who prefer stability and do not want overly complex features.</td>
</tr>
<tr class="odd">
<td style="text-align: left;"><a href="https://code.visualstudio.com/">Visual Studio Code</a></td>
<td style="text-align: left;">By installing the <a href="https://marketplace.visualstudio.com/items?itemName=James-Yu.latex-workshop">LaTeX Workshop</a> extension, it offers powerful code completion, jump-to-definition, and a beautiful UI.</td>
<td style="text-align: left;">Developers already using VS Code, who enjoy a modern interface and high customisability.</td>
</tr>
</tbody>
</table>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>A Quick Reminder
</div>
</div>
<div class="callout-body-container callout-body">
<p>Whichever editor you choose, please make sure your <strong>compiler</strong> (MiKTeX / TeX Live / MacTeX) is already installed and working. Only then can the editor successfully convert your code into a beautiful PDF!</p>
</div>
</div>
</section>
</section>
<section id="an-overview-of-latex-syntax" class="level2">
<h2 class="anchored" data-anchor-id="an-overview-of-latex-syntax">An Overview of LaTeX Syntax</h2>
<p>Before we dive into the syntax in detail, let us first look at a simple example to understand the basic structure of a LaTeX document:</p>
<div class="latex-runner" data-engine="pdflatex"><pre class="latex-source">\documentclass[12pt]{article}  % Define document type and font size

\title{My First Document}      % Title
\author{Author}                % Author
\date{\today}                  % Date

\begin{document}               % Body begins
\maketitle                     % Generate the title block

This is my first \LaTeX\ document!

\end{document}                 % Body ends</pre></div>
<p>As you may have already noticed, LaTeX commands always begin with a backslash <code>\</code>. Whenever you need to use a command, you must tell the LaTeX compiler by starting with <code>\</code>, otherwise the computer will simply treat it as ordinary text.</p>
<p>Besides the backslash, LaTeX also has a number of <strong>reserved characters</strong> that carry specific functions. You must use them correctly when they appear in your document:</p>
<table class="caption-top table">
<caption>Common LaTeX Special Characters</caption>
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th style="text-align: left;">Symbol</th>
<th style="text-align: left;">Function</th>
<th style="text-align: left;">Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;"><code>\</code></td>
<td style="text-align: left;">Command prefix</td>
<td style="text-align: left;">The starting point of every LaTeX command, used to invoke formatting or functionality.</td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>%</code></td>
<td style="text-align: left;">Comment</td>
<td style="text-align: left;">Any text after this symbol will be ignored by the compiler. Commonly used for writing notes.</td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>#</code></td>
<td style="text-align: left;">Parameter marker</td>
<td style="text-align: left;">Used when defining macros or custom commands to specify argument placeholders (e.g., #1, #2).</td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>~</code></td>
<td style="text-align: left;">Non-breaking space</td>
<td style="text-align: left;">Inserts a space that will not be broken by automatic line wrapping.</td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>$</code></td>
<td style="text-align: left;">Math mode toggle</td>
<td style="text-align: left;">Text enclosed between two <code>$</code> signs enters the Math Mode environment.</td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>^</code></td>
<td style="text-align: left;">Superscript</td>
<td style="text-align: left;">In math mode, raises the following character to a superscript (e.g., <img src="https://latex.codecogs.com/png.latex?x%5E2">).</td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>_</code></td>
<td style="text-align: left;">Subscript</td>
<td style="text-align: left;">In math mode, lowers the following character to a subscript (e.g., <img src="https://latex.codecogs.com/png.latex?a_n">).</td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>&amp;</code></td>
<td style="text-align: left;">Column separator</td>
<td style="text-align: left;">Primarily used in tables and aligned equations to separate columns or entries.</td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>{ }</code></td>
<td style="text-align: left;">Grouping &amp; arguments</td>
<td style="text-align: left;">Used to define the scope of a command, or to wrap the required arguments of a command.</td>
</tr>
</tbody>
</table>
<p>If you want to display any of the above symbols literally in your document — for example, 50% or $ — you cannot simply type them directly. Most of these symbols need to be <strong>escaped</strong> by placing a backslash in front of them:</p>
<ul>
<li>Type <code>\%</code> to display <code>%</code></li>
<li>Type <code>\$</code> to display <code>$</code></li>
<li>Type <code>\_</code> to display <code>_</code></li>
</ul>
<section id="using-arguments" class="level3">
<h3 class="anchored" data-anchor-id="using-arguments">Using Arguments</h3>
<p>In <code>\documentclass[12pt]{article}</code>, you can see two different types of brackets, each with its own role in LaTeX:</p>
<ul>
<li><strong>Optional argument <code>[...]</code></strong>: Typically used to adjust settings (such as the font size <code>12pt</code>). These arguments can be left empty — if omitted, the system will simply apply the default value.</li>
<li><strong>Mandatory argument <code>{...}</code></strong>: Defines the core content of a command (such as the document class <code>article</code>). If this is left empty or omitted, the compiler will throw an error because it does not know what to act on.</li>
</ul>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
<span class="screen-reader-only">Tip</span>A Quick Reminder
</div>
</div>
<div class="callout-body-container callout-body">
<p>Before you start typing, please keep these two concepts in mind — they are quite different from how Word works:</p>
<ol type="1">
<li><strong>Spaces</strong>: In a LaTeX document, <strong>one space and ten spaces have the same effect</strong>. The compiler always treats them as a single space.</li>
<li><strong>Line breaks</strong>: Pressing <code>Enter</code> once does not create a new line in the PDF output. To force a line break, use:
<ul>
<li>Commands: <code>\\</code>, <code>\newline</code>, or <code>\linebreak</code>.</li>
<li>Keyboard shortcut: <strong>Windows</strong> users can press <code>Ctrl + Enter</code>; <strong>macOS</strong> users can press <code>⌘ + ↩︎</code>.</li>
</ul></li>
</ol>
</div>
</div>
</section>
<section id="using-commands" class="level3">
<h3 class="anchored" data-anchor-id="using-commands">Using Commands</h3>
<p>A LaTeX command typically starts with a backslash <code>\</code> and ends at the first “non-letter” character (such as a space, punctuation mark, or digit). This can lead to a common problem:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode latex code-with-copy"><code class="sourceCode latex"><span id="cb1-1">This is my first <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">\LaTeX</span> typesetting example.</span></code></pre></div></div>
<p>Because the space after <code>\LaTeX</code> is consumed as the command terminator — it gets “eaten up” — the output becomes: <code>This is my first LaTeXtypesetting example.</code>, with the words squashed together!</p>
<p>To avoid this, there are three standard solutions:</p>
<ol type="1">
<li><code>{\LaTeX}</code>: Isolate the command inside curly braces.</li>
<li><code>\LaTeX{}</code>: Add an empty pair of curly braces after the command.</li>
<li><code>\LaTeX\</code>: Add a backslash followed by a space to force the output of one space.</li>
</ol>
<div class="latex-runner" data-engine="pdflatex"><pre class="latex-source">\documentclass[12pt]{article}

\title{My First Document}
\author{Author}
\date{\today}

\begin{document}
\maketitle

This is my first \LaTeX typesetting example.    % Words squash together

This is my first {\LaTeX} typesetting example.  % Method 1: isolate with curly braces

This is my first \LaTeX{} typesetting example.  % Method 2: add empty curly braces after the command

This is my first \LaTeX\ typesetting example.   % Method 3: add backslash and space to force output

\end{document}</pre></div>
</section>
<section id="using-comments" class="level3">
<h3 class="anchored" data-anchor-id="using-comments">Using Comments</h3>
<p>The comment symbol <code>%</code> can be placed anywhere in the document. Any text that follows it on the same line will be ignored by the compiler and is usually displayed in a different colour in the editor. Comments are useful not only for writing notes, but also for “experimenting” — when you want to test whether a certain setting looks better, you can “comment out” the old setting rather than deleting it, making it easy to switch back and compare the differences.</p>
</section>
<section id="using-environments" class="level3">
<h3 class="anchored" data-anchor-id="using-environments">Using Environments</h3>
<p>LaTeX has a powerful structure called an <strong>environment</strong>, which defines a specific region in which the enclosed content follows a particular set of typesetting rules.</p>
<p>Every environment must have both an opening and a closing tag:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode latex code-with-copy"><code class="sourceCode latex"><span id="cb2-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\begin</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">environment</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;"> name</span>}</span>
<span id="cb2-2">The content affected goes here ...</span>
<span id="cb2-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\end</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">environment</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;"> name</span>}</span></code></pre></div></div>
<p>The most fundamental environment is <code>document</code>. All content you want to appear in the final output must be placed inside it. This forms the standard skeleton of a LaTeX document:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode latex code-with-copy"><code class="sourceCode latex"><span id="cb3-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\documentclass</span>[12pt]{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">article</span>}</span>
<span id="cb3-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">% This is the preamble</span></span>
<span id="cb3-3"></span>
<span id="cb3-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\begin</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">document</span>}</span>
<span id="cb3-5">This is the "body" ...</span>
<span id="cb3-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\end</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">document</span>}</span></code></pre></div></div>
</section>
</section>
<section id="exercises" class="level2">
<h2 class="anchored" data-anchor-id="exercises">Exercises</h2>
<div class="exercises">
<div class="exercise" data-answer="A">
<p>What is the correct order when setting up a LaTeX environment?</p>
<div class="options">
<ul>
<li>Install the <strong>compiler</strong> first, then the editor</li>
<li>Install the <strong>editor</strong> first, then the compiler</li>
<li>The order doesn’t matter — install both at the same time</li>
<li>Just use Overleaf; no installation needed</li>
</ul>
</div>
<div class="answer">
<p>The correct answer is to <strong>install the compiler first</strong>. Editors need to detect the compiler’s path to function correctly. If you install the editor first, it may fail to locate the compiler, making configuration difficult. While Overleaf requires no installation, you should still follow this order when setting up a local environment.</p>
</div>
</div>
<div class="exercise" data-answer="B">
<p>In <code>\documentclass[12pt]{article}</code>, what kind of argument is <code>12pt</code>?</p>
<div class="options">
<ul>
<li>A mandatory argument, placed inside <code>{}</code></li>
<li>An optional argument, placed inside <code>[]</code></li>
<li>An environment name, placed inside <code>\begin{}</code></li>
<li>Part of the command name, placed after <code>\</code></li>
</ul>
</div>
<div class="answer">
<p><code>12pt</code> is placed inside square brackets <code>[]</code>, making it an <strong>optional argument</strong> — it can be omitted, in which case LaTeX defaults to 10pt. The <code>article</code> inside curly braces <code>{}</code> is the <strong>mandatory argument</strong> and cannot be left out.</p>
</div>
</div>
<div class="exercise" data-answer="C">
<p>What does the <code>%</code> symbol do in LaTeX source code?</p>
<div class="options">
<ul>
<li>Enters math mode</li>
<li>Produces a percentage sign</li>
<li>Marks a comment — everything after <code>%</code> on that line is ignored by the compiler</li>
<li>Separates table columns</li>
</ul>
</div>
<div class="answer">
<p><code>%</code> is LaTeX’s <strong>comment character</strong>. Everything following it on the same line is ignored during compilation and will not appear in the PDF. To display an actual percentage sign in your document, you must type <code>\%</code> (escaped with a backslash).</p>
</div>
</div>
<div class="exercise">
<p>Write a minimal LaTeX document using the <code>article</code> class with a 12pt font size that outputs the line <code>Hello, LaTeX!</code>.</p>
<div class="answer">
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode latex code-with-copy"><code class="sourceCode latex"><span id="cb4-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">\documentclass</span>[12pt]{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">article</span>}</span>
<span id="cb4-2"></span>
<span id="cb4-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\begin</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">document</span>}</span>
<span id="cb4-4"></span>
<span id="cb4-5">Hello, LaTeX!</span>
<span id="cb4-6"></span>
<span id="cb4-7"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">\end</span>{<span class="ex" style="color: null;
background-color: null;
font-style: inherit;">document</span>}</span></code></pre></div></div>
<p>This is the simplest valid LaTeX document. <code>\documentclass</code> defines the class and options; all body content must sit between <code>\begin{document}</code> and <code>\end{document}</code>; the preamble (between the two) is left empty here but is where you would load packages and configure global settings.</p>
</div>
</div>
</div>
</section>
<section id="chapter-summary" class="level2">
<h2 class="anchored" data-anchor-id="chapter-summary">Chapter Summary</h2>
<p>In this chapter, we have lifted the curtain on LaTeX and established the correct mindset for typesetting. Here are the key takeaways:</p>
<section id="core-concept-separation-of-content-and-formatting" class="level3">
<h3 class="anchored" data-anchor-id="core-concept-separation-of-content-and-formatting">Core Concept: Separation of Content and Formatting</h3>
<ul>
<li><strong>LaTeX</strong> allows you to focus on “writing content”, while the “professional typesetting” is handled by the compiler.</li>
<li>Unlike Word, LaTeX uses <strong>commands</strong> to precisely control the structure of a document.</li>
</ul>
</section>
<section id="environment-setup-order-matters" class="level3">
<h3 class="anchored" data-anchor-id="environment-setup-order-matters">Environment Setup: Order Matters</h3>
<ul>
<li><strong>Golden rule</strong>: You must <strong>install the compiler first</strong> (e.g., TeX Live, MiKTeX, MacTeX), and <strong>then install the editor</strong> (e.g., TeXstudio, VS Code).</li>
<li>If you want to skip the installation process, <strong>Overleaf</strong> is the most convenient cloud-based alternative.</li>
</ul>
</section>
<section id="quick-syntax-reference" class="level3">
<h3 class="anchored" data-anchor-id="quick-syntax-reference">Quick Syntax Reference</h3>
<ul>
<li><strong>Command prefix</strong>: All commands begin with a backslash <code>\</code>.</li>
<li><strong>Argument types</strong>: Square brackets <code>[]</code> are optional “add-ons”; curly braces <code>{}</code> are required and cannot be omitted.</li>
<li><strong>Spaces and line breaks</strong>: Multiple spaces are treated as one; use <code>\\</code> or a blank line to create a new paragraph.</li>
<li><strong>Document structure</strong>: A complete document consists of a “Preamble” and a “Body” wrapped inside the <code>document</code> environment.</li>
</ul>


</section>
</section>


<a onclick="window.scrollTo(0, 0); return false;" id="quarto-back-to-top"><i class="bi bi-arrow-up"></i> Back to top</a><div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>The word “not smart” here does not refer to the “wrap text around a picture” feature itself, but rather to the fact that images tend to drift out of their intended position — they often shift around depending on how much text is on the page, and adjusting them manually can be an exhausting experience.↩︎</p></li>
<li id="fn2"><p>The editor is used for writing and managing LaTeX code, providing a convenient writing environment; the compiler is responsible for converting that code into the final output document.↩︎</p></li>
<li id="fn3"><p>Choosing a LaTeX Compiler. (n.d.). Overleaf, Online LaTeX Editor. <a href="https://es.overleaf.com/learn/latex/Choosing_a_LaTeX_Compiler%23Other_compilers">https://es.overleaf.com/learn/latex/Choosing_a_LaTeX_Compiler%23Other_compilers</a>↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>LaTeX</category>
  <guid>https://yueswater.com/articles/posts/latex/intro/latex-intro-en.html</guid>
  <pubDate>Sun, 12 Apr 2026 00:00:00 GMT</pubDate>
  <media:content url="https://yueswater.com/articles/posts/cover/LaTeX_logo.png" medium="image" type="image/png" height="81" width="144"/>
</item>
</channel>
</rss>
