Peng Lv

毋意,毋必,毋固,毋我。 言必行,行必果。

导航

[转]Everything You Wanted to Know About TeX, but Were Too Afraid to Ask

==================================================================================

This artical came from: http://xoph.co/20111024/latex-tutorial/

Thanks for Xophmeister sincerely.

===================================================================================

My previous website was well-known for perhaps one thing: a LaTeX tutorial which I wrote when I was young and foolish. Despite being fairly rudimentary and not especially well-written, it gained a lot of credence — there was a time when it was the top Google result for “TeX tutorial”; I kid you not! — and because the spiders have picked up upon the HTTP 301, I have decided to edit and reinstate the best bits in this one enormous post. (I’m a traffic whore; what can  I say!?) Moreover, I will probably write further tutorials in time, so keep an eye out on my LaTeX category (or even subscribe to it, if you’re feeling particularly saucy)!

 

Contents [hide]

Introduction

This tutorial aims to provide a “quick and dirty” guide to using the document typesetting systems TeX and, particularly, LaTeX. We’ll avoid such scary things as dvips -Pcmz foo.dvi -o foo.ps and assume you’ll be using some kind of Integrated Development Environment (IDE) or specialised text editor that does the dirty work for you! Moreover, as well as general typesetting, I shall also concentrate on mathematical typesetting (follow-up posts may digress into further, specific applications).

Initially, I introduce the concepts behind TeX and LaTeX and provide some basic information on how things work. I shall assume that you already have a working TeX distribution for your platform and are using your favourite IDE (or text editor), rather than making software recommendations that are unconsidered, biased and potentially anachronistic! (I provide some links in the Online Resources section, but I’m sure the comments to this post will come to better guide you on such things.) Otherwise, the remaining sections provide a LaTeX instruction, guide and reference to various important features.

Finally, I don’t pretend to be an expert on this subject! I taught myself enough to get by between my first and second undergraduate years, with some more advanced usage while a postgraduate. Various references from throughout the Internet have been used and, I’m afraid to say, abused: My apologies to the original right holders for not respecting their terms (which I no longer have the details of). If you are in such a position and feel wronged, please get in touch with me via the comments so I can make amends. Having said that, a significant proportion of this post is derived from A Simplified Introduction to LaTeX (Greenberg) and the quite renowned The Not-So-Short Introduction to LaTeX (Oetiker, Partl, Hyna and Schlegl).

Fundamentals

What are TeX and LaTeX?

TeX — pronounced [tɛx] (usually [tɛk], in English); the reason being (and for the alternating capitals) is because it represents the Greek letters τ, ε and χ — is a typesetting engine written by Donald Knuth, back in 1977. In being particularly well-suited to the typesetting of text and mathematical formulae, it was produced to rescue the ever-deteriorating state of typographical quality in technical publications. As we use it today was first released in 1982, with some minor (future-proofing) enhancements in 1989. It is renowned for (as well as its abilities) being extremely stable, portable and virtually bug free.

Rather than a certain dubious elastomer, LaTeX is a macro package — think of it as an extension — fulfilling the role of book designer: When publishing, authors give their manuscript to their publishers, whose book designers decide on the layout before being passed to the typesetter. LaTeX — originally written by Leslie Lamport, using TeX as its typesetter — provides authors with predefined, albeit customisable, professional layouts. However, it needs instruction on, for example, a document’s structure: these are coded into the text in the form of LaTeX commands.

This is an obvious departure from the WYSIWYG approach, used by modern word processors. With these applications, one defines the layout and formatting interactively, displaying the results on screen. Unskilled authors often consequently commit layout suicide by making glib assumptions about book design; particularly focusing on aesthetics, rather than the more important aspects of readability and, ultimately, comprehension.

This can’t be done in LaTeX: it forces you to declare a logical structure and then chooses the most suitable layout. However, this comes at the cost of coding your document (usually) without a real-time preview; but rewards you with publication-quality output and, as your document grows, better management options.

Oh, and it’s free!

Nuts and Bolts

LaTeX documents are written in a number of plaintext files, usually saved with the extension .tex, and can be done so in any editor that will output ANSI-encoded ASCII (i.e., no extended characters or Unicode). As already mentioned, these files contain the document text and layout, etc. that LaTeX attempts to parse and compile into what’s known as a Device Independent file (.dvi).

(It is important to note that while the LaTeX source files are ANSI-encoded, this doesn’t mean the output cannot use extended character sets. (La)TeX provides this ability — which we shall cover later — and otherwise, if you want to use Unicode, there are alternative typesetters, such as XeTeX.)

At this point, providing there were no errors during the compilation process, we are done with LaTeX. However, beyond the fairly basic capabilities of DVI previewers, we want to do more with our output; specifically, conversion into a more useful format. Notably, into either a PDF or PostScript (.ps) file which can be distributed and printed conveniently. Indeed, nowadays, if you don’t need to go through the PostScript stage — which is most of the time! — you can bypass the DVI and go straight to PDF using the pdfLaTeX extension.

Anyway, that’s enough postulation, let’s get on with an example! The following minimal code generates this PDF output:

\documentclass{article}
\begin{document}
  I \LaTeX, therefore I am.
\end{document}

Let us note a few things from the source, which will be covered in more detail later:

  • Any text preceded by a backslash (e.g. \LaTeX) is a LaTeX command (sometimes called a LaTeX control sequence). This particular one formats the LaTeX logo.
  • The \begin and \end commands mark the opening and closing of what are known as LaTeX “environments” — the document environment, in this case: which is a minimum requirement — similar to HTML tags. These can be nested arbitrarily.
  • The section above this is known as the “preamble”; containing here the mandatory \documentclass command, which takes an operand.

So, to summarise, any LaTeX source must contain at least these three things. Also note the compiled output: Doesn’t it look nice? The margins, font sizes, etc. are all set corresponding to the “article” document class, on letter paper. (The paper size is of course adjustable; as a staunch European, I always choose A4!) If you scrolled down that far, you’d also know there’s a page number at the bottom.

\begin{document}

Source Code

Whitespace

Whitespace characters — a regular space, tab or single line break — are all treated in the same way by LaTeX: Consecutive whitespace characters are considered as one space and whitespace at the start of a line is ignored. This allows you to indent your document’s source logically to increase readability. However, of course, this means you can’t use tabs in the same way as you would in, say, Microsoft Word; we’ll cover this later. Finally, an empty gap (i.e., more than one line break) between two lines of text define the beginning and end of paragraphs.

Spaces    between   words   are condensed.

An empty line and a new paragraph!
But one line break does nothing.

Grouping

Within your text, you may form groups and subgroups, to an arbitrary degree. This is done by surrounding the appropriate with curly braces (in a logical way; i.e., for every open brace, there must be a corresponding closing brace). This has the purpose of segregating sections of text that may need to be altered independently with LaTeX commands; in effect, groups define the scope in which commands can operate.

This is a {sentence, with a group and {subgroup}}. We often
also require a null group{}.

This will output in exactly the same way as the following, as we’ve not used any modifying commands:

This is a sentence, with a group and subgroup. We often also
require a null group.

LaTeX Commands

There are two formats to LaTeX commands: both beginning with a backslash, then followed with either a case-sensitive identifying name, or a single, special character. The first of which is terminated using a space (or other “non-letter”). LaTeX ignores the whitespace after all commands; so if you need a space, insert a null group (or a tilde) first, which stops LaTeX from eating it!

Knuth divides people working with \TeX{} into \TeX{}nicians
and \TeX perts.

Some commands need a parameter that has to be given between curly braces immediately after the command name. Some commands also support optional parameters, these are added after the command name in square brackets.

\documentclass[a4paper]{article}

Reserved Characters

There are a number of symbols that you cannot use, as LaTeX uses them for something else. If you use them, LaTeX will output (if it gets that far) unexpected results. These characters are:

# $ % ^ & _ { } ~ and \

If you need to use these characters (except for the backslash), they must be preceeded with another backslash. If you try a double backslash, you will end up with a forced line break, so we use mathematics mode as an alternative (see later):

\# \$ \% \^{} \& \_ \{ \} \~{} $\backslash$

Commenting

Whenever LaTeX encounters a % character, the remainder of the line (including the %) is ignored. These are used to write comments in your input files; they’re not outputted and can be helpful (to the author) when dealing with complicated code.

This will be outputted % but this won't
This new line will be outputted

Code Structure

It’s All About Class

As we’ve already discussed, all LaTeX documents must start with a document class declaration:

\documentclass[options]{class}

There are various classes to choose from, which alter the final output of your document accordingly. To use a class, you must write its name within the curly braces:

  • article for articles in scientific journals, presentations, short reports, software documentation, etc.
  • report for longer reports containing several chapters, small books, theses, etc.
  • book for the obvious, with alternating wide margins for binding.
  • slides uses large, sans-serif lettering, particularly suited for slides and transparencies.

The optional parameters (n.b., if no options are required, the square brackets should be omitted) customise the behaviour of the document class. Multiple options must be separated by commas. Here are some common options:

  • 10pt, 11pt and 12pt specify the size of the main base font for the document. If this is unspecified, 10pt is assumed.
  • a4paper, letterpaper, etc. defines the paper size. The default is letterpaper. Also available are a5paper, b5paper, executivepaper and legalpaper.
  • fleqn typesets “displayed” mathematical formulae with left-alignment, rather than centrally.
  • leqno places the numbering for formulae on the left instead of the right hand side.
  • titlepage and notitlepage specify whether a new page should be started after the document title or not. The article class doesn’t do this by default, whereas report and book do.
  • openright and openany make chapters begin either only on right hand pages or on the next available page. This does not work with the article class; the report class by default starts chapters on the next available page, whereas the book class starts them on right hand pages.

The Preamble

The section between the document class declaration and document text is called the preamble. This is where one calls external packages and provides other miscellaneous information (such as the document’s title and author, etc.) and custom definitions. This is done with special LaTeX commands; of which, we outline a few.

Invoking Packages

LaTeX can’t do everything, at least not by itself! You can however enhance its abilities by activating any number packages you may have installed. This is done using the following command for each extra package you wish to include:

\usepackage[options]{package}

The operation and functionality of these packages should be defined in said package’s documentation. For example, for colour support we include the color package (note the American spelling):

\usepackage[usenames,dvipsnames]{color}

Also note the options: These are, in the case of the color package, specific to the generation of PDF output; they may need to change for different output formats.

There are a great deal of packages available. Their names often only loosely describe their function, so it’s a good idea to look them up in your favourite repository.

Shameless Self-Promotion

Several commands are available to specify your document’s title and your (the author’s) name. LaTeX also provides commands to put all these metadata together to produce a homogeneous, albeit simplistic title page. For a larger project, one often roles their own, but these commands are nonetheless useful for the purpose of specification:

\title{Preamble Example}
\author{Xophmeister's World}

Page Styles

LaTeX supports three predefined header/footer combinations, called “page styles”. They are set as parameters of the \pagestyle command:

  • plain prints the page numbers at the bottom of the page, in the centre of the footer. This is the default.
  • headings prints the current chapter heading and page number in the header of each page, whilst the footer remains empty.
  • empty sets both the header and footer to be empty.

It is also possible to define your own styles. Moreover, to change the page style of an individual page, the command \thispagestyle, taking the same operand, may be employed in the relevant place within the document environment.

The Document Environment

After the document class definition and the preamble comes the most important part: the document environment. This section of your input file contains your document’s content. Everything from headings and body text to mathematical formulae, tables and figures.

To define the document environment, we use the environment opening and closing commands — \begin and \end, respectively — with the option document. Everything between these boundaries is considered part of the document, which LaTeX will attempt to parse and output; anything outside this environment, as it is the last thing that LaTeX expects, will simply be ignored.

\begin{document}
  My first \LaTeX{} document!
\end{document}

General Environments

Environments are of extreme importance in LaTeX and it is paramount that you understand them before continuing. They are how LaTeX forces you to define a rigid, hierarchical structure to your document: Every environment must start with a \begin command, taking some parameter (the name of the environment); then, when finished, end with a corresponding \end command, with the same parameter name.

Environments may be nested arbitrarily, which provides us with our hierarchy. However, they must never overlap. The following, for example — presuming the appropriate environments are defined — will break LaTeX:

\begin{something}
\begin{another}
\end{something}
\end{another}

If you are familiar with HTML and particularly XML, you should find this structure (of opening and closing) rather familiar.

At this point, we also see the benefit of using indentation in your source files. Compare the following on their readability:

\begin{document}
  \begin{titlepage}
    Here Is My Title Page
  \end{titlepage}
  Here is my document.
\end{document}
\begin{document}
\begin{titlepage}
Here Is My Title Page
\end{titlepage}
Here is my document.
\end{document}

Even though this is a simple example, it is still — at a glance, at least — easier to decipher the top fragment than the second. When you get into the realms of hugely nested code, you’ll never go back to unindented code!

We shall note new environments as-and-when we come across them; you can’t put anything as the parameter! We have already discussed document and titlepage doesn’t leave much to the imagination! Your own environments, with their own special properties, may also be defined.

Larger Projects

As already mentioned, you may split your input file into several parts using multiple source files. This is especially useful when working on large documents, where individual chapters, say, can be segregated. (Moreover, as an aside, if multiple authors are involved, as source files are plaintext, version control systems such as Git can be used for distributed editing while maintaining concurrency.) LaTeX provides two commands which enable you to do this.

\include{filename}

This can be used within the document environment and will insert the contents of said file (viz., the parameter) into your document, where LaTeX will start a new page before processing the included file. Its sister command can be used in the preamble to instruct LaTeX to only input a selection of the included files:

\includeonly{filename,filename,...}

After this command is executed, only \include commands for the filenames listed in the argument of \includeonly will be executed. (Note that there must be no spaces between the filenames and commas.)

The \include command starts typesetting the included text on a new page. This is helpful with \includeonly as, even when some files are omitted, page breaks will not move. However, this may not be desirable; in which case, one may use:

\input{filename}

This simply includes the file specified with no bells or whistles.

Inline Syntax Checking

To make LaTeX quickly check your document, you can use the syntonly package. This causes LaTeX to skim through your document, checking only for proper syntax and command usage, without producing output. LaTeX runs faster in this mode, so may save you time when parsing large documents.

Usage is simple:

\usepackage{syntonly}
\syntaxonly

To produce output, just comment out the second line using a %.

Typesetting Text

Basics

Fonts and Measure

There are a number of typefaces available, by default, in LaTeX. They are accessed using the \textXX command, where XX denotes the particular font code. Text you wish this font to be applied to must proceed the command in a group; type styles may also be combined in this way. (Not all combinations are available in the basic LaTeX document. For example, you must put \usepackage[T1]{fontenc} in the preamble to enable \textbf{\textsc{bold small caps}}.)

This is \textbf{boldface} \\
This is \textit{italic} \\
This is \textrm{Roman} \\
This is \textsc{small caps} \\
This is \textsl{slanted} \\
This is \texttt{typewriter} \\
This is \textbf{\textit{bold-italic}}

There are a great deal of fonts available. While, personally, I am particularly partial to Palatino, the default standard of Computer Modern suffices for most applications. Adobe Type 1 versions of this font are used when generating PDF output, to ensure scalability; some, less common fonts, however, are just bitmapped. Here is an interesting example which you’ll see, on zooming, shows jagged edges:

\newfont{\elvish}{tengwar}
\elvish {
  One ring to rule them all One ring to find them
  One ring to bring them all and in the darkness bind them
}

We may also obtain different sized fonts using various commands, which may also be combined with different font styles:

This normal-sized text is good for general purpose usage.
However, some like things {\large large}, or perhaps {\Large
larger}, and even {\LARGE larger still}. Freud, however, may
have a few things to say if you insisted on {\huge huge}
text. A lot say that {\small small} is beautiful; but we can
get {\footnotesize smaller} and {\scriptsize smaller}, {\tiny
tiny} even.

These font sizes are relative to the document base font size, which defaults to 10pt. Furthermore, rather than using commands, we may also access these sizes using environments, where the size is used as the environment name.

LaTeX Units

You’ll have noticed that we have been referring to absolute font sizes using points as our scale. Additionally, LaTeX provides a number of other units that you may need to use when specifying measurements:

  • mm for millimetres.
  • cm for centimetres.
  • in for inches.
  • pt for typographical points (about 1/72 of an inch).
  • em is the approximate width of an “M” in the current font.
  • ex is the approximate height of an “x” in the current font.

There are also some other, less common, units if you’re in the mood:

  • pc for picas (1/6 of an inch).
  • bp for big points (1/72 of an inch).
  • dd for didôts (1157dd = 1238pt).
  • cc for cîceros (1/12 of a didôt).
  • sp for scaled points (1/65536 of a point).

We may also specify lengths relative to the width of the text on a page, or the width of the text in the current environment. We use the \textwidth and \linewidth commands, respectively, in place of the unit. (There are some other relative measurement commands.)

Spacing

To achieve full justification, LaTeX outputs varying amounts of space between words and slightly more at the end of a sentence to make things more readable. Any exception to LaTeX’s assumptions must be made by the author.

A backslash in front of a space generates a space that will not be enlarged. A tilde generates a space which not only can’t be enlarged, but also prohibits line breaking. The command \@ in front of a full-stop specifies that said full-stop indicates the end of a sentence (rather than a delimiter in an abbreviation, etc.). To avoid the additional spacing in general, use the \frenchspacing command.

If you want to use larger line spacing within your document, you can change the default value using the \linespread command in the preamble. This command takes a parameter, which is a multiplier of the default line spacing. Inline, we may also use the \bigskip, \medskip and \smallskip commands at the end of paragraphs to induce different spacing:

First line.\bigskip

That was a big skip;\medskip

that was a medium skip;\smallskip

and that was a small skip.

That was just plain ol' vanilla line spacing.

There are two parameters influencing paragraph layout, used in the preamble. Here’s an example, which results in no opening paragraph indentation and a pleasing gap between paragraphs:

\setlength{\parindent}{0pt}
\setlength{\parskip}{1ex plus 0.5ex minus 0.2ex}

To indent a paragraph that otherwise wouldn’t be, precede it with the \indent command. Alternatively, to remove a paragraph’s indentation, we have the \noindent command.

For explicit horizontal and vertical spacing, we employ the \hspace and \vspace commands, respectively. They both take one parameter, namely the distance that you wish to span. (Other space-filling commands are available: they usually start with an h or v, for obvious reasons.)

Breaking

In special cases it may be necessary to force LaTeX to break a line. This is done using the \\ or \newline commands, which start a new line without starting a new paragraph. Adding a * to the command also prohibits a page break after said forced line break.

Similarly, we may start a new page using the \newpage command; we also have \linebreak, \nolinebreak, \pagebreak and \nopagebreak commands. These commands take an optional argument: this tells LaTeX how badly (on a scale from zero to four: zero meaning “definitely” and four meaning “ignore”, essentially) you want said command executed. However, if LaTeX thinks better, with regard to page and line breaking, then sobeit!

Hyphenation

LaTeX hyphenates words wherever necessary. If the hyphenation algorithm fails (by putting hyphens in funny places), you may remedy the situation by defining exceptions. The \hyphenation command, used in the preamble, allows you to do this by including your exception list (space separated) as a parameter. To indicate where you want hyphens to occur, use a “-”. For example:

\hyphenation{hy-phen-a-tion COBOL}

would cause the word “hyphenation” to be, if required, hyphenated at the appropriate points, whereas “COBOL” would never be hyphenated (the list is case insensitive). The word list may not contain special characters or symbols.

Inline words, we can use the \- command. This is especially useful for words containing special characters, such as accents, which are not usually hyphenated:

su\-per\-cal\-i\-frag\-i\-lis\-tic\-ex\-pi\-al\-i\-do\-cious

Titles, Chapters and Sections

When writing something non-trivial, you should arrange your document into chapters, sections and subsections. LaTeX supports special commands for dealing with these, which take the section title as their argument. (Note that, for reasons that I don’t comprehend, sectioning is not done with environments, so it is up to you to get the ordering right!)

The following are made available in the article class:

  • \section{...}
  • \subsection{...}
  • \subsubsection{...}
  • \paragraph{...}
  • \subparagraph{...}

The report and book classes also allow \part{...} and \chapter{...}, where the \part command doesn’t influence the numbering of chapters. We also have \appendix, which takes no argument and changes the chapter or section numbering to letters. For sections that don’t require numbering, starred versions of the above commands (e.g., \paragraph*{...}) exist that provide this functionality.

…and Footnotes

The easiest way to add a footnote is to use the \footnote command immediately after the text in question. The text of the footnote should appear as the command’s parameter and then LaTeX will do the hard work of positioning, numbering and paginating. Note that it is considered a typographical rule of thumb to place footnotes after any punctuation (e.g., commas, full-stops, etc.) that appears.

Here is some\footnote{This is a footnote}
text.\footnote{...and another for luck!}

Alignment Environments

By default, your document text is fully justified. However, it is possible for exclusive left or right justification and central alignment using the flushleft, flushright and center (more American spelling) environments, respectively:

\begin{flushleft}I am on the left.\end{flushleft}
\begin{flushright}I am on the right.\end{flushright}
\begin{center}I am in the\\middle.\end{center}

We may also use, for central alignment, the \centerline command; it differs only in that the environment skips a line before and after the paragraph.

Special Environments

Lists

We can make several different types of list: The itemize (with a ‘z’) environment is suitable for simple bullet lists, the enumerate environment for enumerated lists and the description environment for descriptive lists. These environments may be nested and mixed to render a hierarchy.

\begin{enumerate}
  \item Here's our list item
   \begin{itemize}
    \item Second level
     \item Another item
  \end{itemize}
   \item What about another item?
   \begin{description}
    \item[Foo] Have you got the idea yet?
     \item[Bar] Sure you have!
   \end{description}
\end{enumerate}

Quotes, Quotations and Verses

The quote environment is useful for quotes and important phrases. There are two similar environments: quotation and verse. The first being useful for longer quotes spanning several paragraphs (as it doesn’t indent paragraphs); the second for poems, where the line breaks are important (\\ is used at the end of each line and an empty line for a new verse).

A typographical rule of thumb for the line length is:
\begin{quote}
  On average, no line should be longer than 66
  characters.
\end{quote}
This is why \LaTeX{} pages have such large borders by
default and why multicolumn print is used in newspapers.

Verbatim

Text enclosed within the verbatim environment is printed directly, as if it were typed on a typewritter, with all line breaks and spaces (without LaTeX commands being executed). A similar behaviour, used inline paragraphs, is accessed with the \verb command (on using this command, consistent delimiting characters are required on either side of its argument; * and space are not allowed). Asterisks can be appended to both commands to highlight spacing, but note that neither command can be used as a parameter, elsewhere (e.g., you can’t use \verb inside \textbb‘s scope).

The following produces this PDF output:

The \verb+\ldots+ command \ldots

\begin{verbatim}
10 PRINT "HELLO WORD ";
20 GOTO 10
\end{verbatim}
\begin{verbatim*}
The  starred version   emphasises spaces.
\end{verbatim*}

When Boxes Go Bad

When LaTeX attempts to typeset your document, it will try to keep as rigidly to its rules as possible; there are cases, however, when things go awry. If your text cannot be spaced or split appropriately, it may overfill a line; that is, run long. In this case, LaTeX will notify you that an infamous “bad box” error has occurred. Short of rewording your document or using dubious hyphenation, there’s nothing you can really do to fix these.

However, we may make LaTeX slightly less pedantic by using the \sloppy command or sloppypar environment. This may however result in underfill errors, but they don’t look nearly as bad! If, when using the \sloppy command, you wish to return to normality, invoke the \fussy command.

If you use the draft document class, thick black lines, known as slugs, will appear in your output where bad boxes occurred.

Special Characters

Quotes

You should never use the " and ' symbols for quotation marks as you would usually. Microsoft Word and various other word processors will automatically convert these quotes into opening and closing marks; “Smart Quotes” is how they’re usually referred. However, LaTeX (and similarly for TeX) predate this idea, so we must tell it explicitly.

For opening marks we use the grave symbol `; either use one or two, depending on what style of marks we require. To end we use the respective number of apostrophes.

`This ``situation'' takes some getting used to.' is what
most say. Mind you, "regular" quotes look hideous!

Dashes and Hyphens

LaTeX knows of four kinds of dashes. Three of them are accessed using an increasing number of consecutive dashes, the fourth is the mathematical minus sign:

A hyphen has one dash, for example ``The Bolzano-Weierstrass
Theorem''. Two dashes are called an en-dash, which could be
used for ranges such as ``the average human is 2--25 feet
tall''. The em-dash is made with three dashes and has various
uses: ``yes---or no?'' and so on. To get a minus sign, enclose
it in \$ signs, like so ``$-1$''; I'll explain why later!

Miscellany

First of all we introduce a common construction: ellipsis; that is, “…”. In plaintext we would use three full-stops; Word will convert three full-stops into its ellipsis character automatically, but in LaTeX, this doesn’t happen and three dots look, well, rubbish! So there is a special command to accomplish this: \ldots.

Next we have two symbols where, again, we must resort to mathematics mode: They are the tilde and degree symbols. We can, if you remember, use \~ for a tilde, but it floats in the air; sometimes it is more desirable to use $\sim$. The degree symbol is only ever, realistically, going to be used with numbers anyway, so we enclose the whole thing between dollar signs:

$90^{\circ}$C -- dry-clean only

Ligatures

Some letter combinations are typeset not just by setting the different letters one after the other, but by actually using special symbols. It happens most often when two similar letters are next to each other (like fs, is and ls). These are known as ligatures, and can be removed (if you need to) by grouping one of the offending letters by itself. For example:

Mathematicians often use the contraction ``iff''. Sometimes
this looks nicer if we remove the ligature: ``i{f}f''.

Accents

LaTeX supports the use of accents and special characters from many languages. The following table, from “The Not-So-Short Introduction to LaTeX”, shows how to apply the most common:

Any letter, lower or upper case, may have an accent applied to it. If accenting an ‘i’ or ‘j’, it is customary to use dotless versions of these letters: these are available using the commands \i and \j, respectively.

Tables and Tabs

Making Tables

Tables are made with the tabular environment, which takes the following syntax:

\begin{tabular}{columns} options
  first row \\ options
  ...
  last row \\ options
\end{tabular}

Each row (except the last, unless otherwise required) ends with a double backslash. Each column is specified, in the columns parameter, by one of the following:

  • l for left alignment.
  • r for right alignment.
  • c for central alignment.
  • p{width} for justified alignment, where the width must be declared using any scale.

To insert vertical lines between columns, use the pipe symbol | between said column specifications. For horizontal lines, across rows, insert \hline after the double backslash (and at the top of the environment, if required). If you only wish to draw lines across a few rows, we use the \cline{i-j} command, where i and j represent the first and last column numbers to be ruled.

When editing the rows, use the & symbol to inform LaTeX where a new column should start (it’s often helpful, but not necessary, to align the ampersands to increase readability). Let’s take a look at some examples:

\begin{tabular}{lcr}
  left & centre & right \\
  1    & 2      & 3
\end{tabular}
\begin{tabular}{|l|c|r|} \hline
  a & b & c \\ \hline
  one & two & three \\ \hline
\end{tabular}
\begin{tabular}{p{1in}|cc}
  Question & Yes & No \\ \cline{1-1}
  First question & 342 & 12 \\
  Another one & 142 & 96 \\ \cline{2-3}
  & 484 & 108
\end{tabular}

We use the p specification for table columns that are likely to contain a lot of text. If we used one of the other specifiers and the text is too long, it will run off the edge of the page. Similar results to p can be produced with the \parbox command and l specifier. However the \parbox command takes an option defining its placement: t for top, b for bottom, centrally, otherwise. Observe the following:

\begin{tabular}{|l|l|} \hline
  This amount of text is too long to fit on
  one line of the page. & This is column 2 \\ \hline
\end{tabular}
\begin{tabular}{|p{2in}|l|} \hline
  This amount of text is too long to fit on
  one line of the page. & This is column 2 \\ \hline
\end{tabular}
\begin{tabular}{|l|l|} \hline
  \parbox{2in}{This amount of text is too long to fit on
  one line of the page.} & This is column 2 \\ \hline
\end{tabular}
\begin{tabular}{|l|l|} \hline
  \parbox[t]{2in}{This amount of text is too long to fit on
  one line of the page.} & This is column 2 \\ \hline
\end{tabular}

We may use the \multicolumn command to span several columns. It takes three parameters: the number of columns to span (note that you must omit the ampersands for the columns that are consumed), our new cell specification, and the cell’s contents.

We may also change the column separator by using the @{...} construct. This removes the inter-column space and replaces it with whatever’s in the curly braces. This can be used as a hack for aligning decimal expansions about the decimal point.

\begin{tabular}{|c|c|} \hline
  \multicolumn{2}{|c|}{Double Width!} \\ \hline
  Normal & Normal \\ \hline
\end{tabular}
\begin{tabular}{c r @{.} l}
  Variable & \multicolumn{2}{c}{Value} \\ \hline
  a & 314&0 \\
  b &  31&4 \\
  c &   3&14
\end{tabular}

Material typeset with the tabular environment always stays together, on the same page. If you want to typeset long tables, which may span multiple pages, have a look at the supertabular and longtabular packages.

Tabbing

The tabbing environment provides an alternative to the tabular environment by letting you set your own column tabs. We have two basic tabbing commands: \= to define a tab setting; and \>; to move to a tab setting. We also use \\ to end each row, but unlike the tabular environment, the first sentence continues normally, without extra spaces, so that the position of the tab is not equivalent to that of a table’s column.

\begin{tabbing}
  Begin: \=set tab 1\ldots \=set tab 2\\
         \>skip to 1       \>then to 2\\
  \>\>skip to 2
\end{tabbing}

Sometimes we don’t want to have the longest bit of text first, yet it is needed to define the tab stops. This is solved using the \kill command instead of \\, which suppresses the output, but sets the tabs.

\begin{tabbing}
  1-3 \= Sting like a bee \\
  4-6-8 \> Don't be late \\
\end{tabbing}
\begin{tabbing}
  4-6-8 \= Don't be late \kill
  1-3 \> Sting like a bee \\
  4-6-8 \> Don't be late \\
\end{tabbing}

If we wish to explicitly set sizes for our tabs, we can use the \hspace* command, taking a parameter denoting the required width, on a \kill‘d line:

\begin{tabbing}
  \hspace*{.5in} \= \hspace*{2in} \= \kill
  Try \> This \> For Size
\end{tabbing}

Counters and Labels

Concepts

A counter is a variable that refers to something that is being numbered: pages, sections, equations and so on. A label is the identification of a particular counter value and a reference is, as you would probably expect, a citation to a label.

The LaTeX command for labelling a counter is:

\label{labelName}

Placed where the counter’s value is set and the parameter, labelName, is unique in the document. The LaTeX syntax for referencing a label is:

\ref{labelName}

For example, we may set a label like so:

\section{This is A Section} \label{Useless}

You can now refer to this section within the text of your document using the \ref command with the appropriate label (n.b., LaTeX will output a § symbol when using the \S command):

see \S\ref{Useless}

The choice of label is arbitrary and up to you: something relevant is usually a good idea; however, you mustn’t use LaTeX special characters or spaces. A standard convention for labels is to first use a standard prefix such as “sect” or “eqn” (then possibly a delimiter) and use it consistently throughout your document. For example, we could have sectTypesettingText or eqn:TriangleIneq.

There are times when you just need to produce a counter’s value, without a label. This is done using the command:

\theXX

Where XX stands for the counter in question. For example, in all document styles, we have \thepage, \thesection and \thesubsection commands, producing the page, section and subsection numbers, respectively. Alternatively, if you need to use a counter’s value as an argument in a command, use:

\value{counter}

We may also refer to a label’s page number:

page \pageref{thmPythagoras}

Of course, any counter may be used with the \pageref command.

Rolling Your Own

In the preamble you may define your own counter with the \newcounter command:

\newcounter{name}[within]

Where name is the unique name for the counter, which must also be different from any intrinsic counter names. The initial value of the counter is set to zero. You may also define your counter to be a subcounter (i.e., “within” another). For example:

\newcounter{myCounter}[section]

defines myCounter to be within the section counter. That is, myCounter will be reset to zero when entering a new section; resulting in the familiar Dewey decimal output.

Custom counter values are, by default, printed in Arabic numerals, but this can be changed to any of the following:

  • a, b, c, d,… using \alph{myCounter}
  • A, B, C, D,… using \Alph{myCounter}
  • 1, 2, 3, 4,… using \arabic{myCounter}
  • i, ii, iii, iv,… using \roman{myCounter}
  • I, II, III, IV,… using \Roman{myCounter}

We may use the \renewcommand command to alter the apperance of intrinsic (or custom) counters. For example:

\renewcommand{\thepage}{\Roman{page}}

will cause \thepage to use capital Roman numerals. Note that the \renewcommand will alter said command from the point in the document at which it’s called, onwards. To change it back, you must do it manually.

Let’s have a look a less trivial example. The following LaTeX source will generate this PDF output:

\documentclass[10pt,a4paper]{article}

\begin{document}
  \textit{First we use vanilla enumeration:}

  \begin{enumerate}
    \item Introduction
    \item Terms and Concepts
    \begin{enumerate}
      \item Cats and Dogs
      \item Bells and Whistles
    \end{enumerate}
  \end{enumerate}

  \bigskip
  \textit{Then we make some changes:}

  \renewcommand{\theenumi}{\Roman{enumi}}
  \renewcommand{\theenumii}{\Alph{enumii}}  % changes numeral type
  \renewcommand{\labelenumii}{\theenumii.}  % changes appearance

  \begin{enumerate}
    \item Introduction
    \item Terms and Concepts
    \begin{enumerate}
      \item Cats and Dogs
      \item Bells and Whistles
    \end{enumerate}
  \end{enumerate}
\end{document}

Counter values may be increased arbitrarily with the \addtocounter command; simply increased by one with the \stepcounter command; or set to some value with the \setcounter command:

\addtocounter{myCounter}{9} % myCounter + 9
\stepcounter{myCounter}     % myCounter + 1
\setcounter{myCounter}{5}   % myCounter = 5

% Set myCounter to the current page number
\setcounter{myCounter}{\value{page}}

When using a custom counter, we may need to label it for referencing purposes. This is done with the \refstepcounter command, which also increments said counter’s value:

\refstepcounter{myCounter} \label{someLabel}

This can then be referenced/page-referenced in the usual way.

Typesetting Mathematics

Getting Started

LaTeX provides special modes for typesetting mathematics. If a mathematical expression is required inline with the current paragraph text, we enter it between any one of the following:

\( ... \)
$ ... $
\begin{math} ... \end{math}

We may also put our expressions on display; that is, give them their own paragraph, so to speak, making them stand out. This is done using any one of these:

\[ ... \]
$$ ... $$
\begin{displaymath} ... \end{displaymath}

Conventionally,the single dollar signs are used for inline equations and the displaymath environment is used for prominent equations; however, this is entirely down to personal choice. There is also the equation environment, which LaTeX numbers for us automatically.

Here is an example; we’ll go over how it, and more, are done over the coming sections:

Grouping

Grouping was introduced right at the beginning. It is used in exactly the same way in TeX’s mathematics modes, but is  of particular importance here. When constructing expressions, often only the first character following a control sequence (command) is affected. In which case, if this is not our will, we must use grouping to tell LaTeX what other text must be considered.

For example, in the following, we might expect the same output. Try it for yourself:

$e^2x$ and $e^{2x}$

Groups also help with the logical structure of your code, which will make things a little easier to read; especially if well spaced, as spaces are ignored. Often the best way to produce the desired results is to imagine groupings as liberally-used parentheses, sorting out the structure.

Spacing

As previously stated, spaces within expressions are largely ignored: TeX figures it out for itself. However, if it’s unsatisfactory, the spacing can be adjusted by inserting the following into your expression, wherever they’re needed:

  • \, for 3/18em;
  • \: for 4/18em;
  • \; for 5/18em;
  • \ (that is, a backslash followed by a space) for a medium space;
  • \quad for 1em;
  • \qquad for large spaces;
  • \! shrinks a gap by 3/18em.

The \! command is often used with multiple integral signs as TeX inserts a rather large gap between them. However, alternative (prefabricated) multiple integral signs exist in the amsmath package, for example.

Style

When writing mathematical expressions in documents, one must take care over how the text reads. It is a common mistake to just idly stick formulae here, there and everywhere, without taking into account punctuation, grammar or the semantics of its positioning.

This is perhaps a tad pedantic, but if you’re going to go to the trouble of using LaTeX, you’d might as well go out with a bang! All you need do is treat expressions linguistically whilst writing and be somewhat more discerning when proof-reading. For example, perhaps the most abusive use of LaTeX is when referring to variables, etc. within your paragraph-text and not using maths mode. That is:

In the previous equation, x takes the value y

You may not see a problem with this, but the thing is variables (in maths mode) are typeset differently to normal text. For a truly beautiful document, the above should be:

In the previous equation, $x$ takes the value $y$

Expression Syntax

It’s All Greek to Me

We mathematicians like our variables, we also like to pay homage to the guys who started the ball rolling (in a manner of speaking)! As such Greek characters are used prolifically in mathematics, so LaTeX makes it easy for us to get at them.

The Greek minuscules are accessed, in mathematics mode only, just by typing a backslash and then the letter’s name (e.g., \alpha). Well, almost: an omicron looks just like an o, so a solitary “o” suffices. There are also a few variant minuscules, for those who write them differently.

As for majuscules, a lot of the capital Greek alphabet, like the lower case omicron, look like their Latin counterparts (or some Latin letter). Thus, only the required majuscules are defined. These are accessed in the same way, except that the first letter is capitalised (e.g., \Omega).

This incompleteness is said to change in upcoming versions. Here is a more comprehensive list:

\alpha                 A
\beta                  B
\gamma                 \Gamma
\delta                 \Delta
\epsilon  \varepsilon  E
\zeta                  Z
\eta                   H
\theta    \vartheta    \Theta
\iota                  I
\kappa                 K
\lambda                \Lambda
\mu                    M
\nu                    N
\xi                    \Xi
o                      O
\pi       \varpi       \Pi
\rho      \varrho      P
\sigma    \varsigma    \Sigma
\tau                   T
\upsilon               \Upsilon
\phi      \varphi      \Phi
\chi                   X
\psi                   \Psi
\omega                 \Omega

Exponents and Subscripts

Exponents and subscripts are specified using the ^ and _ characters, respectively. They may be grouped and nested arbitrarily:

$a_0$ $x^2$ $e^{-x^2}$

As Limits

There are a few mathematical operations that take upper and/or lower limits. To define these limits, we use the exponent and subscript codes, as outlined above, appended to said operation. Examples of these operations include summations (\sum), products (\prod), integrals (\int) and so on.

Anything can be put as an exponent or subscript; LaTeX doesn’t know better, so assumes you know what you’re doing. (Note that the infinity symbol, often used with limits, is accessed with \infty.)

$$ \sum_{i=1}^{n} $$
$$ \int_{0}^{\frac{\pi}{2}} $$
$$ \prod_\epsilon $$
$$ \int\!\!\!\int_{\delta A} $$

Constructs

Fractions

A regular fraction is typeset with the \frac command; taking two parameters: the numerator and denominator, respectively. If these are small — in terms of amount of typographic data, rather than value — we may prefer to use the vulgar fraction, typeset with a forward slash between the numerator and denominator. We may of course use complicated expressions, including other fractions, in \frac‘s parameters.

I'm only $3\frac{1}{2}$ years old.
$x^{2/3}$

Roots

The square root is entered as \sqrt; with the nth root generated by \sqrt[n]. Its size is determined automatically by LaTeX, but if you just want the sign, use \surd.

$\sqrt{x}$
$\sqrt{ x^{2}+\sqrt{y} }$
$\sqrt[3]{2}$
$\surd[x^2 + y^2]$

Operators and Relations

As you can imagine, there are hundred of possible operators and relations. Everything from simple binary operations and comparison relations, to set operations and logic relations. You name it, LaTeX (or some package) has got it! Said functions are simply typeset with their corresponding LaTeX command; although, as I say, some require additional packages to be loaded. Here’s a pretty extensive list to get you started! (See the appendix for an even more comprehensive list.)

Parentheses et al.

TeX provides many symbols that may be used as braces or delimiters. Round and square braces can be entered using the corresponding keys and curly braces with \{; remaining delimiters are generated with special commands:

Parentheses          (        )
Square brackets      [        ]
Curly braces         \{       \}
Angle brackets       \langle  \rangle
Floor                \lfloor  \rfloor
Ceiling              \lceil   \rceil
Line (e.g., mod)     |        |
2 Line (e.g., norm)  \|       \|

If you use the command \left in front of an opening delimiter and \right in front of a closing delimiter, TeX will automatically determine their sizes. Note, however, that there must be as many \lefts are there are \rights and they must be typeset on the same line in the output.

Occasionally, TeX gets it wrong and you have to correct the delimiter size manually. This is done by preceeding the offending delimiter with one of \big, \Big, \bigg or \Bigg.

The delimiters don’t have to match (nor, necessarily, be appropriate opening/closing delimiters). Moreover, if we only wanted a one sided brace (and used the \left and \right commands) we use a full-stop to indicate an invisible, “phantom” delimiter.

$A=\left\{1+\left(\frac{1}{1-x^2}\right)^3\ \bigg|\ x>4\right\}}$

Decorations

We may add various appendages to our expressions:

Horizontal lines, above and below, are typset using the \overline and \underline commands, respectively. For long horizontal curly braces we have \overbrace and \underbrace. Vectors can be set with \overleftarrow and \overrightarrow, or for a singleton, \vec suffices. Wide hats and tildes are accessed with \widehat and \widetilde. Finally, of course, there’s the ubiquitous prime (or multiple primes), typset with as many apostrophes as required.

$\overline{x + y} = \underline{x + y}$
$\overbrace{a_0 + \cdots + a_n}^{n+1}$
$\vec a\quad\overrightarrow{AB}$
$\widehat{abc} \neq \widetilde{abc}$
$y'' + 2y' + 3 = 0$

We also have a variety of maths mode accents, which are different from the regular accents. The following are taken from “The Not-So-Short Introduction to LaTeX”:

Symbol Stacking

We can also stack symbols on top of others using the \stackrel command. It takes two parameters, the first being the top symbol on the stack and the second the base symbol. For example:

$a \stackrel{?}{=} b$

As always, you can nest stacks to an arbitrary degree of perversity.

Functions

There are a number of defined common function commands, which output said function name in an upright font to distinguish it from a variable (which are set in italics). If your favourite funtion is not predefined, you may revert to an upright font using the \mathrm (or other, relevant font) command (see the next section for details).

\cos     \sin     \tan
\arccos  \arcsin  \arctan
\sec     \csc     \cot
\cosh    \sinh    \tanh    \coth

\lim     \liminf  \limsup
\min     \max     \inf     \sup

\ln      \log     \lg      \exp

\det     \deg     \dim     \gcd
\arg     \hom     \ker

For example (here’s another one of those constructions that take limits):

$$\lim_{x \rightarrow 0} \frac{\sin x}{x} = 1$$

Further Formatting

Bold Symbols

Bold symbols in mathematics mode are reasonably hard to get hold of from within LaTeX, due to their abusive overuse. We may use \mathbf, but it uses Roman characters, which are upright rather than in italics; there’s also \boldmath, which must be called before entering maths mode (it has the effect of making the entire expression bold, not just your symbols).

However there is a solution to our dilemma. The amsbsy package (which is included by amsmath) provides the \boldsymbol command:

$\boldsymbol{A} = (1, 2, 3)$

Fonts

There are a few alternative fonts available for typesetting mathemathics: they provide script-like, Germanic and open-faced forms. Note, however, that some fonts require additional packages to be loaded. The following is taken from “The Not-So-Short Introduction to LaTeX”:

Arrays and Alignment

Matrices and Vectors

To typeset matrices or vectors (or any other kind of arrayed data) we make use of the array environment. It, rather happily, works in the same way as the tabular environment we met earlier:

\begin{displaymath}
  \boldsymbol{A} = \left(
  \begin{array}{ccc}
    a_{11} & a_{12} & \ldots \\
    a_{21} & a_{22} & \ldots \\
    \vdots & \vdots & \ddots
  \end{array}\right)
\end{displaymath}

Here’s an example where we need to use an invisible delimiter; we also use \textrm, which differs from \mathrm in that it temporarily reverts us to normal text mode.

\begin{displaymath}
  \delta_i(x) = \left\{
  \begin{array}{ll}
    1 & \textrm{if } x=i \\
    0 & \textrm{otherwise}
  \end{array}\right.
\end{displaymath}

Equations

For formulae or derivations spanning several lines, we may use the eqnarray and eqnarray* environments, instead of equation. The starred version does not number your equations, whereas the vanilla version will number every line. To veto numbering, at any line, we invoke the \nonumber command before inducing a line break.

These environments are essentially aliases for the array environment, with the column specifications set to rcl. The middle column is usually left for binary relations whereas the left and right are the relevant hand sides.

\begin{eqnarray}
  f(x)  & = & \cos x \\
  f'(x) & = & -\sin x
\end{eqnarray}

\begin{eqnarray}
  \sin x & = & x - \frac{x^3}{3!} + \nonumber \\
  &   & + \frac{x^5}{5!} - \cdots
\end{eqnarray}

Theorems and the Like

When writing mathematical publications, you’ll almost certainly need a method of typesetting definitions, lemmas, theorems, corollaries and the like. LaTeX supports this with the command:

\newtheorem{name}[counter]{text}[section]

This should be declared, as many times (making appropriate changes) as are necessary, in your document’s preample. The name parameter is a short keyword used to define the theorem, or whatever; when we come to use it, we use the name environment for access. The text parameter declares the name; that is, what kind of theorem we are dealing with. The optional parameters are used to specify numbering: The counter argument lets you specify how your theorem is numbered with respect to another theorem; and the section argument specifies numbering relative to your document’s sectioning.

(Note that I use “theorem” for want of a better, all-encompassing word. In the above, it applies to definitions, lemmas, corollaries and whatever else you like: such as bananas, tadpoles or even 19th century impressionist portraits.)

As stated, when all this is done, to access your theorem, we use the following environment construction:

\begin{name}[title]
  ...
\end{name}

Where name refers to the relevant class of theorem, and title gives an optional title.

To better illustrate this functionality, this LaTeX source generates this PDF output.

Specialities

Title Page and Abstract

We have already seen, briefly, the \maketitle command: it may be called anywhere, although it’s probably best to do it at the very beginning of your document! You may, if you like, enclose it within the titlepage environment. If you are using the article document class, title pages aren’t de facto, so we can supply the titlepage option in the document class definition: this gives us a dedicated title page when we call the \maketitle command (we may otherwise still call this command).

To make the title, we need some information. These are supplied with various commands that must be called sometime before invoking the \maketitle command; usually one puts them in the preample:

  • \title{...} supplies the document title.
  • \author{...} supplies the document’s author. If there were multiple authors, we use the \and command to separate them.
  • \date{...} supplies the document’s date; if it is omitted, the current date, according to your computer’s clock, is used.

To write an abstract for your document, we use the abstract environment where appropriate. Note that the article class apparently doesn’t define the abstract environment; it needs the titlepage option in the class declaration. Personally, I like to include the abstract as part of the title page — which is why I’ve put them together in this one section — although, this messes up the page numbering, which can be fixed by using \thispagestyle{empty} within the titlepage environment.

Here are two examples, so you can make your own decision! The first generates this PDF output, and the second this.

\documentclass[10pt,a4paper]{article}

\title{A Simple Example}
\date{1066} % You can write anything in here

\begin{document}
  \begin{titlepage}
    \maketitle
    \vfill

    \begin{abstract}
      Here is a synopsis.
    \end{abstract}
  \end{titlepage}

  Here is some document.
\end{document}
\documentclass[10pt,a4paper,titlepage]{article}

\title{A Simple Example}
\author{Me \and Another Guy}

\begin{document}
  \maketitle

  \begin{abstract}
    Here is a synopsis.
  \end{abstract}

  Here is some document.
\end{document}

Table of Contents

The \tableofcontents command makes, as you would probably guess, a table of contents; which is placed wherever you invoke the command (straight after the cover page seems to be the fashion of the time).

The contents includes numbered parts (or otherwise, if the starred versions of said commands were used), such as sections and subsections. To add other front matter, LaTeX provides the \addcontentsline command. It takes two parameters of interest to us: firstly toc to tell LaTeX that said front matter should be part of the contents (as opposed to the table of figures or tables); then another telling LaTeX how to format the entry in the table (e.g. like a section or subsection, etc.).

Note that it is conventional to use different page numbering for the front matter of a document; traditionally Roman numerals. We do this by using the \pagenumbering{roman} command at the start of our front matter. Then, to revert to normality, \pagenumbering{arabic} when we’re done (usually preceded by \newpage).

LaTeX also provides functionality for creating an index. We leave this as an exercise for the reader!

Bibliography and References

You can produce a simple bibliography with the thebibliography environment. For larger projects, you may wish to have a look at the BibTeX package, included with most TeX distributions. (Having used BibTeX extensively as a postgraduate, I will probably do a follow-up post on its usage, soon.)

Within the document, one uses the \cite command, along with a parameter for identification purposes (LaTeX numbers automatically). Then we use the environment where appropriate, using the \bibitem command to mark entries with their corresponding identifiers. For example:

This example was largely copied~\cite{NSSITL}; but what
do you expect!?

\begin{thebibliography}{9}
  \bibitem{NSSITL}T. Oetiker; H. Partl; I. Hyna; E. Schlegl:
    \textit{The Not-So-Short Introduction to \LaTeX}
    (2001)
\end{thebibliography}

The parameter to the thebibliography environment tells LaTeX not to expect any bibliography reference number to be wider — in a typographical sense — than, in the above case, “9″. This is simply so LaTeX can set the tabs correctly.

Customisation

Page Layout

Before making any rash decisions, I urge you not to mess with the page layout. As we have discussed earlier, this has been designed to offer the best trade off between aesthetics and readability. However, if you must, there are commands to do so.

Two in particular: \setlength and \addtolength. These commands take two parameters, the first being the length you wish to change (see below), and the second being the new length. This new length is applied absolutely, with regard to the first command, or relatively for the second.

Here are some various common lengths that you may change, from our favourite reference:

  1. one inch + \hoffset
  2. one inch + \voffset
  3. \evensidemargin
  4. \topmargin
  5. \headheight
  6. \headsep
  7. \textheight
  8. \textwidth
  9. \marginparsep
  10. \marginparwidth
  11. \footskip

Custom Commands

We may make our own commands or alter the properties of an existing command with the \newcommand and \renewcommand commands, respectively.

The \newcommand first takes as a parameter your new command’s name, then an optional number of arguments (to refer to arguments, you use hashes followed by the argument number) and finally the operation of said command. This can be a very useful way of defining aliases for complicated or common expressions. Note that some commands are specifically for use in maths mode; if we want to use them anywhere, we may use the \ensuremath command.

Here are a few examples:

\newcommand{\ul}{\underline}
\newcommand{\R}{\ensuremath{\mathbb{R}}}
\newcommand{\myRef}[3]{#1: \textit{#2} (#3)}

The first example, \ul, provides an alias for underlining. We might use it like so:

\ul{something}

The second is a somewhat more useful example. It provides a convenient alias to the symbol for the real numbers. It can be used, within maths mode or text mode, by just writing \R. We may, of course, define aliases to the other standard sets in a similar way. (Remember, though, that the appropriate font/symbol package must be loaded into your document for this to work.)

The final example takes three arguments and formats them as shown (the first parameter, followed by a colon, then the second in italics, finishing with the third within parentheses). This, as its name might suggest, could be used within your document’s bibliography:

\begin{thebibliography}{9}
  \bibitem{XW} \myRef{Xophmeister's World}{LaTeX Tutorial}{2004, 2011}
\end{thebibliography}

Defining new commands with the same name as LaTeX commands will result in an error; you’ll have to think of a different name. However, we can alter the behaviour of defined commands: We have already seen, briefly, an example of \renewcommand to alter the enumeration of lists. We can do more! For example, the following will change what’s written as the header for a document’s abstract (in this case “Synopsis”, instead of “Abstract”):

\renewcommand{\abtractname}{Synopsis}

I leave it you to experiment!

Custom Environments

We may also create our own environments with the \newenvironment command (or, similarly, modify others with \renewenvironment). It is used in almost exactly the same way as the custom command commands, but the final parameter is split in two: specifying what to do at the beginning and end of the environment, respectively.

For example, the following defines a proof environment (taking no options; although this is generally possible) that is similar in appearance to the output of environments created with \newtheorem and ending with an “end of proof” symbol:

\newenvironment{proof}
 {\bigskip\noindent\textbf{Proof~}\itshape}
 {\marginpar{$\Box$}\bigskip}

This requires the latexsym package for the Halmos mark (if you prefer a filled square or “QED”, you can change the environment definition appropriately). We can now use this in the usual way:

\begin{proof}
  Baddabing!
\end{proof}

\end{document}

I leave you with the a mirror of The Comprehensive LaTeX Symbol Reference, created by Scott Pakin and otherwise available on CTAN (see below). It contains every symbol — and its associated LaTeX command — you could ever possibly need:

Finally, we provide some TeX and LaTeX resources found on the Internet. Hopefully they will prove useful. Otherwise, I wish you a rich and fulfilling TeX life!

Online Resources

General Information

(La)TeX Distributions

IDEs and Editors

References and Guides

posted on 2012-03-06 18:52  Lvpengms  阅读(778)  评论(0编辑  收藏  举报