Difference between revisions of "LaTeX:Layout"

(Spacing)
(Spacing)
Line 244: Line 244:
 
Spacing in math mode:
 
Spacing in math mode:
  
<math>x + y</math>
+
$x + y$
  
<math>x    +      y</math>
+
$x    +      y$
  
<math>x+y</math>
+
$x+y$
 
</nowiki></pre>
 
</nowiki></pre>
 
Notice that the three math expressions come out all exactly the same. In general, you can trust math mode to space things out right rather than forcing any special spacing. This means that you should write formulas in your source document to be easily readable (by you), and trust LaTeX to do the right spacing.
 
Notice that the three math expressions come out all exactly the same. In general, you can trust math mode to space things out right rather than forcing any special spacing. This means that you should write formulas in your source document to be easily readable (by you), and trust LaTeX to do the right spacing.
Line 269: Line 269:
 
Here are examples of these in action:
 
Here are examples of these in action:
 
<pre><nowiki>
 
<pre><nowiki>
<math>x+y</math>
+
$x+y$
  
<math>x+\,y</math>
+
$x+\,y$
  
<math>x+\:y</math>
+
$x+\:y$
  
<math>x+\;y</math>
+
$x+\;y$
  
<math>x+\quad y</math>
+
$x+\quad y$
  
<math>x+\qquad y</math>
+
$x+\qquad y$
  
<math>x+\!y</math>
+
$x+\!y$
 
</nowiki></pre>
 
</nowiki></pre>
 
Two spacing tools that can can be bluntly used to move things are \hspace and \vspace.
 
Two spacing tools that can can be bluntly used to move things are \hspace and \vspace.

Revision as of 20:14, 10 July 2007

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}

Font Sizes and Styles

To change the font size, use any one of the following commands. To change it for just a portion of the page, enclose that potion in { } and have the relevant font size command occur right at the beginning of the text inside the curly braces. In order from smallest to largest, the font sizes you can use are:

\tiny
\scriptsize
\footnotesize
\small
\normalsize
\large
\Large
\LARGE
\huge
\Huge

Try this out; the effects should be pretty clear:

When I was born, I was {\small small}. Actually, {\scriptsize I was
very small}. When I got older, I thought some day {\Large I would be
large}, {\Huge maybe even gigantic}. But instead, I'm not even
normalsize. {\small I'm still small.}

Here is a simple example that will probably show you all you need to know about bold, italics, and underlining.

When something is \emph{really}, \textbf{really} important, you can
\underline{underline it}, \emph{italicize it}, \textbf{bold it}. If
you \underline{\textbf{\emph{must do all three}}}, then you can nest
them.

Here is another example that demonstrates font families:

You may want to write things \textsf{in a sans-serif font}, or
\texttt{in a typewriter font}, or \textsl{in a slanted font} (which
is \emph{slightly different} than italics). Sometimes it pays
\textsc{to write things in small capitals}. You can next go to
\textbf{bold and then \textsl{bold and slanted} and then back to just
bold} again.

Spacing

There are a few spacing items you'll find useful in LaTeX. First, you can force an extra normal-size space (as between words) by using a single backslash followed by a space. This is particularly useful after periods: LaTeX interprets periods as ends of sentences, so it puts extra space after them, but if a period doesn't in fact end a sentence, you don't want that extra space. Try this to see an example.

When Mr. Rogers read this, he was confused because the first sentence
was only two words long. Mrs.\ Rogers wasn't confused at all.

In math mode, it's a little different. LaTeX ignores normal spaces in math mode, so all three of the following will come out the same:

Spacing in math mode:

$x + y$

$x     +       y$

$x+y$

Notice that the three math expressions come out all exactly the same. In general, you can trust math mode to space things out right rather than forcing any special spacing. This means that you should write formulas in your source document to be easily readable (by you), and trust LaTeX to do the right spacing.

However, if you do need to tweak the spacing in math mode, there are some special commands:

\, a small space
\: a medium space
\; a large space
\quad a really large space
\qquad a huge space
\! a negative space (moves things back to the left)

Here are examples of these in action:

$x+y$

$x+\,y$

$x+\:y$

$x+\;y$

$x+\quad y$

$x+\qquad y$

$x+\!y$

Two spacing tools that can can be bluntly used to move things are \hspace and \vspace.

I \hspace{2in} like \hspace{1in} space.

\vspace{5in}

In every direction.

Sometimes, \hspace and \vspace will not produce the space you want; this happens most frequently at the beginning or the end of a line or of a page. If you want to force space there, use \hspace* or \vspace* instead.

Two more that can be used even more bluntly are \hfill and \vfill, which cause LaTeX to create as much horizontal or vertical space as possible (within the boundaries of a page as defined in the preamble):

I\hfill like \hfill space.
\vfill
In every direction.

Finally, you can force line breaks and prevent them in LaTeX. To force a linebreak somewhere in a block of text, simply use \\. To prevent a linebreak, you can use ~. The ~ in the text below prevents LaTeX from breaking a line between Professor and Reiter:

This website is largely a result of Professor Harold Reiter getting
me involved in math education again.\\ Professor~Reiter's dedication
to educating eager math students is an inspiration. Students,
parents, and educators who like this site should thank
Professor~Reiter. If you don't already know Professor~Reiter, don't
worry, you'll eventually meet him. Professor~Reiter meets everybody.

Try taking out the ~ symbols, and you'll probably see Professor and Reiter get split up at least once. Notice also that the \\ forces LaTeX to start a new line, but not a new paragraph.

Justification (Centering, etc).

Tables

Boxes

Lists

Referencing

Comments

See Also