· 7 years ago · Nov 02, 2018, 05:08 PM
1% Here is a file that illustrates most of the basic LaTeX commands and functionality
2% that you'll need for your assignments.
3% We're only presenting the basics here - research online or ask us for more detailed
4% explanations and more powerful options.
5%
6% Author: David Liu
7% Date: January 2, 2017
8% Prepared for use in CSC165, Winter 2017, for the Department of Computer Science at
9% the University of Toronto, St. George campus.
10
11
12% ************************************************************************
13% READ ME FIRST
14%
15% This is just the source file - reading this alone won't help you learn LaTeX.
16% To get started, jump down to line 83 and read the text there.
17% This will help you compile this file to get a pdf.
18% Then, you can jump back and forth between the source and the pdf to get an idea
19% about how to use LaTeX. For maximum understanding, make sure you read all the
20% comments in this file, and all of the text in the pdf.
21%
22% ************************************************************************
23
24
25% ************************************************************************
26% PREAMBLE (configuring the document that will be produced)
27% ************************************************************************
28
29% The most common document class is an article. This should suit your assignment needs.
30% We're using a larger font size (12pt) than normal.
31% It's helpful to have a larger font on assignments for your TAs in grading.
32\documentclass[12pt]{article}
33
34
35% Packages to use (think import statements in Python)
36\usepackage{amsmath} % This one contains many standard tools. Always include.
37\usepackage{amsfonts} % Fonts package.
38\usepackage{fancyheadings} % Included to make headers (and footers, and margin text).
39\usepackage{hyperref} % Included for hyperlinks. You shouldn't need this for your assignments.
40\usepackage{graphicx} % Include only if you want to import images.
41
42% LaTeX has many different parameters used to set margins.
43% Its default margins are quite wide, so here we're making them narrower.
44% Please do not make your assignment margins much narrower than this, though!
45% Too narrow and your text becomes much harder to read.
46\usepackage[margin=3cm]{geometry}
47
48% Define a header to appear on all pages.
49% Note that page numbers will appear automatically.
50\pagestyle{fancyplain}
51% The following lines are only to change the first page's header (change if you want to make a cover page).
52\fancypagestyle{plain}{
53\renewcommand{\headrulewidth}{0.4pt}
54}
55% Left side of header. First "David Liu" is for the first page, the second is for every other page.
56\lhead{\fancyplain{David Liu}{David Liu}}
57% Right side of header
58\rhead{\fancyplain{An Introduction to LaTeX}{An Introduction to LaTeX}}
59
60% This is metadata used to create a title.
61\title{An Introduction to LaTeX}
62\author{David Liu}
63\date{January 2, 2017}
64
65
66% ************************************************************************
67% DOCUMENT CONTENTS (the actual text that will be generated in the document).
68% ************************************************************************
69
70% This starts the document proper.
71% This is an example of an "environment," which always has a \begin{...}
72% and \end{...} (see the last line of the file).
73\begin{document}
74
75% Print the title.
76\maketitle
77
78% For longer pieces (e.g., course notes), it's helpful to divide up your work into sections.
79% Sections and subsections are automatically numbered.
80% For the remainder of this document, the actual text is all useful information!
81% Read the source and pdf files side by side to get a sense of what the commands actually do.
82\section{Introduction}
83This document is intended as a gentle introduction to some of the basic features in LaTeX.
84Questions, comments, and error-finding are most welcome.
85
86
87% ******************************************************************************
88%
89% FIRST TIME? START READING THIS!
90%
91% Here are some guidelines for getting started with LaTeX.
92Getting started with LaTeX is very easy: either download some LaTeX software
93(e.g., from here: \url{http://latex-project.org}),
94or use an online editor like \url{https://www.sharelatex.com/}.
95
96What you've downloaded from the course webpage, sample\_latex.tex, is a LaTeX source file (think Python .py file).
97Open it up in your chosen LaTeX editor, and compile it using pdflatex.
98This is the equivalent of running a Python source file.
99The result is new pdf file that contains a typeset version of your source file, based on the text and commands in the source file.
100Every time you change the source file, you must recompile using pdflatex to see the changes in the pdf.
101% ******************************************************************************
102
103
104% For assignments, it is *convenient* to have each question start on a separate page.
105\newpage
106
107
108\section{Basic Commands}
109\subsection{Text}
110Just start typing, and you'll produce a paragraph.
111Here is \emph{italics}, \textbf{bold}, and \texttt{monospace (code)}.
112For readability, you can type sentences on multiple lines in the source file, and they will show up in the same paragraph.
113
114Use a blank line to separate actual paragraphs. The first paragraph of a section is not indented, while the rest are.
115Here's a LaTeX trick: use the forward quote key at the top-left of your keyboard to do beginning quotation marks, and the regular apostrophe key to do ending ones.
116Observe: Professor Liu is ``cool.''
117% It's also easy to add footnotes! Just in case you want to add extraneous or tangential information to your assignments.
118\footnote{Seriously.}
119
120\subsection{Math}
121Let's get to the good stuff! To make x=3 look nice, we use \emph{inline math mode} with \$ signs: $x = 3$.
122There, wasn't that nice?
123Here's some more math:
124% Notice the difference between x^2 for a single char exponent and y^{10} for multiple char exponents.
125$x^2 + y^{10} = -\pi \cdot \rho \circ \alpha / \frac{1124}{z_0}$
126Notice how that fraction at the end looks a little squished?
127Here's how to make math equations stand out a bit more, and given them some extra room:
128\[ \frac{1}{2} A^{Z^{ddd}} + \frac{B}{C} + D_{Y_{123}} = \sum_{i=1}^{10} \frac{1}{1 + \frac{1}{i}} \]
129And you can just keep on typing after that.
130Notice the use of (nested) superscripts and subscripts.
131
132Here's an example of using text while in math mode.
133\[ A = \{n \in \mathbb{N} \mid n \text{ has exactly 4 factors}\} \text{ is a set that I care deeply about.}\]
134
135There will be times that you might want to produce multiple equations down a page.
136Doing this with the \textbackslash [...\textbackslash ] isn't great.
137A much better solution is the use the \textbf{align*} environment:
138% "&" is used as the alignment character. "\\" is used to start a new line.
139% You can play around with more than one alignment tab per line.
140\begin{align*}
141x^2 + 4x + 3 &= 0 \\ % Note the use of "\nonumber" and "\tag" below
142(x + 1)(x + 3) &= 0 \\
143x &= -1, -3 \tag{Eq. 1} % No "\\" on last line!
144\end{align*}
145
146\newpage
147
148\section{Three Useful Environments}
149\subsection{Lists}
150Say you're doing an assignment and want to organize your solutions by question (this should be a goal...). One way to do this is the following:
151
1521. Here's my answer to question 1.
153
1542. So far so good, but then when my solution is really long and starts to run over the page width my numbering starts to blend together. And of course this is an assignment so I'm writing a lot of words, and it's getting to the point where a single paragraph is multiple lines long.
155
156This is especially troublesome when I want to have multiple paragraphs in my solution. Again, a good idea to prevent huge walls of text assaulting the markers.
157
158Here's another paragraph before my answer to question 3. Just because.
159
1603. Hey, here's my answer to number 3. Try not to miss it among all the paragraphs. Because there are a lot of paragraphs.
161
162While I could try to play around with indentation and margins, there is a much better way. Use the \textbf{enumerate} and \textbf{itemize} environments!
163
164\begin{enumerate}
165\item[1.] Wow, an ordered list!
166\item[2.] Wouldn't it be so meta to list out the reasons this environment is good?
167\item[3.] It can even do nested lists!
168 \begin{enumerate}
169 \item[(a)] For those pesky questions with multiple parts. We did change the numbering myself, though.
170 \item[(b)] There is a default numbering scheme. Try deleting the [] parts to see what happens.
171 \end{enumerate}
172\end{enumerate}
173
174\begin{itemize}
175\item Okay, an unordered list.
176\item Maybe less useful for assignments, but you never know.
177\item This also supports:
178 \begin{itemize}
179 \item nesting.
180 \end{itemize}
181\item And both list environments support all of the usual math stuff:
182\begin{align*}
183f(x + y)^2 &= x^2 + 2xy + y^2 \\
184(x + y)(x - y) &= x^2 - y^2
185\end{align*}
186\end{itemize}
187
188\subsection{Tables}
189This environment is a bit rarer, and there are many options and more advanced environments you can use.
190
191Here we're killing \emph{three} birds with one stone:
192\begin{itemize}
193 \item showing you how a table works
194 \item giving you a list of symbols you might find useful as a reference
195 \item introducing the \href{http://www.cs.toronto.edu/~david/csc165/resources/csc.sty}{csc package}, which provides easier to remember names for some of the commands
196\end{itemize}
197
198% Put some vertical space before the table.
199\vspace{5pt}
200
201\begin{tabular}{c c c} % This specifies a table with 3 columns, all centred.
202
203% Table header. Notice the use of the alignment character "&" and newline "\\".
204Symbol & LaTeX command & csc command \\
205
206% Draw a horizontal line
207\hline
208
209% The table rows.
210$\wedge$ & \textbackslash wedge & \textbackslash AND \\
211$\vee$ & \textbackslash vee & \textbackslash OR \\
212$\neg$ & \textbackslash neg & \textbackslash NOT \\
213$\Rightarrow$ & \textbackslash Rightarrow & \textbackslash IMP \\
214$\Leftrightarrow$ & \textbackslash Leftrightarrow & \textbackslash IFF \\
215$\forall$ & \textbackslash forall & N/A \\
216$\exists$ & \textbackslash exists & N/A \\
217$\in$ & \textbackslash in & \textbackslash IN \\ % The csc command adds a little extra space around the symbols.
218$\notin$ & \textbackslash notin & \textbackslash NOTIN \\
219$\sum_{i=0}^{n}$ & \textbackslash sum\_\{i=0\}\^\{n\} & N/A \\ % In display mode, the limits will be directly above/below the symbol.
220$\prod_{i=0}^{n}$ & \textbackslash prod\_\{i=0\}\^\{n\} & N/A \\
221$\to$ & \textbackslash to & N/A \\
222$\mathbb{N,Z,R}$ & \textbackslash mathbb\{N,Z,R\} & \textbackslash N, \textbackslash Z, \textbackslash R \\
223$\mathcal O$ & \textbackslash mathcal O & \textbackslash cO \\
224$\Omega$ & \textb ackslash Omega & N/A \\ % Note: capital O - case matters!
225$\Theta$ & \textbackslash Theta & N/A % Note: capital T
226% Note: no newline "\\" on the last line of a table.
227\end{tabular}
228
229% Put some vertical space after the table.
230\vspace{5pt}
231
232If we think of more symbols to add, we'll update this table.
233If you have any suggestions, please let us know!
234\newpage
235
236
237\section{Miscellaneous}
238This section will be updated as the term progresses, as ideas come to us, either from course staff or from you!
239
240\subsection{Images}
241This subsection shows how to import a basic image file into your document.
242It's also possible to use LaTeX commands to draw an image directly,
243but we won't cover that here.
244Please note that the easiest way to import an image file is to put it in the same directory as your LaTeX source file.
245
246% Wrapping the picture in a figure environment to include a caption.
247% The "h" argument puts the figure approximately in the same place
248% that where it occurs in the source file.
249\begin{figure}[h]
250\centering % Centre the image
251% Uncomment the following line to include "myimage.jpg".
252%\includegraphics[width=100pt]{myimage.jpg} % You can specify the width (and more!) of the imported image.
253\caption{It's a tiny polar bear in a snowstorm.}
254\end{figure}
255
256
257\subsection{Algorithms}
258All of the code in this course will be written in Python style, though we will not strictly worry about the exact syntax.
259When you refer to functions, variables, or other code-related nouns in your text, you can use the command ``texttt'' to get a traditional monospace font.
260For example, in the code of \texttt{my\_function} below, the fourth line is \texttt{print(i)}. % Note that all underscores must be escaped by a backslash in LaTeX text.
261
262The easiest way to write code in LaTeX is using the \textbf{verbatim} environment, provided you don't need to typeset math in your code.
263
264% The MOST IMPORTANT THING about the verbatim environment is that
265% all whitespace in the tex file gets translated directly to whitespace
266% in the pdf. However, you must use SPACES, NOT TABS.
267% Try to follow the convention 4 spaces = 1 tab.
268% Alternatively, try to find an editor that automatically replaces
269% tabs with spaces.
270\begin{verbatim}
271def my_function(n):
272 i = 1
273 while i < n:
274 print(i)
275 i = i + 1
276 print("Done!")
277\end{verbatim}
278\newpage
279
280% These links are also available on the course webpage.
281\section{Resources}
282\begin{itemize}
283\item An online LaTeX editor (easiest to get started): \url{https://www.sharelatex.com/}. Also lots of great tutorials on here!
284\item Download LaTeX on the official LaTeX webpage: \url{http://latex-project.org/}.
285\item A relatively comprehensive introduction to LaTeX. \textbf{Highly recommended.} \\ \url{http://ctan.mirror.rafal.ca/info/lshort/english/lshort.pdf}.
286\item A LaTeX wiki. Most Google searches lead here. \url{http://en.wikibooks.org/wiki/LaTeX}.
287\item An amazing application of machine learning. Use it to find commands based on the symbol. \url{http://detexify.kirelabs.org}.
288\item A graphical LaTeX editor (must download software) \url{http://www.lyx.org/}.
289\end{itemize}
290
291
292% Don't forget to end the document environment.
293\end{document}