2: The basics
Installing LaTeX
Installing LaTeX is actually ridiculously simple. If you’re on windows, you download the MiKTeX installer, and run it. This will install virtually every LaTeX engine in existence for you, as well as most of the common packages. If you’re in some kind of *nix, you install TeXLive using your favourite install manager and you’re equally good to go.
My First Source
Every LaTeX file starts with a line that says what you’re actually trying to make, called a “document class”. The two common document classes as “article” and “book”, which mostly differ in terms of which commands you can use for sectioning (“book” lets you use chapters and parts, which for an article don’t make sense, for instance), but there are also really vast and super-customisable document classes like the “memoir” document class, which is so complete that the manual for just using it is well over 550 pages long!
In these tutorials we’ll instead go with the “book” document class, and take it from there. A minimal LaTeX source that will compile into a proper document contains the following code:
\documentclass{book}
\begin{document}
\end{document}
And that’s it. If you save this code to a text file, and save that text file to “mysource.tex”, then you can compile this source into a final document by opening a command line (in windows, start -> “cmd”, or “command” for really old versions of windows; in *nix, open a terminal), navigating to the location that you saved “mysource.tex” to, and then typing:
$> xelatex mysource.tex
The ‘$>’ here is just a two character text that represents your command prompt, and will have its own look on your computer (windows users will be used to things like C:\…\>, *nix users will be more familiar with simple prompts like $)
Running this will generate an output not unlike the following:
This is XeTeX, Version 3.1415926-2.2-0.999.7 (MiKTeX 2.7)
entering extended mode
(mysource.tex
LaTeX2e <2005/12/01>
Babel <v3.8l> and hyphenation patterns for english, dumylang,
nohyphenation, german, ngerman, german-x-2008-06-18,
ngerman-x-2008-06-18, french, loaded.
(“$\tex\latex\base\book.cls” Document Class: book 2005/09/16
v1.4f Standard LaTeX document class (“$\tex\latex\base\bk10.clo”))
No file mysource.aux.
(mysource.aux) )
No pages of output.
Transcript written on mysource.log.
This is XeTeX, Version 3.1415926-2.2-0.999.7 (MiKTeX 2.7)entering extended mode(mysource.texLaTeX2e <2005/12/01>Babel <v3.8l> and hyphenation patterns for english, dumylang, nohyphenation, german, ngerman, german-x-2008-06-18, ngerman-x-2008-06-18, french, loaded.(“C:\Program Files (x86)\MiKTeX 2.7\tex\latex\base\book.cls”Document Class: book 2005/09/16 v1.4f Standard LaTeX document class(“C:\Program Files (x86)\MiKTeX 2.7\tex\latex\base\bk10.clo”))No file mysource.aux.(mysource.aux) )No pages of output.Transcript written on mysource.log.
XeLaTeX, unlike most flavours of LaTeX, compiles straight to PDF, which is great. Other versions tend to compile to something called DVI, which stands for device-independent format, which has as major problem that DVI never became popular, whereas PDF is pretty much the defacto standard for finalised documents. So hurray for XeLaTeX!
Anyway, this compile process will not have generated a PDF file, since there was no text in our source, so it told us “No pages to output”. To make it generate some pages, let’s add some text:
\documentclass{book}
\begin{document}
This is (almost) the simplest \LaTeX\ document there is.
\end{document}
If we run this through XeLaTeX, we get a different output:
This is XeTeX, Version 3.1415926-2.2-0.999.7 (MiKTeX 2.7)
entering extended mode
(mysource.tex
LaTeX2e <2005/12/01>
Babel <v3.8l> and hyphenation patterns for english, dumylang,
nohyphenation, german, ngerman, german-x-2008-06-18,
ngerman-x-2008-06-18, french, loaded.
(“$\tex\latex\base\book.cls” Document Class: book 2005/09/16
v1.4f Standard LaTeX document class (“$\tex\latex\base\bk10.clo”))
(mysource.aux)
[1] (mysource.aux) )
Transcript written on mysource.log.
This did produce a PDF file, with one page (we see that xelatex made one page by the [1] in its output text. If it had made ten pages, we would have seen it make the entire list of [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]). This PDF file has our text in it, but with the word “LaTeX” typeset correctly. How did that happen?
Macros
LaTeX works on the concept of macros, or substitutable codes. In our little demonstrator text, the word \LaTeX is such a macro, and it tells LaTeX to replace “\LaTeX” with whatever code it knows should be used in its place. In this case, some code (of which we don’t know what it exactly looks like) that makes the word “LaTeX” show up the way it is supposed to look, rather than as normal text looking like “capital L, lower case a, capital T, lower case e, capital X”.
In our document, we have two macros: “\LaTeX” and “\ “. The reason we have this second macro there, which is replaced by a space, is because LaTeX is a bit primitive when it comes to seeing what is, and what isn’t a macro, so if it sees “\LaTeX ” it will go “ah, a slash, I will go into macro interpreting mode”, then it will read “LaTeX” and instead of seeing a space after it, it sees “a character that cannot be used in a macro name”, so it goes out of macro mode, does the necessary replacements for you, and then eats up that space. Rather inconvenient! So to ensure we get a space in our document after the \LaTeX macro, we instead write “\LaTeX\ “, which makes it go ”ah, a slash, I will go into macro interpreting mode”, then it will read “LaTeX” and then sees a slash making it go “ah, a new slash, I will do the necessary replacements for the macro up to this point, and then start on a new one”. After doing the replacements for the \LaTeX macro it will build this new macro and it sees ” ” as macro label. This is a special label (there are a small number of single character special macros) and makes it go “ah, a space. This means I must replace this “\ ” with an actual space in the final document”, and it does so.
The reason spaces aren’t always a good thing is because you might want a macro to be part of a word. Say we have a macro called \TeX (which we do, that’s a built in macro) and we want to talk about “texnicians”, with the “tex” part being typeset the way it’s supposed to, we can write “\TeX nicians”, and rather than getting a space in our document, we’ll get a single word, “TeXnicians” with the “TeX’ typeset exactly as it should be.
But this is all kind of academic – to go to a more practical explanation of how to use LaTeX, let us look at the next page in this series, so that we can start writing our own documents, styled the way we want them to be styled.