LaTeX/Formatting
|
|
A Wikibookian suggests that this book or chapter be merged with LaTeX/Numbering. Please discuss whether or not this merger should happen on the discussion page. |
|
|
A Wikibookian believes this page should be split into smaller pages with a narrower subtopic. You can help by splitting this big page into smaller ones. Please make sure to follow the naming policy. Dividing books into smaller sections can provide more focus and allow each one to do one thing well, which benefits everyone. |
This section will guide you through the various text, paragraph, and page formatting techniques. Formatting tends to refer to most things to do with appearance, so it makes the list of possible topics quite eclectic: text style, font, size; paragraph alignment, interline spacing, indents; special paragraph types; footnotes, margin notes, etc.
A lot of the formatting techniques are required to differentiate certain elements from the rest of the text. It is often necessary to add emphasis to key words or phrases. Footnotes are useful for providing extra information or clarification without interrupting the main flow of text. So, for these reasons, formatting is very important. However, it is also very easy to abuse, and a document that has been over-done can look and read worse than one with none at all.
Contents
|
[edit] Text formatting
[edit] Spacing
[edit] Line Spacing
If you want to use larger inter-line spacing in a document, you can change its value by putting the
\linespread{factor}
command into the preamble of your document. Use \linespread{1.3} for "one and a half" line spacing, and \linespread{1.6} for "double" line spacing. Normally the lines are not spread, so the default line spread factor is 1.
The setspace package allows more fine-grained control over line spacing. To set "one and a half" line spacing document-wide, but not where it is usually unnecessary (e.g. footnotes, captions):
\usepackage{setspace} %\singlespacing \onehalfspacing %\doublespacing %\setstretch{1.1}
To change line spacing within the document, the setspace package provides the environments singlespace, onehalfspace, doublespace and spacing:
This paragraph has \\ default \\ line spacing. \begin{doublespace} This paragraph has \\ double \\ line spacing. \end{doublespace} \begin{spacing}{2.5} This paragraph has \\ huge gaps \\ between lines. \end{spacing}
[edit] Horizontal Space
LaTeX determines the spaces between words and sentences automatically. To add horizontal space, use:
\hspace{length}
If such a space should be kept even if it falls at the end or the start of a line, use \hspace* instead of \hspace. The length in the simplest case is just a number plus a unit, e.g. \hspace{1.5 cm}. For a list of the possible units, see the Useful Measurement Macros appendix.
The command:
\stretch{n}
generates a special rubber space. It stretches until all the remaining space on a line is filled up. If two \hspace{\stretch{n}} commands are issued on the same line, they grow according to the stretch factor.
x\hspace{\stretch{1}} x\hspace{\stretch{3}} x |
x x x |
[edit] Vertical Space
The space between paragraphs, sections, subsections, etc. is determined automatically by LaTeX. If you want to customize the default paragraph spacing, it can be achieved with the following command in the preamble of your document:
\parskip 7.2pt
If necessary, additional vertical space between two paragraphs can be added with the command:
\vspace{length}
This command should normally be used between two empty lines. If the space should be preserved at the top or at the bottom of a page, use the starred version of the command, \vspace*, instead of \vspace. The \stretch command, in connection with \pagebreak, can be used to typeset text on the last line of a page, or to center text vertically on a page.
Additional space between two lines of the same paragraph or within a table is specified with the
\\[length]
command.
If you want to add space at the beginning of the document, without anything else written before, then you may use
{ \vspace*{length} }
It's important you use the \vspace* command instead of \vspace, otherwise LaTeX can silently ignore the extra space.
[edit] Fill the rest of the line
Several macros allow filling the rest of the line -- or stretching parts of the line -- in different manners:
- \hfill will produce spaces.
- \dotfill will produce dots.
- \hrulefill will produce a rule.
[edit] Hyphenation
LaTeX hyphenates words whenever necessary. If the hyphenation algorithm does not find the correct hyphenation points, you can remedy the situation by using the following commands to tell TeX about the exception. The command
|
\hyphenation{word list} |
causes the words listed in the argument to be hyphenated only at the points marked by “-”. The argument of the command should only contain words built from normal letters, or rather characters that are considered to be normal letters by LaTeX. It is known that the hyphenation algorithm does not find all correct American English hyphenation points for several words. A log of known exceptions is published periodically in the TUGboat journal. (See a 2008 list: http://www.tug.org/TUGboat/Articles/tb29-2/tb92hyf.pdf)
The hyphenation hints are stored for the language that is active when the hyphenation command occurs. This means that if you place a hyphenation command into the preamble of your document it will influence the English language hyphenation. If you place the command after the \begin{document} and you are using some package for national language support like babel, then the hyphenation hints will be active in the language activated through babel. The example below will allow “hyphenation” to be hyphenated as well as “Hyphenation”, and it prevents “FORTRAN”, “Fortran” and “fortran” from being hyphenated at all. No special characters or symbols are allowed in the argument. Example:
|
\hyphenation{FORTRAN Hy-phen-a-tion} |
The command \- inserts a discretionary hyphen into a word. This also becomes the only point where hyphenation is allowed in this word. This command is especially useful for words containing special characters (e.g., accented characters), because LaTeX does not automatically hyphenate words containing special characters.
|
\begin{minipage}{2in} |
|
This can be quite cumbersome if one has many words that contain a dash like electromagnetic-endioscopy. One alternative to this is using the \hyp command of the hyphenat package. This command typesets a hyphen and allows full automatic hyphenation of the other words forming the compound word. One would thus write
|
electromagnetic\hyp{}endioscopy |
Several words can be kept together on one line with the command
|
\mbox{text} |
It causes its argument to be kept together under all circumstances. Example:
|
My phone number will change soon. It will be \mbox{0116 291 2319}. |
\fbox is similar to \mbox, but in addition there will be a visible box drawn around the content.
To avoid hyphenation altogether, the penalty for hyphenation can be set to an extreme value:
|
\hyphenpenalty=100000 |
You can change the degree to which LaTeX will hyphenate by changing the value of \tolerance=1000 and \hyphenpenalty=1000. You'll have to experiment with the values to achieve the desired effect. A document which has a low tolerance value will cause LaTeX not to tolerate uneven spacing between words, hyphenating words more frequently than in documents with higher tolerances. Also note that using a higher text width will decrease the probability of encountering badly hyphenated word. For example adding
|
\usepackage{geometry} |
will widen the text width and reduce the amount of margin overruns.
[edit] Quote-marks
Latex treats left and right quotes as different entities. For single quotes, ` (on American keyboards, this symbol is found on the tilde key (adjacent to the number 1 key on most) gives a left quote mark, and ' is the right. For double quotes, simply double the symbols, and Latex will interpret them accordingly. (Although, you can use the " for right double quotes if you wish). On British keyboards, ' ` ' is left of the ' 1 ' key and shares the key with ' ¬ ', and sometimes ' ¦ ' or ' | '. The apostrophe (' ' ') key is to the right of the colon/semicolon key and shares it with the ' @ ' symbol.
|
To `quote' in Latex |
|
|
To ``quote'' in Latex |
|
|
To ``quote" in Latex |
|
|
To ,,quote'' in Latex |
|
|
``Please press the `x' key.'' |
|
|
,,Proszę, naciśnij klawisz <<x>>''. |
The right quote is also used for apostrophe in Latex without trouble.
For left bottom quote and European quoting style you need to use T1 font encoding enabled by:
|
\usepackage[T1]{fontenc} |
The package csquotes offers a multi-lingual solution to quotations, with integration to citation mechanisms offered by BibTeX. This package allows one for example to switch languages and quotation styles according to babel language selections.
[edit] Diacritics and accents
Diacritics may be added to letters by placing special escaped metacharacters before the letter that requires the diacritic. For a list of diacritic metacharacters, see LaTeX/Accents.
[edit] Space between Words and Sentences
To get a straight right margin in the output, LaTeX inserts varying amounts of space between the words. By default, It also inserts slightly more space at the end of a sentence. However, the extra space added at the end of sentences is generally considered typographically old-fashioned in English language printing. (The practice is found in nineteenth century design and in twentieth century typewriter styles.) Most modern typesetters treat the end of sentence space the same as the interword space. (See for example, Bringhurst's Elements of Typographic Style.) The additional space after periods can be disabled with the command
\frenchspacing
which tells LaTeX not to insert more space after a period than after ordinary character. Frenchspacing can be turned off later in your document via the \nonfrenchspacing command.
If an author wishes to use the wider end-of-sentence spacing, care must be exercised so that punctuation marks are not misinterpreted as ends of sentences. TeX assumes that sentences end with periods, question marks or exclamation marks. Although if a period follows an uppercase letter, this is not taken as a sentence ending, since periods after uppercase letters normally occur in abbreviations. Any exception from these assumptions has to be specified by the author. A backslash in front of a space generates a space that will not be enlarged. A tilde ‘~’ character generates a space that cannot be enlarged and additionally prohibits a line break. The command \@ in front of a period specifies that this period terminates a sentence even when it follows an uppercase letter. (If you are using \frenchspacing, then none of these exceptions need be specified.)
[edit] Margin misalignment and interword spacing
Some very long words, numbers or URLs may not be hyphenated properly and move far beyond the side margin. One solution for this problem is to use sloppypar environment, which tells LaTeX to adjust word spacing less strictly. As a result, some spaces between words may be a bit too large, but long words will be placed properly.
[edit] Ligatures
Some letter combinations are typeset not just by setting the different letters one after the other, but by actually using special symbols (like "ff"), called ligatures. Ligatures can be prohibited by inserting {} or, if this does not work, {\kern0pt} between the two letters in question. This might be necessary with words built from two words. Here is an example:
|
\Large Not shelfful\\ |
Some tools are unable to perform search in documents that contain ligatures (a search for "finally" wouldn't find the string "finally"). If one desires, for greater accessibility, to disable ligatures altogether in the whole document, the \DisableLigatures from the microtype package can be used:
\usepackage{microtype} \DisableLigatures{encoding = *, family = *}
Note that this will also disable ligatures such as -- → –, --- → —, etc.
If you are using XeLaTeX and OpenType fonts, the fontspec package allows for standard ligatures to be turned off as well as fancy swash ligatures to be turned on.
Another solution is to use the cmap package, which will help the reader to interpret the ligatures:
\usepackage[resetfonts]{cmap}
[edit] Slash marks
The normal typesetting of the / character in LaTeX does not allow following characters to be "broken" on to new lines, which often create "overfull" errors in output (where letters push off the margin). Words that use slash marks, such as "input/output" should be typeset as "input\slash output", which allow the line to "break" after the slash mark (if needed). The use of the / character in LaTeX should be restricted to units, such as "mm/year", which should not be broken over multiple lines.
[edit] Emphasizing Text
In order to add some emphasis to a word or phrase, the simplest way is to use the \emph{text} command.
|
I want to \emph{emphasize} a word. |
[edit] Fonts
- See also: LaTeX/Fonts.
In LaTeX, there are many ways to specify and control font, and this section is only intended to serve as a brief overview of the topic.
[edit] Font Styles
There are three main font families: roman (e.g., Times), sans serif (e.g., Arial) and monospace (e.g., Courier). You can also specify styles such as italic and bold.
The following table lists the commands you will need to access the typical font styles:
| LaTeX command | Equivalent to | Output style | Remarks |
|---|---|---|---|
\textnormal{…} |
{\normalfont …} |
document font family | this is the default or normal font |
\emph{…} |
{\em …} |
emphasis | typically italics |
\textrm{…} |
{\rmfamily …} |
roman font family | |
\textsf{…} |
{\sffamily …} |
sans serif font family | |
\texttt{…} |
{\ttfamily …} |
teletypefont family | this is a fixed-width or monospace font |
\textup{…} |
{\upshape …} |
upright shape | the same as the normal typeface |
\textit{…} |
{\itshape …} |
italic shape | |
\textsl{…} |
{\slshape …} |
slanted shape | a skewed version of the normal typeface (similar to, but slightly different from, italics) |
\textsc{…} |
{\scshape …} |
Small Capitals | |
\uppercase{…} |
uppercase (all caps) | Also \lowercase. There are some caveats, though; see here. |
|
\textbf{…} |
{\bfseries …} |
bold | |
\textmd{…} |
{\mdseries …} |
medium weight | a font weight in between normal and bold |
The commands in column two are not entirely equivalent to the commands in column one: They do not correct spacing after the selected font style has ended. The commands in column one are therefore in general recommended.
You may have noticed the absence of underline. This is because underlining is not recommended for typographic reasons (it weighs the text down). You should use emph instead. Although this is available via the \underline{...} command, text underlined in this way will not break properly. This functionality has to be added with the ulem (underline emphasis) package. Stick \usepackage{ulem} in your preamble. By default, this overrides the \emph command with the underline rather than the italic style. It is unlikely that you wish this to be the desired effect, so it is better to stop ulem taking over \emph and simply call the underline command as and when it is needed.
- To restore the usual
emformatting, add \normalem straight after the document environment begins. Alternatively, use \usepackage[normalem]{ulem}. - To underline, use \uline{...}.
- To add a wavy underline, use \uwave{...}.
- And for a strike-out \sout{...}.
Some font styles are not compatible one with the other. But some extra packages will fill this hole. For bold small capitals, you might want to use:
\usepackage{bold-extra} ... \textsc{\textbf{This is bold small capitals}}
[edit] Sizing text
To apply different font sizes, simply follow the commands on this table:
Note that the font size definitions are set by the document class. Depending on the document style the actual font size may differ from that listed above. And not every document class has unique sizes for all 10 size commands.
| size | standard classes, proc | AMS classes, memoir | slides | beamer | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| [10pt] | [11pt] | [12pt] | [10pt] | [11pt] | [12pt] | [10pt] | [11pt] | [12pt] | ||
| \tiny | 6.80565 | 7.33325 | 7.33325 | 7.33325 | 7.97224 | 8.50012 | 17.27505 | 5.31258 | 6.37509 | 6.37509 |
| \scriptsize | 7.97224 | 8.50012 | 8.50012 | 7.97224 | 8.50012 | 9.24994 | 20.73755 | 7.43760 | 8.50012 | 8.50012 |
| \footnotesize | 8.50012 | 9.24994 | 10.00002 | 8.50012 | 9.24994 | 10.00002 | 20.73755 | 8.50012 | 9.24994 | 10.00002 |
| \small | 9.24994 | 10.00002 | 10.95003 | 9.24994 | 10.00002 | 10.95003 | 20.73755 | 9.24994 | 10.00002 | 10.95003 |
| \normalsize | 10.00002 | 10.95003 | 11.74988 | 10.00002 | 10.95003 | 11.74988 | 24.88382 | 10.00002 | 10.95003 | 11.74988 |
| \large | 11.74988 | 11.74988 | 14.09984 | 10.95003 | 11.74988 | 14.09984 | 29.86258 | 11.74988 | 11.74988 | 14.09984 |
| \Large | 14.09984 | 14.09984 | 15.84985 | 11.74988 | 14.09984 | 15.84985 | 35.82510 | 14.09984 | 14.09984 | 16.24988 |
| \LARGE | 15.84985 | 15.84985 | 19.02350 | 14.09984 | 15.84985 | 19.02350 | 43.00012 | 16.24988 | 16.24988 | 19.50362 |
| \huge | 19.02350 | 19.02350 | 22.82086 | 15.84985 | 19.02350 | 22.82086 | 51.60014 | 19.50362 | 19.50362 | 23.39682 |
| \Huge | 22.82086 | 22.82086 | 22.82086 | 19.02350 | 22.82086 | 22.82086 | 51.60014 | 23.39682 | 23.39682 | 23.39682 |
As a technical note, points in TeX follow the standard American point size in which 1 pt is approximately 0.3513_6 mm. The standard point size used in most modern computer programs (known as the desktop publishing point or PostScript point) has 1 pt equal to approximately 0.352_7 mm while the standard European point size (known as the Didot point) had 1 pt equal to approximately 0.37597151 mm. (See: w:Point_(typography).)
Even if you can easily change the output of your fonts using those commands, you're better off not using explicit commands like this, because they work in opposition to the basic idea of LaTeX, which is to separate the logical and visual markup of your document. This means that if you use the same font changing command in several places in order to typeset a special kind of information, you should use \newcommand to define a "logical wrapper command" for the font changing command.
|
\newcommand{\oops}[1]{\textbf{#1}} |
Do not enter this room, it’s occupied by machines of unknown origin and purpose. |
This approach has the advantage that you can decide at some later stage that you want to use some visual representation of danger other than \textbf, without having to wade through your document, identifying all the occurrences of \textbf and then figuring out for each one whether it was used for pointing out danger or for some other reason.
[edit] Text mode superscript and subscript
To superscript text in text-mode, you can use the \textsuperscript{} command. This allows you to, for instance, typeset 6th as 6th:
Michelangelo was born on March 6\textsuperscript{th}, 1475.
A very common use of subscripts within the text environment is to typeset chemical formulae. For this purposes, a highly recommended package is mhchem. This package is easy to use, and works with your text fonts (rather than math fonts). To insert a chemical formula, use \ce{} with the text-equivalent formula, for example:
|
% In your preamble, add: |
Subscripting in text-mode is not supported by LaTeX alone; however, several packages allow the use of the \textsubscript{} command. For instance, bpchem, KOMA-Script2, and fixltx2e all support this command. Of these, fixltx2e is perhaps the most universal option since it is distributed with LaTeX and requires no additional packages to be implemented.
|
% In your preamble, add: |
![]() |
If you do not load a package that supports \textsubscript{}, the math mode must be used. This is easily accomplished in running text by bracketing your text with the $ symbol. In math mode subscripting is done using the underscore: _{}.
For example, the formula for water is written as:
|
H$_2$O is the formula for water |
H2O is the formula for water |
See also the above mentioned package mhchem for chemical symbols and formulas.
Note that in math mode text will appear in a font suitable for mathematical variables. In math mode, to generate roman text, for example, one would use the \mathrm command:
|
This is $\mathrm{normal\ roman\ and}_\mathrm{subscript\ roman}$ text |
![]() |
Note the use of \<space> to insert a space in math mode.
Similarly, you can superscript using:
|
This is $\mathrm{normal\ roman\ and}^\mathrm{superscript\ roman}$ text |
![]() |
[edit] Text figures ("old style" numerals)
Many typographers prefer to use titling figures, sometimes called lining figures, when numerals are interspersed with full caps, when they appear in tables, and when they appear in equations, using text figures elsewhere. LaTeX allows this usage through the \oldstylenums{} command:
\oldstylenums{1234567890}
Some fonts do not have text figures built in; the textcomp package attempts to remedy this by effectively generating text figures from the currently-selected font. Put \usepackage{textcomp} in your preamble. textcomp also allows you to use decimal points, properly formatted dollar signs, etc. within \oldstylenums{}.
One common use for text figures is in section, paragraph, and page numbers. These can be set to use text figures by placing some code in your preamble:
\usepackage{textcomp} % Enclose everything in an \AtBeginDocument{} \AtBeginDocument{% % Make \section{} use text figures \let\myTheSection\thesection \renewcommand{\thesection}{\oldstylenums{\myTheSection}} % Make \paragraph{} use text figures \let\myTheParagraph\theparagraph \renewcommand{\theparagraph}{\oldstylenums{\myTheParagraph}} % Make the page numbers in text figures \let\myThePage\thepage \renewcommand{\thepage}{\oldstylenums{\myThePage}} }
Should you use additional sectioning or paragraphing commands, you may adapt the previous code listing to include them as well.
NOTE: A subsequent use of the \pagenumbering command, e.g., \pagenumbering{arabic}, will reset the \thepage command back to the original. Thus, if you use the \pagenumbering command in your document, be sure to reinstate your \myThePage definition from the code above:
... \tableofcontents \pagenumbering{roman} \chapter{Preface} ... \chapter{Introduction} ... \pagenumbering{arabic} % without this, the \thepage command will not be in oldstyle (e.g., in your Table of Contents} \renewcommand{\thepage}{\oldstylenums{\myThePage}} \Chapter{Foo} ...
[edit] Symbols and special characters
[edit] Dashes and Hyphens
LaTeX knows four kinds of dashes: a hyphen (-), en dash (–), em dash (—), or a minus sign (−). You can access three of them with different numbers of consecutive dashes. The fourth sign is actually not a dash at all—it is the mathematical minus sign:
|
Hyphen: mother-in-law, sister-in-law, X-rated\\ |
The names for these dashes are: ‘-’(-) hyphen , ‘--’(–) en-dash , ‘---’(—) em-dash and ‘ − ’(−) minus sign. They have different purposes:
Use \hyp{} macro from hyphenat package instead of hyphen if you want LaTeX to break compound words between lines.
[edit] Euro € currency symbol
When writing about money these days, you need the euro sign. You have several choices. If the fonts you are using have a euro symbol and you want to use that one, first you have to load the textcomp package in the preamble: \usepackage{textcomp} then you can insert the euro symbol with the command \texteuro. If you want to use the official version of the euro symbol, then you have to use eurosym, load it with the official option in the preamble: \usepackage[official]{eurosym} then you can insert it with the \euro command. Finally, if you want a euro symbol that matches with the current font style (e.g., bold, italics, etc.) but your current font does not provide it, you can use the eurosym package again but with a different option: \usepackage[gen]{eurosym} again you can insert the euro symbol with \euro
[edit] Ellipsis (…)
A sequence of three dots is known as an ellipsis, which is commonly used to indicate omitted text. On a typewriter, a comma or a period takes the same amount of space as any other letter. In book printing, these characters occupy only a little space and are set very close to the preceding letter. Therefore, you cannot enter ‘ellipsis’ by just typing three dots, as the spacing would be wrong. Instead, there is a special command for these dots. It is called \ldots:
|
Not like this ... but like this:\\ |
Alternatively, you can use the \textellipsis command which allows the spacing between the dots to vary.
[edit] Ready-made strings
There are some very simple LaTeX commands for typesetting special text strings:
[edit] Other symbols
LaTeX has lots of symbols at its disposal. The majority of them are within the mathematical domain, and later chapters will cover how to get access to them. For the more common text symbols, use the following commands:
Not mentioned in above table, tilde (~) is used in LaTeX code to produce non-breakable space. To get printed tilde sign, either make it verbatim text or write \~{}. And a visible space ␣ can be created with \textvisiblespace.
Of course, these are rather boring. For some more interesting symbols, the Postscript ZipfDingbats font is available thanks to the pifont package. Hopefully you are beginning to notice now that when you want to use a package, you need to add the declaration to your preamble; in this instance: \usepackage{pifont}. Next, the command \ding{number}, will print the specified symbol. Here is a table of the available symbols:
[edit] Paragraph Formatting
Altering the paragraph formatting is not often required, especially in academic writing. However, it is useful to know, and applications tend to be for formatting text in floats, or other more exotic documents.
[edit] Paragraph Alignment
Paragraphs in LaTeX are usually fully justified (i.e., flush with both the left and right margins). For whatever reason, should you wish to alter the justification of a paragraph, there are three environments at hand, and also Latex command equivalents.
| Alignment | Environment | Command |
|---|---|---|
| Left justified | flushleft | \raggedright |
| Right justified | flushright | \raggedleft |
| Center | center | \centering |
All text between the \begin and \end of the specified environment will be justified appropriately. The commands listed are for use within other environments. For example, p (paragraph) columns in tabular.
|
|
There is no way to set full justification. It means that if you do not enclose the previous 3 commands into a group, the rest of the document will be impacted.
So the right way of doing this with commands is { \raggedleft Some text flushed right. } |
[edit] Paragraph Indents
By default, the first paragraph after a heading follows the standard Anglo-American publishers' practice of no indentation. The size of subsequent paragraph indents are determined by a parameter called \parindent. The default length that this constant holds is set by the document class that you use. It is possible to override using the \setlength command.
\setlength{\parindent}{1cm}
This will set paragraph indents to 1cm.
Be careful, however, if you decide to set the indent to zero, then it means you will need a vertical space between paragraphs in order to make them clear. The space between paragraphs is held in \parskip, which could be altered in a similar fashion as above. However, this parameter is used elsewhere too, such as in lists, which means you run the risk of making various parts of your document look very untidy by changing this setting. If you want to use the style of having no indentation with a space between paragraphs, use the parskip package, which does this for you, while making adjustments to the spacing of lists and other structures which use paragraph spacing, so they don't get too far apart. Add this to the preamble:
\usepackage{parskip}
To indent subsequent lines of a paragraph, use the TeX command \hangindent. (While the default behaviour is to apply the hanging indent after the first line, this may be changed with the \hangafter command.) An example follows.
\hangindent=0.7cm This paragraph has an extra indentation at the left.
The TeX commands \leftskip and \rightskip add additional space to the left and right sides of each line, allowing the formatting for subsequent paragraphs to differ from the overall document margins. This space is in addition to the indentation added by \parindent and \hangindent.
To change the indentation of the last line in a paragraph, use the TeX command \parfillskip.
White-space in LaTeX can also be made flexible (what Lamport calls "rubber" lengths). This means that values such as \parskip can have a default dimension plus an amount of expansion minus an amount of contraction. This is useful on pages in complex documents where not every page may be an exact number of fixed-height lines long, so some give-and-take in vertical space is useful. You specify this in a \setlength command like this:
\setlength{\parskip}{1cm plus4mm minus3mm}
If you want to indent a paragraph that is not indented, you can use
\indent
at the beginning of the paragraph. Obviously, this will only have an effect when \parindent is not set to zero. If you want to indent the beginning of every section, you can use the indentfirst package, see the chapter about LaTeX/Packages for more information.
To create a non-indented paragraph, you can use
\noindent
as the first command of the paragraph. This might come in handy when you start a document with body text and not with a sectioning command.
[edit] Line Spacing
To change line spacing in the whole document use the command \linespread covered in LaTeX/Customizing_LaTeX#Spacing.
To change line spacing in specific environments do the following:
- Add \usepackage{setspace} to the document preamble.
- This then provides the following environments to use within your document:
- doublespace - all lines are double spaced.
- onehalfspace - line spacing set to one-and-half spacing.
- singlespace - normal line spacing.
- To change line spacing to custom values, use the command \setstretch{baselinestretch} in the preamble to change line spacing for the whole document or use the environment \begin{spacing}{2.5} ... \end{spacing} to change the line spacing to whatever is greater, the document-wide or environment-wide one.
After declaring the package in the preamble the use of the command \singlespacing, \doublespacing, or \onehalfspacing will specify the line spacing for all sections and paragraphs until another command is used.
See the section on customizing lists below for information on how to change the line spacing in lists.
[edit] Special Paragraphs
[edit] Verbatim Text
There are several ways to introduce text that won't be interpreted by the compiler. If you use the verbatim environment, everything input between the begin and end commands are processed as if by a typewriter. All spaces and new lines are reproduced as given, and the text is displayed in an appropriate fixed-width font. Any LaTeX command will be ignored and handled as plain text. This is ideal for typesetting program source code. Here is an example:
|
\begin{verbatim} |
Note: once in the verbatim environment, the only command that will be recognized is \end{verbatim}. Any others will be output. The font size in the verbatim environment can be adjusted by placing a font size command before \begin{verbatim}. If this is an issue, then you can use the alltt package instead, providing an environment with the same name:
|
\begin{alltt} |
Remember to add \usepackage{alltt} to your preamble to use it though! Within the alltt environment, you can use the command \normalfont to get back the normal font. To write equations within the alltt enviroment, you can use \( and \) to enclose them, instead of the usual$.
When using \textbf{} inside the alltt enviroment, note that the standard font has no bold TT font. Txtfonts has bold fonts: just add \renewcommand{\ttdefault}{txtt} after \usepackage{alltt}.
If you just want to introduce a short verbatim phrase, you don't need to use the whole environment, but you have the \verb command:
\verb+my text+
The first character following \verb is the delimiter: here we have used "+", but you can use any character you like but * and space; \verb will print verbatim all the text after it until it finds the next delimiter. For example, the code:
\verb|\textbf{Hi mate!}|
will print \textbf{Hi mate!}, ignoring the effect \textbf should have on text.
For more control over formatting, however, you can try the fancyvrb package, which provides a Verbatim environment (note the capital letter) which lets you draw a rule round the verbatim text, change the font size, and even have typographic effects inside the Verbatim environment. It can also be used in conjunction with the fancybox package and it can add reference line numbers (useful for chunks of data or programming), and it can even include entire external files.
[edit] Typesetting URLs
One of either the hyperref or url packages provides the \url command, which properly typesets URLs, for example:
Go to \url{http://www.uni.edu/~myname/best-website-ever.html} for my website.
will show this URL exactly as typed (similar to the \verb command), but the \url command also performs a hyphenless break at punctuation characters (only in pdflatex, not in plain latex + dvips). It was designed for Web URLs, so it understands their syntax and will never break mid-way through an unpunctuated word, only at slashes and full stops. Bear in mind, however, that spaces are forbidden in URLs, so using spaces in \url arguments will fail, as will using other non-URL-valid characters.
When using this command through the hyperref package, the URL is "clickable" in the PDF document, whereas it is not linked to the web when using only the url package. Also when using the hyperref package, to remove the border placed around a URL, insert pdfborder = {0 0 0 0} (alternately pdfborder = {0 0 0} might work if the four zeroes does not) inside the \hypersetup{}.
[edit] Listing Environment
This is also an extension of the verbatim environment provided by the moreverb package. The extra functionality it provides is that it can add line numbers along side the text. The command: \begin{listing}[step]{first line}. The mandatory first line argument is for specifying which line the numbering shall commence. The optional step is the step between numbered lines (the default is 1, which means every line will be numbered).
To use this environment, remember to add \usepackage{moreverb} to the document preamble.
[edit] Multi-line comments
As we have seen, the only way LaTeX allows you to add comments is by using the special character %, that will comment out all the rest of the line after itself. This approach is really time-consuming if you want to insert long comments or just comment out a part of your document that you want to improve later. Using the verbatim package, to be loaded in the preamble as usual:
|
\usepackage{verbatim} |
(you can also use the comment package instead) you can use an environment called comment that will comment out everything within itself. Here is an example:
|
This is another |
This is another example for embedding comments in your document. |
Note that this won’t work inside complex environments, like math for example. You may be wondering, why should I load a package called verbatim to have the possibility to add comments? The answer is straightforward: commented text is interpreted by the compiler just like verbatim text, the only difference is that verbatim text is introduced within the document, while the comment is just dropped.
Alternatively, you can define a \comment{} command, by adding the following to the document's preamble:
|
\newcommand{\comment}[1]{} |
Then, to comment out text, simply do something like this:
|
\comment{ This is a long comment and can extend over multiple lines, etc. } |
[edit] Quoting text
LaTeX provides several environments for quoting text; they have small differences and they are aimed for different types of quotations. All of them are indented on either margin, and you will need to add your own quotation marks if you want them. The provided environments are:
- quote
- for a short quotation, or a series of small quotes, separated by blank lines.
- quotation
- for use with longer quotations, of more than one paragraph, because it indents the first line of each paragraph.
- verse
- is for quotations where line breaks are important, such as poetry. Once in, new stanzas are created with a blank line, and new lines within a stanza are indicated using the newline command, \\. If a line takes up more than one line on the page, then all subsequent lines are indented until explicitly separated with \\.
[edit] Abstracts
In scientific publications it is customary to start with an abstract which gives the reader a quick overview of what to expect. LaTeX provides the abstract environment for this purpose. It is available in article and report document classes; it's not available in the book, but it's quite simple to create your own if you really need it.
[edit] Footnotes
Footnotes are a very useful way of providing extra information to the reader. Usually, it is non-essential information which can be placed at the bottom of the page. This keeps the main body of text concise.
The footnote facility is easy to use. The command you need is: \footnote{text}. Do not leave a space between the command and the word where you wish the footnote marker to appear, otherwise Latex will process that space and will leave the output not looking as intended.
|
Creating a footnote is easy.\footnote{An example footnote.} |
Latex will obviously take care of typesetting the footnote at the bottom of the page. Each footnote is numbered sequentially - a process that, as you should have guessed by now, is automatically done for you.
It is possible to customize the footnote marking. By default, they are numbered sequentially (Arabic). However, without going too much into the mechanics of Latex at this point, it is possible to change this using the following command (which needs to be placed at the beginning of the document, or at least before the first footnote command is issued).
| \renewcommand{\thefootnote}{\arabic{footnote}} | Arabic numerals, e.g., 1, 2, 3... |
| \renewcommand{\thefootnote}{\roman{footnote}} | Roman numerals (lowercase), e.g., i, ii, iii... |
| \renewcommand{\thefootnote}{\Roman{footnote}} | Roman numerals (uppercase), e.g., I, II, III... |
| \renewcommand{\thefootnote}{\alph{footnote}} | Alphabetic (lowercase), e.g., a, b, c... |
| \renewcommand{\thefootnote}{\Alph{footnote}} | Alphabetic (uppercase), e.g., A, B, C... |
| \renewcommand{\thefootnote}{\fnsymbol{footnote}} | A sequence of nine symbols (try it and see!) |
To make a footnote without number mark use this declaration:
|
\let\thefootnote\relax\footnote{There is no number in this footnote} |
Nevertheless, in that case, the current footnote counter is still incremented, so for instance you'd get footnote 1, unnumbered, and 2. A better solution[1] consists in defining the following macro in the preamble, and to use it:
\makeatletter
\def\blfootnote{\xdef\@thefnmark{}\@footnotetext}
\makeatother
The package footmisc offers many possibilities for customizing the appearance of footnotes. It can be used, for example, to use a different font within footnotes.
[edit] Common problems and workarounds
- Footnotes unfortunately don't work with tables, as it is considered a bad practice. You can overcome this limitation with several techniques: you can use \footnotemark[123] in the table, and \footnotetext[123]{HelloWorld!} somewhere on the page. Or, you can add \usepackage{footnote} and \makesavenoteenv{tabular} to the preamble, and put your table environment in a \begin{savenotes} environment. Note that the latter does not work with the packages color or colortbl. See this FAQ page for other approaches.
- Footnotes also don't work inside minipage environment (In fact, several environments break footnote support. the \makesavenoteenv{environmentname} command of the footnote package might fix most). The minipage includes its own footnotes, independent of the document's. The package mpfnmark allows greater flexibility in managing these two sets of footnotes.
- If the text within the footnote is an URL (using \url or \href commands) with special characters, it will not compile. You must either escape the characters with a leading backslash, or use another command.
- If the text within the footnote is very long, LaTeX may split the footnote over several pages. You can prevent LaTeX from doing so by increasing the penalty for such an operation. To do this, insert the following line into the preamble of your document:
- \interfootnotelinepenalty=10000
- To make multiple references to the same footnote, you can use the following syntax:
- Text that has a footnote\footnote{This is the footnote} looks like this. Later text referring to same footnote\footnotemark[\value{footnote}] uses the other command.
- If you need hyperref support, use instead:
- Text that has a footnote\footnote{This is the footnote}\addtocounter{footnote}{-1}\addtocounter{Hfootnote}{-1} looks like this. Later text referring to same footnote\footnotemark uses the other command.
- Note that these approaches don't work if there are other footnotes between the first reference and any of the other "duplicates".
- If the footnote is intended to be added to the title of a chapter, a section, or similar, two methods can be used:
- Write \section[title] {title\footnote{i'm a footnote referred to the section}} where "title" is the title of the section.
- Use the footmisc package, with package option stable, and simply add the footnote to the section title.
[edit] Margin Notes
Margin Notes are useful during the editorial process, to exchange comments among authors. To insert a margin note use \marginpar{margin text}. For one-sided layout (simplex), the text will be placed in the right margin, starting from the line where it is defined. For two-sided layout (duplex), it will be placed in the outside margin and for two-column layout it will be placed in the nearest margin.
To swap the default side, use \reversemarginpar and margin notes will then be placed on the opposite side, which would be the inside margin for two-sided layout.
If the text of your marginpar depends on which margin it is put in (say it includes an arrow pointing at the text or refers to a direction as in "as seen to the left..."), you can use \marginpar[left text]{right text} to specify the variants.
To insert a margin note in an area that \marginpar can't handle, such as footnotes or equation environments, use the package marginnote.
Another option for adding colored margin notes in a fancy way provides the package todonotes by using \todo{todo note}. It makes use of the package pgf used for designing and drawing with a huge tool database.
Also see the package mparhack.
[edit] Summary
Phew! What a busy tutorial! A lot of material was covered here, mainly because formatting is such a broad topic. LaTeX is so flexible that we actually only skimmed the surface, as you can have much more control over the presentation of your document if you wish. Having said that, one of the purposes of LaTeX is to take away the stress of having to deal with the physical presentation yourself, so you need not get too carried away!
This page uses material from Andy Roberts' Getting to grips with LaTeX with permission from the author.
The 









