LaTeX:Layout
LaTeX |
About - Getting Started - Diagrams - Symbols - Downloads - Basics - Math - Examples - Pictures - Layout - Commands - Packages - Help |
This article outlines some of the basics of layout in LaTeX.
Note: Rather than typing up all the examples, you can copy-paste the examples into your TeXnicCenter files. We highly recommend opening up your TeXnicCenter and trying out each of the examples as you go. It takes almost no time at all to just copy-paste, compile, and view the results.
Source File Format
The source file of a LaTeX broadly consists of two parts, the preamble and the document itself. The preamble consists of everything before the \begin{document} command. Things like margin settings, document style definitions, paragraph spacing settings, custom function definition and page numeration style are items that are set in the preamble. Often, much of the preamble is placed in a separate file and included using the \usepackage statement. This allows you to use the same code in many source files by just including a single line in each source file.
Our next three sections deal primarily with preamble items, while the rest cover tools you might use within your document.
Preamble Formatting
Document Class
The first line of your source code sets the document class with the cleverly named command \documentclass. While LaTeX supports several classes, such as book, report, and letter, we will be focusing on the document class article in these webpages. Using \documentclass, we can also set the general font size for the document (which we can reset for parts of the document as required). Thus, the first line of your source code will almost always look like this:
\documentclass[11pt]{article}
If you want a slightly larger font, try 12pt. If smaller, go for 10pt.
Should you decide to use one of the other classes, for example if you chose to write a book with LaTeX, we suggest getting a book on LaTeX.
Including Packages You'll Need
Immediately following the \documentclass statement we usually include all the packages we'll need using \usepackage. The two most common packages to include are the amsmath package, which defines many new mathematical symbols, and the graphicx package, which allows you to include images in your document. So, for instance, many of your documents may begin:
\documentclass[11pt]{article} \usepackage{amsmath} \usepackage[pdftex]{graphicx}
The pdftex in brackets is an optional argument to the graphicx package. In general, for many commands, we can add optional arguments in brackets; we'll be describing some of these as we go along.
After you have become more comfortable with LaTeX, read about how to create your own packages to simplify your source files and to allow you to easily use the same style and commands in multiple source files without having to copy over all the commands from one source file to the next.
Layout of the Page
In the preamble we define all the relevant settings for our page, such as margins, width of text, page sizes, etc. Here's an example of what might follow your \documentclass declaration to set page parameters:
\documentclass[11pt]{article} \usepackage{amsmath} \pdfpagewidth 8.5in \pdfpageheight 11in \setlength\topmargin{0in} \setlength\headheight{0in} \setlength\headsep{0in} \setlength\textheight{7.7in} \setlength\textwidth{6.5in} \setlength\oddsidemargin{0in} \setlength\evensidemargin{0in} \setlength\parindent{0.25in} \setlength\parskip{0.25in}
That may look pretty intimidating. For starters, you really only need to worry about the first two lines if you are creating PDF documents. You can easily get away with ignoring the rest; LaTeX will simply use reasonable defaults for the other values. The two \pdf commands tell LaTeX what size PDF document to create. You can see that our settings above are for a standard 8.5 x 11 page.
Here are some parameters you might set (note you can use any of several different units of length, such as in for inches, cm for centimeters, or pt for points):
\pdfpageheight, \pdfpagewidth | Height and width of the PDF page to create (i.e. size of paper you'd print on). |
\topmargin | Margin at top of page above all printing. Add 1 inch (so that, for example, setting \topmargin to 0.25in would produce a top margin of 1.25 inches). |
\evensidemargin | Left margin on even numbered pages. Add 1 inch (as with \topmargin). |
\oddsidemargin | Left margin on odd numbered pages. Add 1 inch (as with \topmargin). |
\headheight | Height of the header (the header is text that appears atop all pages). |
\headsep | Distance from bottom of header to the body of text on a page. |
\topskip | Distance from top of main text box to the baseline of the first line of text in the main text box. |
\textheight, \textwidth | Height and width of main text box. |
\footskip | Distance from bottom of body to the bottom of the footer (the footer is the text at the bottom of each page). |
\parskip | Distance between paragraphs. |
\parindent | Amount of indentation at the first line of a paragraph. |
Many of these parameters (if not all) can be set to negative numbers. Note also that there's no explicit way to set the right margin on a page: you can control the left margin (with \oddsidemargin and \evensidemargin) and the width of the text (with \textwidth), which implicity controls the right margin.
There are many other parameters that you can set in the preamble, such as the title of the document, the header style, the footer style, page numbering, etc. You can consult books or Google for more information on these areas.
Starting Your Document
After you've finished your preamble and are ready to start typing up your document, you start off your document with \begin{document}. As you might guess, you must end with \end{document}. Here is a complete source file that should compile correctly to create a PDF document:
\documentclass[11pt]{article} \usepackage{amsmath} \pdfpagewidth 8.5in \pdfpageheight 11in \setlength\topmargin{0in} \setlength\headheight{0in} \setlength\headsep{0in} \setlength\textheight{7.7in} \setlength\textwidth{6.5in} \setlength\oddsidemargin{0in} \setlength\evensidemargin{0in} \setlength\headheight{77pt} \setlength\headsep{0.25in} \begin{document} This is my practice document. \end{document}
For the remainder of this page, all the sample code will only include items in the body of the document (i.e. the stuff between \begin{document} and \end{document}). You can just copy it into the file you created above in place of the 'This is my practice document.'
Document Formatting
This section will cover techniques to format material in the document you create--remember, all the code in this section is just the stuff between \begin{document} and \end{document} in your source document.
Paragraphs
Any time LaTeX sees a blank line, it treats the next line as the start of a new paragraph. For example, try the following text:
Fourscore and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty and dedicated to the proposition that all men are created equal.
Now we are engaged in a great civil war, testing whether that nation or any nation so conceived and so dedicated can long endure. We are met on a great battlefield of that war. We have come to dedicate a portion of it as a final resting place for those who died here that the nation might live. This we may, in all propriety do. But in a larger sense, we cannot dedicate, we cannot consecrate, we cannot hallow this ground. The brave men, living and dead who struggled here have hallowed it far above our poor power to add or detract. The world will little note nor long remember what we say here, but it can never forget what they did here. It is rather for us the living, we here be dedicated to the great task remaining before us--that from these honored dead we take increased devotion to that cause for which they here gave the last full measure of devotion--that we here highly resolve that these dead shall not have died in vain, that this nation shall have a new birth of freedom, and that government of the people, by the people, for the people shall not perish from the earth. |
When you typeset this, you should find that each of the three sections above is indented and is its own paragraph. Moreover, you should see that if you don't have a full empty line between two lines in your source file, there is not only no new paragraph, but there is no line break, either. To end one paragraph and start another it is not enough to simply hit return and start typing on the next line - you must hit return twice and create an empty line for LaTeX to know to start a new paragraph.
You can also create indents wherever you want in text by adding the \indent command, and you can suppress the automatic indent caused by a new paragraph by using \noindent. For example, try typsetting this:
Fourscore and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty and dedicated to the proposition that all men are created equal.
\indent \indent Now \indent we \indent are engaged in a great civil war, testing whether that nation or any nation so conceived and so dedicated can long endure. We are met on a great battlefield of that war. We have come to dedicate a portion of it as a final resting place for those who died here that the nation might live. This we may, in all propriety do. But in a larger sense, we cannot dedicate, we cannot consecrate, we cannot hallow this ground. The brave men, living and dead who struggled here have hallowed it far above our poor power to add or detract. The world will little note nor long remember what we say here, but it can never forget what they did here. \noindent It is rather for us the living, we here be dedicated to the great task remaining before us--that from these honored dead we take increased devotion to that cause for which they here gave the last full measure of devotion--that we here highly resolve that these dead shall not have died in vain, that this nation shall have a new birth of freedom, and that government of the people, by the people, for the people shall not perish from the earth. |
Note the effects of \indent and \noindent. Finally, understanding paragraphing rules in LaTeX is very important when using display math. Notice the difference in the following:
Now, if we can prove <math> \Big(2\sqrt{\frac{a}{b}}\Big)^m + \Big(2\sqrt{\frac{b}{a}}\Big)^m \ge 2^{m+1} </math> then we will be done. Dividing both sides of this inequality by <math>2^m</math> yields <math> \Big(\frac{a}{b}\Big)^m + \Big(\frac{b}{a}\Big)^m \ge 2, </math> which we can verify easily by AM-GM on the reciprocals that make up the LHS, thus we have our desired <math> \Big(1 + \frac{a}{b}\Big)^m + \Big(1 + \frac{b}{a}\Big)^m \ge \Big(2\sqrt{\frac{a}{b}}\Big)^m + \Big(2\sqrt{\frac{b}{a}}\Big)^m \ge 2^{m+1}. </math>
and
Now, if we can prove <math> \Big(2\sqrt{\frac{a}{b}}\Big)^m + \Big(2\sqrt{\frac{b}{a}}\Big)^m \ge 2^{m+1} </math> then we will be done. Dividing both sides of this inequality by <math>2^m</math> yields <math> \Big(\frac{a}{b}\Big)^m + \Big(\frac{b}{a}\Big)^m \ge 2, </math> which we can verify easily by AM-GM on the reciprocals that make up the LHS, thus we have our desired <math> \Big(1 + \frac{a}{b}\Big)^m + \Big(1 + \frac{b}{a}\Big)^m \ge \Big(2\sqrt{\frac{a}{b}}\Big)^m + \Big(2\sqrt{\frac{b}{a}}\Big)^m \ge 2^{m+1}. </math>
In the latter, there are indents after each display math, which we clearly don't want. These are caused by the blank lines (and could be suppressed with \noindent).
Sections
For longer documents that you want to split into parts, you can use LaTeX sectioning commands. Here's an example illustrating them. If you use the book document class, you can also use \chapter, but for most documents short of books, these should be sufficient.
\section{In This First Section} This is the first section. \subsection{We Have This First Subsection} This is the first subsection. \subsubsection{And This Subsubsection} A subsubsection. \paragraph{And This Paragraph} A notable paragraph. \subparagraph{And This Subparagraph} A notable subparagraph. \subsubsection{And Then This Subsubsection} \section{The This Second Section} \subsection{We Have This Subsection}