2-0: Preamble and Packages
Last week we tried to cover all of the features of LaTeX that are most essential, that you could not write a paper without. A lot of the features that we will cover this week are not included in the “vanilla” parts of LaTeX but rather come in extension packages. Hence, this brief lesson will teach you about how to use a package.
Historically, a package would be a separate file that you would install separately from the LaTeX core. Modern installations will generally handle the installation of extra packages for you. If you are using a typical online service like ShareLaTeX, then all of the common packages will also be handled automatically by their system.
This week, we’ll be putting stuff above \begin{document}
; stuff located here is known as the preamble of your input file. So compared to the basic template we used last week, we will have:
\documentclass{article} % the preamble goes here \begin{document} % all the main content goes here, like before \end{document}
To use a package, put the line \usepackage{«package name»}
in your preamble. Here is an example of using the amssymb
package, which is a symbol package by the American Math Society:
\documentclass{article} \usepackage{amssymb} % just one line of preamble \begin{document} The gtrless symbol is: $\gtrless$ \end{document}
Leaving out the include command would cause an error since \gtrless
would be undefined. But if you run the above code exactly you’ll get
\(\textrm{The gtrless symbol is: } \gtrless \)
Sometimes you will want to pass some options to a package. This is done using the syntax \usepackage[«package options»]{«package name»}
. For example, there is a fullpage
package which reduces the margins to one inch. You can pass it the option cm
to make the margins 1.5 cm instead:
\documentclass{article} \usepackage[cm]{fullpage} % smaller margins than \usepackage{fullpage} \begin{document} This page has very small margins! \end{document}
- Sometimes you’ll want to pass multiple arguments to a package, which uses the comma syntax
\usepackage[«option 1», «option 2», ..., «last option»]{«package name»}
- As a shortcut you can include multiple packages with no options using the comma syntax
\usepackage{«package 1», «package 2», ..., «last package»}
Why are LaTeX’s margins so big in the first place? It tries to meet the best known guidelines on how many characters per line is optimal for readability; newspaper articles are set in multi-column layouts for the same reason.
inputenc
for International Characters
Do NOT use this package on Assignment 1, since it makes the pate
question trivial.
On the one hand, the \'e
-type system for accents works well enough if you are just using a few accents, e.g., for author names. On the other hand, if you’re really writing in a different language, then it would be a lot more convenient if your source document was more natural-looking. There is a package that can allow this!
Specifically, the inputenc package allows you to specify accents in a more natural way. LaTeX is old-school and was originally built to only accept very plain ASCII input (of which there are less than 128 characters). Historically, every country made their own extensions into a 256-character set, matching the natural 8-bit size of “bytes” — information chunks — in computers. This resulted in a lot of incompatibility. But over the last two decades, the Unicode standard has become a way to encode information from all known languages in a unified way. At the time of writing, there are 128,237 different characters in the latest version of Unicode (9.0).
Lots of text editors, including ShareLaTeX and TeXworks, can handle editing Unicode characters. Copy and paste this chunk — éőàêçüůßø — if you want to see if your editor handles it. Even assuming that you successfully got this into your editor, unfortunately when you run LaTeX, these characters won’t appear in the output.
To get the same characters to show up in the output, you still need to tell LaTeX that you want it to be prepared for this relatively new feature. To do this, include the package inputenc
with the option utf8
:
\usepackage[utf8]{inputenc}
(As with all packages, this must go in the preamble, before \begin{document}
. UTF-8 is the name of the standard encoding by which the hundreds of thousands of Unicode characters are squeezed and re-packaged into 8-bit chunks.) Once you use this package, the accents and other Unicode characters should show up in the output.
Note that this is not a comprehensive solution and by itself cannot typeset certain other Unicode characters like ȑǎŏṗԓưǫþ, or many foreign scripts such as Arabic, Chinese, Japanese, Korean, Persian, Thai, Vietnamese, Indian languages, etc. But, we mention it as a starting point in case you are interested.
Again: Do NOT use the inputenc
package on Assignment 1, since it makes the pate
question trivial.
Conclusion
While we only talked about packages above, other things can go in the preamble too, such as the definitions of new commands. Some things like \author{}
can either go in the preamble or the main body.
There are thousands of LaTeX packages (4321 at the time of writing this), as well as other ways to extend LaTeX such as new document classes. In this course we’ll talk about the packages that we think are very important. However, you should be unafraid to read online documentation and try out any additional ones that suit your purposes. You may want to use some in the course’s final project.