-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhello.tex
293 lines (275 loc) · 9.67 KB
/
hello.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
\chapter{Hello, \texorpdfstring{\LaTeX}{LaTeX}!}
\label{hello}
Now that you have \LaTeX{} installed,
let's try it out.
Open up your favorite text editor and save the following as \texttt{hello.tex}:
\begin{leftfigure}
\begin{lstlisting}
\documentclass{article}
% Say hello
\begin{document}
Hello, World!
\end{document}
\end{lstlisting}
\end{leftfigure}
Next, we run this file through \LaTeX{} (the program)\footnote{Not to be
confused with \LaTeX{} the lunchbox, \LaTeX{} the breakfast cereal,
or \LaTeX{} the flamethrower. The kids love this stuff!}
to get our document.
The installation placed several different versions---or
\introduce{engines}---on your machine,
but throughout this book, we use the newest ones:
\LuaLaTeX{} and \XeLaTeX.\punckern\footnote{See Appendix~\ref{history} for a
comparison of the various \LaTeX{} engines.}
If you are using a \LaTeX{}-specific editor, it should have some menu
to select the engine you would like to use,
along with a button to generate your document.
Otherwise, run the following from your terminal:\footnote{%
How to work a terminal,
make sure the newly-installed \LaTeX{} programs are in your \texttt{PATH},
and so on are all outside the scope of this book.
As is tradition, the leading dollar sign in this example just denotes a console
prompt, and shouldn't actually be typed.}
\begin{leftfigure}
\begin{lstlisting}
$ xelatex hello.tex
\end{lstlisting}
\end{leftfigure}
Feel free to try \texttt{lualatex} instead---there are a few differences
between the two that we will discuss later, but either is fine for now.
With luck, you should see some output that ends in a message like:
\begin{leftfigure}
\begin{lstlisting}
Output written on hello.pdf (1 page).
Transcript written on hello.log.
\end{lstlisting}
\end{leftfigure}
And in your current directory, you should find a newly minted \texttt{hello.pdf}.
Open it up and you should see a page with this at the top:
\begin{leftfigure}
\lm Hello, World!
\end{leftfigure}
Congrats,
you created your first document!
Let's unpack what we just did.
All \LaTeX{} documents begin with a \verb|\documentclass| command,
which picks a base ``style'' to use.
Many classes are available---and you can even create your own---but common
ones include \texttt{article}, \texttt{report}, \texttt{book},
and \texttt{beamer}.\punckern\footnote{This last one is for slideshows,
named after a German term for a projector.}
For the average document,
\texttt{article} is probably a good choice.
The next line, \verb|% Say hello|,
is a \introduce{comment}.
\LaTeX{} ignores the rest of a line once it sees a percent sign,
so we use it to leave notes for anybody reading
the document's source.\punckern\footnote{Including, perhaps most importantly,
a confused version of your future self!}
Finally, \verb|\begin{document}| tells \LaTeX{} that what follows
is the actual document content,
and \verb|\end{document}| states that we are finished.
Let's cover some more basics.
\section{Spacing}
\LaTeX{} generally handles inter-word spacing for you, regardless of how many
times you mash the space bar or tab key.
For example, typing the following into your editor
\begin{leftfigure}
\begin{lstlisting}
The number of spaces between words doesn't matter.
The same is true for space between sentences.
An empty line ends the previous paragraph and
starts the next.
\end{lstlisting}
\end{leftfigure}
yields
\begin{leftfigure}
\lm The number of spaces between words doesn't matter.
The same is true for space between sentences.
An empty line ends the previous paragraph and
starts the next.
\end{leftfigure}
Notice how \LaTeX{} automatically follows typographic
conventions, such as indenting new paragraphs and leaving a little
more space between sentences than the space between words.
One quirk to be aware of is that comments ``eat'' any leading
space on the following line, so
\begin{leftfigure}
\begin{lstlisting}
This% weird, right?
is strange.
\end{lstlisting}
\end{leftfigure}
gives
\begin{leftfigure}
\lm This% weird, right?
is strange.
\end{leftfigure}
\section{Commands}
\LaTeX{} provides many commands to format your text,
and you can also define your own!
Commands always begin with a backslash (\,\texttt{\textbackslash}\,),
contain only letters, and are case-sensitive.\punckern\footnote{%
% \verb doesn't play nicely in footnotes, so...
\texttt{\textbackslash foo}
is different from \texttt{\textbackslash Foo}, for example.}
Some commands need more information, or \introduce{arguments}:
\verb|\documentclass|,
for example, needs to know which class we want.
Arguments are enclosed in consecutive pairs of braces,
so if some command needed two arguments, we would type:
\begin{leftfigure}
\begin{lstlisting}
\somecommand{argument1}{argument2}
\end{lstlisting}
\end{leftfigure}
Many commands also take optional arguments.
They precede the mandatory ones,
are enclosed in square brackets, and are separated by commas.
Say you want to print your document as double-sided
pages\footnote{\texttt{twoside} introduces commands
that only make sense for double-sided printing,
like one that skips to the start of the next odd page.
It also lets you have different margins for even and odd pages,
which is useful for texts like this book.}
in 11~point type.
We make these demands as optional \verb|\documentclass| arguments:
\begin{leftfigure}
\begin{lstlisting}
\documentclass[11pt, twoside]{article}
\end{lstlisting}
\end{leftfigure}
Other commands take no arguments at all---\verb|\LaTeX|,
which prints the \LaTeX{} logo, is one example.
These commands consume any space that follows them.
For example,
\begin{leftfigure}
\begin{lstlisting}
\LaTeX is great, but it can be a bit odd sometimes.
\end{lstlisting}
\end{leftfigure}
will give you
\begin{leftfigure}
\lm \LaTeX is great, but it can be a bit odd sometimes.
\end{leftfigure}
You can fix this with an empty pair of braces following the command.
Of course, you don't need braces if there is no space to preserve:
\begin{leftfigure}
\begin{lstlisting}
Let's learn \LaTeX! \LaTeX{} is a powerful tool,
but a few of its rules are a little weird.
\end{lstlisting}
\end{leftfigure}
gets us
\begin{leftfigure}
\lm Let's learn \LaTeX! \LaTeX{} is a powerful tool,
but a few of its rules are a little weird.
\end{leftfigure}
\section{Special characters and line breaks}
Some characters have special meanings in \LaTeX.
We just saw, for example, that \verb|%| starts a comment
and \verb|\| starts a command.
The full list of special characters is:
\begin{leftfigure}
\begin{lstlisting}
# $ % ^ & _ { } ~ \
\end{lstlisting}
\end{leftfigure}
Each has a corresponding command
for actually printing it in your document. Respectively, they are:
\begin{leftfigure}
\begin{lstlisting}
\# \$ \% \^{} \& \_ \{ \} \~{} \textbackslash
\end{lstlisting}
\end{leftfigure}
Regardless of whatever follows them,
the caret (\,\texttt{\^{}}\,) and tilde (\,\texttt{\~{}}\,) always need braces.
This is a relic from days when they produced \introduce{diacritical marks}:
once upon a time, \LaTeX{} users would typeset ``jalapeño'' with
\verb|jalape\~no|.
Today we just type \texttt{ñ} into our source
file.\punckern\footnote{Of course, this depends
on your keyboard, your editor, and your language settings in your \acronym{os}.
We will talk more about languages and Unicode fun in \chapref{i18n}.}
If you're wondering why we print \texttt{\textbackslash} with
\verb|\textbackslash| instead of \verb|\\|\,,
it is because the latter is the command to force a line break.
\begin{leftfigure}
\begin{lstlisting}
Give me \\
a brand new line!
\end{lstlisting}
\end{leftfigure}
obeys:
\begin{leftfigure}
\lm Give me \\
a brand new line!
\end{leftfigure}
Use this power sparingly---deciding how to elegantly break paragraphs into
lines is one of \LaTeX{}'s greatest skills.
\section{Environments}
We often format text in \LaTeX{} by placing it in \introduce{environments}.
These always start with \verb|\begin{name}| and conclude with \verb|\end{name}|,
where \texttt{name} is that of the desired environment.
Take the \texttt{quote} environment,
which adds additional space on both sides of a block quotation:
\begin{leftfigure}
\begin{lstlisting}
Donald Knuth once wrote,
\begin{quote}
We should forget about small efficiencies,
say about 97\% of the time:
premature optimization is the root of all evil.
Yet we should not pass up our opportunities in
that critical 3\%.
\end{quote}
\end{lstlisting}
\end{leftfigure}
produces
\begin{leftfigure}
\lm
Donald Knuth once wrote,
\begin{quote}
We should forget about small efficiencies,
say about 97\% of the time:
premature optimization is the root of all evil.
Yet we should not pass up our opportunities in
that critical 3\%.
\end{quote}
\end{leftfigure}
\section{Groups and scopes}
Some commands change how \LaTeX{} sets the text that follows them.
\verb|\itshape|, for example, \textit{italicizes} everything that comes after it.
To limit a command's influence to a certain area, surround it with braces.
\begin{leftfigure}
\begin{lstlisting}
{\itshape Sometimes we want italics}, but only sometimes.
\end{lstlisting}
\end{leftfigure}
becomes
\begin{leftfigure}
\lm {\itshape Sometimes we want italics}, but only sometimes.
\end{leftfigure}
The braced region is called a \introduce{group},
and commands issued inside a group lose their power once it ends.
Environments also create their own groups:
\begin{leftfigure}
\begin{lstlisting}
\begin{quote}
\itshape If I italicize a quote, the following text will
use upright type again.
\end{quote}
See? Back to normal.
\end{lstlisting}
\end{leftfigure}
typesets
\begin{leftfigure}
\lm
\begin{quote}
\itshape If I italicize a quote, the following text will
use upright type again.
\end{quote}
See? Back to normal.
\end{leftfigure}
You can also use groups to prevent spacing oddities with zero-argument
commands: some prefer \verb|{\LaTeX}| over \verb|\LaTeX{}|.