Вы находитесь на странице: 1из 53

LATEX guide for Slader contributors

Slader LLC

Document version: 1.2


Outline
Introduction
Typesetting Text
Adding Programming Code to LateX
Typesetting Mathematics
Dollar Signs
LaTeX on Slader
Tables
Packages
Align Environment
Additional Examples
Overset Command
Overbrace/Underbrace Commands
Gather Environment
Matrices
Colors
Fractions
Brackets
Final Comments
Why LATEX

I It makes beautiful documents


I Especially documents with mathematics content
I It was created by scientist, for scientists
I A large and active community
I It is easy to use
I Slader has its own built-in LaTeX compiler
I List of packages that can be used will be given later
How does it work

I You write your document in plain text with commands that


describe its structure and meaning.
I The LaTeX program processes your text and commands to
produce a beautifully formatted document.
I A large and active community

The rain in Spain falls \emph{ mainly } on the plain.




latex
y

The rain in Spain falls mainly on the plain.


More examples of commands and their output

LaTeX code Output


\begin{itemize}
\item Tea I Tea
\item Milk I Milk
\item Biscuits I Biscuits
\end{itemize}

LaTeX code Output


\begin{equation}
\alpha + \beta + 1
\end{equation} α+β+1 (1)
Typesetting Text

I For the most part, you can just type your text normally.

. LaTeX code . Output


Words are separated by one Words are separated by one
or more spaces. or more spaces.

Paragraphs are separated by Paragraphs are separated by


one or more blank lines. one or more blank lines.

I Space in the source file is collapsed in the output.

. LaTeX code . Output


The rain in Spain The rain in Spain falls
falls mainly on the plain. mainly on the plain.
Typesetting Text: Caveats
I Quotation marks are a bit tricky:
use a backtick ‘ on the left and an apostrophe ’ on the
right.
. LaTeX code . Output
Single quotes: ‘text’. Single quotes: ‘text’.
Double quotes: “text”. Double quotes: “text”.

I Some common characters have special meanings in LATEX


% percent sign
# hash (pound / sharp) sign
& ampersand
$ dollar sign
I If you just type these, you’ll get an error. If you want one to
appear in the output, you have to escape it be preceding it
with a backslash.
Typesetting Text: Other commands

To force a line break one can use \\.


To start a new paragraph, just add a blank line to your code.

Some useful commands in text mode:


I \textit{italic text} italic text
I \textbf{bold text} bold text
I \textcolor{red}{red text} red text
I \fbox{boxed text} boxed text
Adding programming code

We can use \begin{verbatim} and \end{verbatim} commands to


start and end code environment. This means that inside this
environment you can write special LaTeX characters without
getting any errors.
To write programming code in-line you can use commands:
I {\tt some code} some code
I {\texttt some code} some code
Note that when writing programming code in-line special
characters should still be escaped with the use of \, while in
verbatim environment you can write whatever text you want.
Typesetting Mathematics: Dollar Signs

I Why are the dollar signs $ so special? We use them to


mark mathematics in text.

. LaTeX code Output


.
% not so good: Let a and b be distinct
Let a and b be distinct positive positive integers, and let
integers, and let c = a - b + 1. c = a - b + 1.

% much better: Let a and b be


Let $a$ and $b$ be distinct positive distinct positive integers,
integers, and let $c = a - b + 1$. and let c = a − b + 1.
I Always use dollar signs in pairs - one to begin the
mathematics, and one to end it.
Dollar Signs - Continued

Another important thing to remember is that single dollar signs


are used for in-line formulas and double dollar signs or \[ ... \]
are used for display style.

Example code:
Use single dollar signs for in line display of
$f(x)=x^2\cdot \sin x$.

Use double dollar signs or \[ \] for display


style of function:

$$f(x)=x^2\cdot \sin x$$


\[ f(x)=x^2 \]
Dollar Signs - Continued

LaTeX output:

Use single dollar signs for in line display of f (x) = x 2 · sin x.


Use double dollar signs or \[ \] for display style of function:

f (x) = x 2 · sin x
f (x) = x 2

Notice that when display style environment is used, output is


centered.
Typesetting Mathematics: Notation
I Use caret ˆ for superscripts and underscore . for
subscripts.
. LaTeX code . Output
$ y = c 2 xˆ2 + c 1 x + c 0$ y = c1 x 2 + c1 x + c0
I Use curly braces { } to group superscripts and subscripts.

. LaTeX code Output


.
$ F n = F n-1 + F n-2$ Not ok! Fn = Fn − 1 + Fn − 2
$ F n = F {n-1} + F {n-2}$ Ok! Fn = Fn−1 + Fn−2
I There are commands for Greek letters and common
notation.

LaTeX code . Output


.
Q/RT
$ \mu = A eˆ{Q/RT} $ µ = Ae
Pn
$ \Omega = \sum {k=1}ˆ{n} \omega k $ Ω = k=1 ωk
Typesetting Mathematics: Displayed Equations

I If it’s big and scary, display it on its own line using


\begin{equation} and \end{equation}.

. LaTeX code Output


.
The roots of a quadratic equation The roots of a quadratic
are given by equation are given by
\begin{equation} √
x = \frac {-b \pm \sqrt bˆ2 - 4ac}} x = −b± 2a b 2 −4ac
(2)
{2a}
\end{equation} where a, b and c
where $a$, $b$ and $c$ are \ldots are . . .
A
Caution: LTEXmostly ignores your spaces in mathematics,
but it can’t handle blank lines in equations - don’t put blank
lines in your mathematics.
Typesetting Mathematics: Functions

When using common functions in math mode, use them with


backslash.

Function Usage
cos x $ \cos x $
sin x $ \sin x $
ln x $ \ln x $
det (A) $ \det (A) $
arctan x $ \arctan x $

If you want to use a function that is not supported by the


compiler you can write the function as the argument of \text{...}
command, and it will be displayed normally (not italic).
LaTeX editor on Slader

Slader site has its own built in LaTeX editor which means that you
can write the code directly on site.

So, when you want to write a solution using LaTeX, you can avoid
writing standard stuff like \begin{document} → you simply write
the content of the solution itself.
How to write LateX using Slader editor
Once you found the exercise in the desired book, click on the
Upload your solution button to open the editor.
When you click on the button, editor opens up where you write
your LateX.

By default, there are two rows and two columns of boxes where
you can write the code. Also, there is a final answer box.
If you want to add more rows or columns you can simply click on
”add row” or ”add column” button.
Clicking on the box
When you click on the box, editor opens up where you write the
actual code. You can keep writing the code as you like, but you
have to click outside the box to compile the code and see the
output.

After clicking outside the box:


Using built-in tools
Once you click on the box, small button x 2 appears which
contains some built-in tools which can help you with writing your
solution or reduce the time needed for writing one.
Using built-in tools - continued
You can write your math into the box below ”Enter your math
below and automatically see what the output would be. After you
are done, you can click on the Place math button to render the
code.

Check out all the buttons that are already built in when writing
your code.
Boxes containing only text

If you have boxes that contain only text (no dollar signs or other
LaTeX commands) then you can add \\ to the end. This way,
output will be in standard LaTex font instead of the default one.

In first box, we used code This is some text. and in second box
we used This is some text.\\ .

As you can see output fonts are different and we want the second
one.
Writing final answer

After you have worked out the solution, it is time to write final
answer into the final answer box.

Please try to avoid answers like ”Click for solution” or ”See


inside” as much as possible.

If you obtained some number or expression as a final result, write


it down into the final answer box.

On the other hand, if the problem was to draw some graph or


write some proof then you can write answers like:

Click for graph.\\


Click for proof.\\
Interlude: Environments

I Commands \begin and \end are used to create many different


environments
I Here are examples of itemize and enumerate environments

. LaTeX code Output


\begin{itemize} % for bullet points I Biscuits
\item Biscuits
\item Tea I Tea
\end{itemize}
.
\begin{enumerate} % for numbers
\item Biscuits 1. Biscuits
\item Tea
2. Tea
\end{enumerate}
Creating a simple table

To create a simple table in LaTeX we use \begin{tabular}


command.
Code Output
\begin{center}
\begin{tabular}{ c c }
cell1 & cell2 \\ cell1 cell2
cell3 & cell4 cell3 cell4
\end{tabular}
\end{center}
With the parameter { c c } we specified that we want two
columns in our table. We add more rows as we fill out the content
of the cells. If you want vertical lines between cells then use
parameter with separator lines { | c | c | }.
Vertical and horizontal separators
You can use multiple vertical separators as well as horizontal
separators. If you want to add a horizontal separator before or after
the row, use \hline command. You can also use parameter [αex] to
add white space after the row (use desired number in place of α).

Code Output
\begin{center}
\begin{tabular}{|c||c|||}
\hline
cell1 & cell2 \\ cell1 cell2
\hline\hline cell3 cell4
cell3 & cell4 \\ [1ex]
cell 5 cell 6
\hline
cell 5 & cell 6
\end{tabular}
\end{center}
Additional parameters

Before you start constructing a table, you can also set different
parameters:
I \setlength{\arrayrulewidth}{1mm}
It sets the thichkness of the borders of the table
I \setlength{\tabcolsep}{15pt}
It sets the space between the text and the borders (left and
right) of its containing cell
I \renewcommand{\arraystretch}{1.5}
It sets the height of each row relative to its default height. If
we use 1.5, then the row has height 1.5 × ( default height)
Example
Below is the example code that uses additional parameters.
\setlength{\arrayrulewidth}{1mm}
\setlength{\tabcolsep}{18pt}
\renewcommand{\arraystretch}{1.5}

\begin{tabular}{ |p{2cm}|p{2cm}|p{2cm}| }
\hline
\multicolumn{3}{|c|}{Students List} \\
\hline
Student & Subject & Grade \\
\hline
Ana & Calculus &B+ \\
Steve & Biology & A\\
Lily & Physics & A+ \\
\hline
\end{tabular}
Example - continued

LaTeX output:

Students List

Student Subject Grade

Ana Calculus B+
Steve Biology A
Lily Physics A+

When defining tabular we used p{2cm} to define column with


fixed length of 2 cm. After that we used \multicolumn command
to join multiple cells in the row into a single cell.
Interlude: Packages
Below is the list of all the LaTeX packages included in Slader
compiler with brief descriptions:
I amsmath
Includes various elements for improving the printed
mathematical formulas such as \equation and \align.
I amssymb
Includes various symbols that can be added in math mode,
such as \cap (∩) and \cup (∪).
I wasysym
Includes various characters that come from the wasy fonts,
such as \Box (2) and \checked ().
I textcomp
Includes commands that make multiple column writing
possible.
I array
Includes implementations of the array and tabular
environments.
Interlude: Packages
I arcs
Includes two commands, \overarc and \underarc .
I polynom
Includes various commands for manipulating polynomials, such
as \polylongdiv{}{} (long division) and
\polyhornerscheme[ ]{} (synthetic division).
I cancel
Includes commands for canceling out terms (usually in
a·b
fractions),  .
a
I enumerate
Includes enumerate environment.
I dcolumn
Includes various types of column formatting.
Documentation for each package with additional explanations can
be found on https://ctan.org/ .
Typesetting Mathematics: Examples with amsmath
I Use equation* (”equation-star”) for unnumbered equations

LaTeX code Output


.
\begin {equation*} Xn
. \Omega =\sum {k=1}ˆ{n} \omega k Ω= ωk
\end {equation*} k=1
I LATEX treats adjacent letters as variables multiplied together,
which is not always what you want. amsmath defines
command for many common mathematical operators

. LaTeX code Output


\begin {equation*} % Bad!
. min {x,y} (1-x)ˆ2 minx,y (1 − x)2
\end {equation*}
.
\begin {equation*} % Good!
min (1 − x)2
. \min {x,y}{ (1-x)ˆ2} x,y
\end {equation*}
Typesetting Mathematics: Examples with amsmath
Align a sequence of equations at the equals sign

(x + 1)3 = (x + 1)(x + 1)(x + 1)


= (x + 1)(x 2 + 2x + 1)
= x 3 + 3x 2 + 3x + 1

with the align* environment.


Code:
\begin{align*}
(x+1)^3 &= (x+1)(x+1)(x+1) \\
&= (x+1)(x^2+2x+1)\\
&= x^3+3x^2+3x+1
\end{align*}
I An ampersand & separates the left column (before the =)
from the right column (after the =).
I A double backslash \\ starts a new line.
Typesetting Mathematics: Align environment

Most important and most common command used when writing


solutions is align. It can be used in every single mathematical
process that needs alignment like evaluating integrals, reducing
matrices, solving equations.
If used as align, then each row in the process will be numbered.
To avoid that, we can use align*.
Align environment - Examples
Code:
\setcounter{equation}{0}
\begin{align}
\int (\sin x+x^2)dx &=\ int \sin x dx+\int x^2dx\\
& =\boxed{-\cos x+\frac{x^3}{3} + C}
\end{align}

LaTeX output:
Z Z Z
(sin x + x 2 )dx = sin xdx + x 2 dx (1)

x3
= − cos x + +C (2)
3

When using align, each row is numbered. If you want to skip


numbering of one row, just add \nonumber command before \\.
Command \setcounter{equation}{0} was used to ensure that the
equation numbering starts from (1).
Align environment - Examples

Code:
\setcounter{equation}{0}
\begin{align}
f(x)&=x^2+e^x\sin x\\ \nonumber\\
f’(x)&=(x^2+e^x\sin x)’\nonumber\\
&=(x^2)’+(e^x\sin x)’\nonumber\\
&=2x+(e^x)’\cdot \sin x
+e^x\cdot (\sin x)’\nonumber\\
&=2x+e^x\sin x+e^x\cos x\nonumber\\
&=\boxed{2x+e^x(\sin x+\cos x)}
\end{align}
Align environment - Examples
LaTeX output:

f (x) = x 2 + e x sin x (1)

f 0 (x) = (x 2 + e x sin x)0


= (x 2 )0 + (e x sin x)0
= 2x + (e x )0 · sin x + e x · (sin x)0
= 2x + e x sin x + e x cos x
= 2x + e x (sin x + cos x) (2)

In the first row, between \\ we used command \nonumber because


first \\ sets us in the new row, and that new row will be
automatically assigned with the equation number. That is why we
have to remove it.
To remove equation numbering in the whole process, you can just
replace align with align* in both \begin and \end commands.
Align environment - Examples

To produce the same example, we can use following code:


\begin{align*}
f(x)&=x^2+e^x\sin x \tag{1}\\\\
f’(x)&=(x^2+e^x\sin x)’\\
&=(x^2)’+(e^x\sin x)’\\
&=2x+(e^x)’\cdot \sin x
+e^x\cdot (\sin x)’\\
&=2x+e^x\sin x+e^x\cos x\\
&=\boxed{2x+e^x(\sin x+\cos x)} \tag{\text{Result}}
\end{align*}

This time we used align* environment and we manually added


tags with \tag command.
ADDITIONAL EXAMPLES
In the following pages we present few examples of some
commonly used commands and show how to implement them
into the code.
Example #1 - Command \overset
It can be used for short descriptions of steps. For example, when
finding derivative of a function.
Code:
\begin{align*}
(\log (x^2\sin x))’&=\dfrac{1}{x^2\sin x}
\overset{\text{Chain rule}}{\cdot}(x^2\sin x)’\\
&=\dfrac{1}{x^2\sin x}\cdot
\overset{\text{Product Rule}}{((x^2)’\cdot \sin x
+ x^2\cdot (\sin x)’)}
\end{align*}

LaTeX output:
1 Chain rule
(log(x 2 sin x))0 = · (x 2 sin x)0
x 2 sin x
Product Rule
1
= 2 · ((x 2 )0 · sin x + x 2 · (sin x)0 )
x sin x
Example #2 - Command \overset
It can also be used to add detailed explanations later.
Code:
\begin{align*}
\dfrac{1}{\csc^2x}+\cos^2x&\overset{(1)}{=}
\sin^2x+\cos^2x\\
&\overset{(2)}{=}\color{red}{1}
\end{align*}

Explanations:

(1)\hspace{0.2cm}By definition,
$\dfrac{1}{\csc^2 x}
=\dfrac{1}{\frac{1}{\sin^2x}}=\sin^2x$

(2)\hspace{0.2cm}Pytagorean rule:$\sin^2x+\cos^2x=1$
Example #2 - Continued

LaTeX output:
1 (1)
2
+ cos2 x = sin2 x + cos2 x
csc x
(2)
=1

Explanations:
1 1
(1) By definition, 2
= 1 = sin2 x
csc x
sin2 x
(2) Pytagorean rule: sin2 x + cos2 x = 1

Additional commands used:


I \color{red}→ change color of the output to red
I \hspace{x cm}→ add horizontal spacing of x centimeters
Example #3 - Commands \overbrace and \underbrace
Example Code:
\begin{align*}
\overbrace{\int x^2e^xdx}^{\boxed{\begin{smallmatrix}
\text{Integration by parts}\\
u=x^2\quad du = 2x\\
dv = e^x\quad v = e^x
\end{smallmatrix}}}&=x^2e^x - \int 2xe^xdx\\
&=x^2e^x -2\underbrace{\int xe^xdx}_{\boxed{
\begin{smallmatrix}
\text{Integration by parts}\\
u=x\quad du = 1\\
dv = e^x\quad v = e^x
\end{smallmatrix}}} +C\\
&=x^2e^x-2(xe^x-\int 1\cdot e^xdx)+C\\
&=x^2e^x-2(xe^x-\int e^xdx)+C\\
&=\boxed{\color{blue}{e^x(x^2-2x-2)+C}}
\end{align*}
Example #3 - Continued
LaTeX output:

Integration by parts
u=x 2 du=2x
dv =e x v =e x
z
Z }| { Z
2 x
x e dx = x 2e x − 2xe x dx
Z
= x 2e x − 2 xe x dx +C
| {z }
Integration by parts
u=x du=1
dv =e x v =e x
Z
2 x x
= x e − 2(xe − 1 · e x dx) + C
Z
2 x x
= x e − 2(xe − e x dx) + C

= e x (x 2 − 2x − 2) + C
Example #3 - Further explanations

As seen in the example we can use \overbrace or \underbrace


command to write one group of text/formulas over/below another
group of text/formulas.
It is best used when explaining some steps. This is a very neat tool
when you want to describe:
I differentiation rules that were used when derivative was
evaluated
I rules used when simplifying some logarithmic expression or
when working with powers/roots
Also, in this example we used:
I \quad: adds white space (like tab)
I smallmatrix: like a regular matrix, just smaller
I \boxed: draws box around the output, very good for writing
formulas or final result
Example # 4 - Gather

Another way to show equations in the center of the field is to use


gather environment. Use \begin {gather} to start the environment
and \end {gather} to close the environment. To avoid automatic
numbering of equations use gather* instead of gather.

Example Code:
\begin{gather*}
3x+4y=17\\
4y=17-3x\\
y=\frac{17-3x}{4}
\end{gather*}
Example #4 - Continued

LaTeX output:

3x + 4y = 17
4y = 17 − 3x
17 − 3x
y=
4

As you can see, all rows written inside gather environment are
pushed towards the middle.
Example # 5 - Matrices
Below are examples of different types of matrices.

LaTeX code Output


\begin{matrix}
a_{11}&a_{12} \\ a11 a12
a_{13}&a_{14} a13 a14
\end{matrix}
\begin{bmatrix}  
a_{11}&a_{12} \\ a11 a12
a_{13}&a_{14} a13 a14
\end{bmatrix}
\begin{Bmatrix}  
a_{11}&a_{12} \\ a11 a12
a_{13}&a_{14} a13 a14
\end{Bmatrix}
LaTeX code Output
\begin{pmatrix}  
a_{11}&a_{12} \\ a11 a12
a_{13}&a_{14} a13 a14
\end{pmatrix}
\begin{vmatrix}
a_{11}&a_{12} \\ a11 a12

a_{13}&a_{14} a13 a14
\end{vmatrix}
\begin{Vmatrix}
a_{11}&a_{12} \\ a11 a12

a_{13}&a_{14} a13 a14
\end{Vmatrix}
\bigl[ \begin{smallmatrix}
a_{11}&a_{12} \\  a11a12

a13 a14
a_{13}&a_{14}
\end{smallmatrix}\bigr]
Example # 5 - Further explanations

In previous examples we saw that the general form of matrix is:

\begin {*matrix}
cell 1 & cell 2 & cell 3 \\
cell 4 & cell 5 & cell 6
\end {*matrix}

• Choose b, B, p, v, V or nothing instead of * for the type of


matrix you want to produce
• We use delimiters & to separate columns and \\ to separate
rows
• smallmatrix comes without brackets by default; we have to
add brackets ourselves
Example # 6 - Colors
We can change the color of the output. Color can be changed of
both plain text and math outputs.

LaTeX code Output


{\color{red}{
Some text.
\text{Some text.}}}
{\color{blue}{
f (x) = sin(3x)
$f(x)=\sin (3x)$}}
{\color{Orange}{ x2 x
3x = 3
$\frac{x^2}{3x}=\frac{x}{3})$}}
List of all colors that can be used within Slader LaTeX compiler
can be found here:
https://en.wikibooks.org/wiki/LaTeX/Colors
Using a specific color is case sensitive, so something like
\color{maroon} is not going to work.
Notice that in last example we use Orange with an upper case
”O”.
Example # 7 - Fractions
Basically, there are two types of fractions that are most commonly
used. These are \frac and \dfrac.
The main difference here is that frac reduces the font size in
numerator and denominator where dfrac keeps the same font. The
best way is to use dfrac when you have complicated expressions in
numerator and/or denominator and use frac otherwise.

LaTeX code Output


x3
$\dfrac{x^3}{x^2}=x$ =x
x2
$\frac{\sin x} sin x
cos x = tan x
{\cos x}=\tan x$
$\dfrac{\frac{x^3}{x}} x3
x 3x 4
{\frac{2x+1}{3x}}= 2x+1
= 2x 2 +x
\frac{3x^4}{2x^2+x}$ 3x
Example #8 - Brackets
Here is the correct way brackets should be used inside LaTeX
code. This way, brackets are automatically sized up to appropriate
expression inside them.

LaTeX code Output


\left ( ... \right ) (...)
\left \| ... \right \| k...k
\left [ ... \right ] [...]
\left \{ ... \right \} {...}
\left \lfloor ... \right \rfloor b...c
\left | ... \right | |...|
\left \lceil ... \right \rceil d...e
Final comments

Here is the list of tips & tricks that can be useful:

• Use variables inside math environment as default (ex.


$ a, b, x, y $) so they are displayed as italic and use units and
everything else inside \text{} (ex. $ \text{ m/s } $).

• Write final answer in different color and/or with a box around


it (ex. \boxed{\color{Maroon}{f(x)=\sin(x)}} ).

• When writing fractions in superscript or subscript use \frac


instead of \dfrac. Or simply write them as a/b.

Вам также может понравиться