Difference between revisions of "LaTeX:LaTeX on AoPS"

(The Very Basics)
 
(101 intermediate revisions by 2 users not shown)
Line 11: Line 11:
 
LaTeX uses a special "math mode" to display mathematics. There are two types of this "math mode":
 
LaTeX uses a special "math mode" to display mathematics. There are two types of this "math mode":
  
* <b>In-line math mode</b>. In in-line math mode, we use <code>$</code> signs to enclose the math we want to display, and it displays in-line with our text. For example, typing <code>$\sqrt{x} = 5$</code> gives us <math>\sqrt{x} = 5.</math>
+
==== In-line Math Mode ====
  
* <b>Display math mode</b>. In display math mode, we enclose our code in double dollar signs, and it displays the math centered and on its own line. For example, <nowiki>$$\sqrt{x} = 5$$</nowiki> gives us <cmath>\sqrt{x} = 5.</cmath>
+
In in-line math mode, we use <code>$</code> signs to enclose the math we want to display, and it displays in-line with our text. For example, typing <code>$\sqrt{x} = 5$</code> gives us <math>\sqrt{x} = 5.</math>
  
Besides displaying in-line vs. displaying centered and on a new line, the two modes render differently in other ways. Note that <nowiki>$\sum_{k=1}^n k^2$</nowiki> gives us <math>\textstyle\sum_{k=1}^n k^2,</math> whereas <nowiki>$$\sum_{k=1}^n k^2$$</nowiki> gives us <cmath>\sum_{k=1}^n k^2.</cmath> If, for some reason, you want the display-math-mode style in in-line mode, just place <code></code> at the start of your code. For example, <nowiki>$\sum_{k=1}^n k^2$ </nowiki> gives <math>\textstyle \sum_{k=1}^n k^2,</math> whereas <nowiki>$\sum_{k=1}^n k^2$</nowiki> gives <math>\sum_{k=1}^n k^2.</math>
+
==== Display Math Mode ====
  
LaTeX can also be used to draw figure and diagrams on the AoPS site using a language called [http://artofproblemsolving.com/wiki/index.php/Asymptote_(Vector_Graphics_Language) Asymptote]. See [http://artofproblemsolving.com/wiki/index.php/Asymptote_(Vector_Graphics_Language) Asymptote] to learn more.
+
In display math mode, we enclose our code in double dollar signs, and it displays the math centered and on its own line. For example, <code>$$\sqrt{x} = 5$$</code> gives us <cmath>\sqrt{x} = 5.</cmath>
  
That's all there is to invoking LaTeX on the AoPS site. Of course, you'll want to do more than square roots!  You can read through the [[LaTeX:Symbols|Symbols]] and [[LaTeX:Commands|Commands]] pages to learn how to render other symbols with LaTeX. You can also click on formulas created by others to see the code they've used.
+
==== In-line vs. Display ====
 +
 
 +
Besides displaying in-line vs. displaying centered and on a new line, the two modes render differently in other ways. Note that <code>$\sum_{k=1}^n k^2$</code> gives us <math>\textstyle\sum_{k=1}^n k^2,</math> whereas <code>$$\sum_{k=1}^n k^2$$</code> gives us <cmath>\sum_{k=1}^n k^2.</cmath>
 +
 
 +
=== Basic Expressions ===
 +
 
 +
==== Multiplication ====
 +
 
 +
Sometimes, when we're multiplying, we don't need a multiplication symbol. For instance, we can write <math>xy</math> instead of <math>x\cdot y</math> without ambiguity. However, when you're multiplying numbers, for instance, a multiplication symbol comes in handy. The standard symbol is given by <code>$\cdot$</code>. For example, <code>$12\cdot\frac{1}{2}$</code> gives us <math>\textstyle 12\cdot \frac 12.</math>
 +
 
 +
==== Fractions ====
 +
 
 +
We can make fractions via <code>$\frac{...}{...}$</code>. For instance, <code>$\frac{x+y}{2}$</code> will give us <math>\textstyle\frac{x+y}{2}.</math>
 +
 
 +
==== Roots ====
 +
 
 +
Square roots in <math>\LaTeX</math> are pretty simple; we just type <code>$\sqrt{...}$</code>. For instance, <code>$\sqrt{2}$</code> gives us <math>\sqrt 2.</math> Cube roots, fourth roots, and so on are only slightly more difficult; we type <code>$\sqrt[n]{...}$</code>. For instance, <code>$\sqrt[4]{x-y}$</code> gives <math>\sqrt[4]{x-y}.</math>
 +
 
 +
==== Superscripts & Subscripts ====
 +
 
 +
To get superscripts (or exponents), we use the caret symbol <code>^</code>. Typing <code>$x^2+y^2$</code> gives <math>x^2+y^2.</math> Subscripts are obtained via an underscore (holding shift and the minus sign on most keyboards). For instance, <code>$a_k$</code> yields <math>a_k.</math>
 +
 
 +
==== Groups ====
 +
 
 +
Most operations in <math>\LaTeX</math> (such as superscripts and subscripts) can only see the "group" of characters immediately following it. We use curly braces <code>{...}</code> to indicate groups longer than one character. For instance, if we wrote <code>$x^2015$</code>, we'd expect to get <math>x^{2015},</math> but we instead get <math>x^2015.</math> This is because each character in the string <code>2015</code> is in its own group until we tell <math>\LaTeX</math> that <code>2015</code> should be one whole group. To convey this information to <math>\LaTeX</math>, we write <code>$x^{2015}$</code> and we get <math>x^{2015}.</math>
 +
 
 +
=== Beyond the Basic Expressions ===
 +
 
 +
==== Grouping Basic Expressions ====
 +
 
 +
Our ordinary parentheses <code>(...)</code> and brackets <code>[...]</code> work to group expressions in <math>\LaTeX</math>. For instance, <code>$(x+y)[z+w]$</code> gives us <math>(x+y)[z+w].</math> We can also group expressions using curly braces, but we can't just type <code>{...}</code>. Rather, we must type <code>\{...\}</code>. This is because <math>\LaTeX</math> uses plain curly braces for other things, such as fractions and superscripts and subscripts.
 +
 
 +
<br><br>
 +
 
 +
When we put (vertically) large expressions inside of parentheses (or brackets, or curly braces, etc.), the parentheses don't resize to fit the expression and instead remain relatively small. For instance, <code>$$f(x) = \pi(\frac{\sqrt{x}}{x-1})$$</code> comes out as <cmath>f(x) = \pi(\frac{\sqrt{x}}{x-1}).</cmath> To automatically adjust the size of parentheses to fit the expression inside of them, we type <code>\left(...\right)</code>. If we do this for our <math>f</math> equation above, we get <cmath>f(x) = \pi\left(\frac{\sqrt{x}}{x-1}\right).</cmath> We can use <code>\left</code> and <code>\right</code> for all sorts of things... parentheses (as we saw), brackets <code>$\left[...\right]$</code>, braces <code>$\left\{...\right\}</code>, absolute values <code>$\left|...\right|$</code>, and much more (norms, floor and ceiling functions, inner products, etc.).
 +
 
 +
==== Lists ====
 +
 
 +
To make a list, such as a sequence, we use <code>\dots</code>. For example, <code>$a_0,a_1,\dots,a_n$</code> will give us <math>a_0,a_1,\dots,a_n.</math>
 +
 
 +
==== Sums ====
 +
 
 +
There are two basic ways to write out sums. First, we can use <code>+</code> and <code>\cdots</code>. An example of this way would be <code>$a_1+a_2+\cdots+a_n$</code> This will give us <math>a_1+a_2+\cdots+a_n.</math> Second, we could use summation notation, or <code>\sum</code>. Such an example is <code>$\sum_{i=0}^n a_i$</code>, giving <math>\textstyle \sum_{i=0}^n a_i.</math> Note the use of superscripts and subscripts to obtain the summation index.
 +
 
 +
==== Products ====
 +
 
 +
Again, there are two basic ways to display products. First, we can use <code>\cdot</code> and <code>\cdots</code>. An example is <code>$n! = n\cdot(n-1)\cdots 2\cdot 1$</code>, which of course gives <math>n! = n\cdot(n-1)\cdots 2 \cdot 1.</math> The alternative is to use product notation with <code>\prod</code>. For instance, <code>$n! = \prod_{k=1}^n k$</code>, giving <math>\textstyle n! = \prod_{k=1}^n k.</math>
 +
 
 +
=== Equalities and Inequalities ===
 +
 
 +
==== Inequalities ====
 +
 
 +
the commands <code>>, <, \geq, \leq,</code> and <code>\neq</code> give us <math>>,</math> <math><,</math> <math>\geq,</math> <math>\leq,</math> and <math>\neq,</math> respectively.
 +
 
 +
==== Aligning Equations ====
 +
 
 +
To align multiple equations, we use the <code>align*</code> environment. For example, we might type a system of equations as follows:
 +
<pre>
 +
\begin{align*}
 +
ax + by &= 1 \\
 +
cx + dy &= 2 \\
 +
ex + fy &= 3.
 +
\end{align*}
 +
</pre>
 +
(You do not need dollar signs.) The <code>&</code> symbol tells <math>\LaTeX</math> where to align to and the \\ symbols break to the next line. This code will output
 +
<cmath>\begin{align*}
 +
ax + by &= 1 \\
 +
cx + dy &= 2 \\
 +
ex + fy &= 3.
 +
\end{align*}</cmath> An example of a string of equations is:
 +
<pre>
 +
\begin{align*}
 +
((2x+3)^3)' &= 3(2x+3)^2 \cdot (2x+3)' \\
 +
&= 3(2x+3)^2 \cdot 2 \\
 +
&= 6(2x+3)^2.
 +
\end{align*}
 +
</pre>
 +
Again, the <code>&</code> symbol tells <math>\LaTeX</math> where to align to, and the \\ symbols break to the next line. This code outputs
 +
<cmath>\begin{align*}
 +
((2x+3)^3)' &= 3(2x+3)^2 \cdot (2x+3)' \\
 +
&= 3(2x+3)^2 \cdot 2 \\
 +
&= 6(2x+3)^2.
 +
\end{align*}</cmath>
 +
 
 +
==== Numbering Equations ====
 +
 
 +
To number equations, we use the <code>align</code> environment. This is the same environment as the <code>align*</code> environment, but we leave the <code>*</code> off. The <code>*</code> suppresses numbering. To number one equation, the code
 +
<pre>
 +
\begin{align}
 +
ax + by = c
 +
\end{align}
 +
</pre>
 +
will produce
 +
<cmath> \begin{align}
 +
ax + by = c.
 +
\end{align} </cmath> We don't have to use <code>&</code> or \\ since there is nothing to align and no lines to break. To number several equations, such as a system, the code
 +
<pre>
 +
\begin{align}
 +
ax + by &= c \\
 +
dx + ey &= f \\
 +
gx + hy &= i
 +
\end{align}
 +
</pre>
 +
will produce
 +
<cmath> \begin{align}
 +
ax + by &= c \\
 +
dx + ey &= f \\
 +
gx + hy &= i.
 +
\end{align} </cmath> In general, <code>align</code> will auto-number your equations from first to last.
 +
 
 +
==== Comments in Equations ====
 +
 
 +
Again, we use the <code>align*</code> environment. The code
 +
<pre>
 +
\begin{align*}
 +
ax + by &= c & \text{because blah} \\
 +
dx + ey &= f & \text{by such-and-such}
 +
\end{align*}
 +
</pre>
 +
will produce
 +
<cmath> \begin{align*}
 +
ax + by &= c & \text{because blah} \\
 +
dx + ey &= f & \text{by such-and-such}.
 +
\end{align*} </cmath> (You can use <code>align</code> to get numbering <i>and</i> comments!)
 +
 
 +
==== Definition by Cases ====
 +
 
 +
To define, say, a function by cases, we use the <code>cases</code> environment. The code
 +
<pre><nowiki>
 +
$$ </nowiki> \delta(i,j) = \begin{cases} 0 & \text{if } i \neq j \\ 1 &\text{if } i = j \end{cases} <nowiki> $$
 +
</nowiki></pre>
 +
gives us
 +
<cmath>
 +
\delta(i,j) = \begin{cases} 0 & \text{if } i \neq j \\ 1 &\text{if } i = j. \end{cases}
 +
</cmath> As usual, the <code>&</code> is for aligning and the \\ is for line-breaking.

Latest revision as of 21:35, 26 June 2020

LaTeX
About - Getting Started - Diagrams - Symbols - Downloads - Basics - Math - Examples - Pictures - Layout - Commands - Packages - Help

This article explains how to use LaTeX in the AoPSWiki, the AoPS Community, and the AoPS Classroom. See Packages to know which packages are prebuilt into the AoPS site.



Getting Started with LaTeX

The Very Basics

LaTeX uses a special "math mode" to display mathematics. There are two types of this "math mode":

In-line Math Mode

In in-line math mode, we use $ signs to enclose the math we want to display, and it displays in-line with our text. For example, typing $\sqrt{x} = 5$ gives us $\sqrt{x} = 5.$

Display Math Mode

In display math mode, we enclose our code in double dollar signs, and it displays the math centered and on its own line. For example, $$\sqrt{x} = 5$$ gives us \[\sqrt{x} = 5.\]

In-line vs. Display

Besides displaying in-line vs. displaying centered and on a new line, the two modes render differently in other ways. Note that $\sum_{k=1}^n k^2$ gives us $\textstyle\sum_{k=1}^n k^2,$ whereas $$\sum_{k=1}^n k^2$$ gives us \[\sum_{k=1}^n k^2.\]

Basic Expressions

Multiplication

Sometimes, when we're multiplying, we don't need a multiplication symbol. For instance, we can write $xy$ instead of $x\cdot y$ without ambiguity. However, when you're multiplying numbers, for instance, a multiplication symbol comes in handy. The standard symbol is given by $\cdot$. For example, $12\cdot\frac{1}{2}$ gives us $\textstyle 12\cdot \frac 12.$

Fractions

We can make fractions via $\frac{...}{...}$. For instance, $\frac{x+y}{2}$ will give us $\textstyle\frac{x+y}{2}.$

Roots

Square roots in $\LaTeX$ are pretty simple; we just type $\sqrt{...}$. For instance, $\sqrt{2}$ gives us $\sqrt 2.$ Cube roots, fourth roots, and so on are only slightly more difficult; we type $\sqrt[n]{...}$. For instance, $\sqrt[4]{x-y}$ gives $\sqrt[4]{x-y}.$

Superscripts & Subscripts

To get superscripts (or exponents), we use the caret symbol ^. Typing $x^2+y^2$ gives $x^2+y^2.$ Subscripts are obtained via an underscore (holding shift and the minus sign on most keyboards). For instance, $a_k$ yields $a_k.$

Groups

Most operations in $\LaTeX$ (such as superscripts and subscripts) can only see the "group" of characters immediately following it. We use curly braces {...} to indicate groups longer than one character. For instance, if we wrote $x^2015$, we'd expect to get $x^{2015},$ but we instead get $x^2015.$ This is because each character in the string 2015 is in its own group until we tell $\LaTeX$ that 2015 should be one whole group. To convey this information to $\LaTeX$, we write $x^{2015}$ and we get $x^{2015}.$

Beyond the Basic Expressions

Grouping Basic Expressions

Our ordinary parentheses (...) and brackets [...] work to group expressions in $\LaTeX$. For instance, $(x+y)[z+w]$ gives us $(x+y)[z+w].$ We can also group expressions using curly braces, but we can't just type {...}. Rather, we must type \{...\}. This is because $\LaTeX$ uses plain curly braces for other things, such as fractions and superscripts and subscripts.



When we put (vertically) large expressions inside of parentheses (or brackets, or curly braces, etc.), the parentheses don't resize to fit the expression and instead remain relatively small. For instance, $$f(x) = \pi(\frac{\sqrt{x}}{x-1})$$ comes out as \[f(x) = \pi(\frac{\sqrt{x}}{x-1}).\] To automatically adjust the size of parentheses to fit the expression inside of them, we type \left(...\right). If we do this for our $f$ equation above, we get \[f(x) = \pi\left(\frac{\sqrt{x}}{x-1}\right).\] We can use \left and \right for all sorts of things... parentheses (as we saw), brackets $\left[...\right]$, braces $\left\{...\right\}, absolute values $\left|...\right|$, and much more (norms, floor and ceiling functions, inner products, etc.).

Lists

To make a list, such as a sequence, we use \dots. For example, $a_0,a_1,\dots,a_n$ will give us $a_0,a_1,\dots,a_n.$

Sums

There are two basic ways to write out sums. First, we can use + and \cdots. An example of this way would be $a_1+a_2+\cdots+a_n$ This will give us $a_1+a_2+\cdots+a_n.$ Second, we could use summation notation, or \sum. Such an example is $\sum_{i=0}^n a_i$, giving $\textstyle \sum_{i=0}^n a_i.$ Note the use of superscripts and subscripts to obtain the summation index.

Products

Again, there are two basic ways to display products. First, we can use \cdot and \cdots. An example is $n! = n\cdot(n-1)\cdots 2\cdot 1$, which of course gives $n! = n\cdot(n-1)\cdots 2 \cdot 1.$ The alternative is to use product notation with \prod. For instance, $n! = \prod_{k=1}^n k$, giving $\textstyle n! = \prod_{k=1}^n k.$

Equalities and Inequalities

Inequalities

the commands >, <, \geq, \leq, and \neq give us $>,$ $<,$ $\geq,$ $\leq,$ and $\neq,$ respectively.

Aligning Equations

To align multiple equations, we use the align* environment. For example, we might type a system of equations as follows:

\begin{align*}
ax + by &= 1 \\
cx + dy &= 2 \\
ex + fy &= 3.
\end{align*}

(You do not need dollar signs.) The & symbol tells $\LaTeX$ where to align to and the \\ symbols break to the next line. This code will output \begin{align*} ax + by &= 1 \\ cx + dy &= 2 \\ ex + fy &= 3. \end{align*} An example of a string of equations is:

\begin{align*}
((2x+3)^3)' &= 3(2x+3)^2 \cdot (2x+3)' \\
&= 3(2x+3)^2 \cdot 2 \\
&= 6(2x+3)^2.
\end{align*}

Again, the & symbol tells $\LaTeX$ where to align to, and the \\ symbols break to the next line. This code outputs \begin{align*} ((2x+3)^3)' &= 3(2x+3)^2 \cdot (2x+3)' \\ &= 3(2x+3)^2 \cdot 2 \\ &= 6(2x+3)^2. \end{align*}

Numbering Equations

To number equations, we use the align environment. This is the same environment as the align* environment, but we leave the * off. The * suppresses numbering. To number one equation, the code

\begin{align}
ax + by = c
\end{align}

will produce \begin{align} ax + by = c. \end{align} We don't have to use & or \\ since there is nothing to align and no lines to break. To number several equations, such as a system, the code

\begin{align}
ax + by &= c \\
dx + ey &= f \\
gx + hy &= i
\end{align}

will produce \begin{align} ax + by &= c \\ dx + ey &= f \\ gx + hy &= i. \end{align} In general, align will auto-number your equations from first to last.

Comments in Equations

Again, we use the align* environment. The code

\begin{align*}
ax + by &= c & \text{because blah} \\
dx + ey &= f & \text{by such-and-such}
\end{align*}

will produce \begin{align*} ax + by &= c & \text{because blah} \\ dx + ey &= f & \text{by such-and-such}. \end{align*} (You can use align to get numbering and comments!)

Definition by Cases

To define, say, a function by cases, we use the cases environment. The code

$$  \delta(i,j) = \begin{cases} 0 & \text{if } i \neq j \\ 1 &\text{if } i = j \end{cases}  $$

gives us \[\delta(i,j) = \begin{cases} 0 & \text{if } i \neq j \\ 1 &\text{if } i = j. \end{cases}\] As usual, the & is for aligning and the \\ is for line-breaking.