LaTeX/Paragraph Formatting

From Wikibooks, open books for an open world
Jump to: navigation, search
LaTeX logo.svg

LaTeX

Getting Started
  1. Introduction
  2. Installation
  3. Installing Extra Packages
  4. Basics

Common Elements

  1. Document Structure
  2. Text Formatting
  3. Paragraph Formatting
  4. Colors
  5. Fonts
  6. List Structures
  7. Special Characters
  8. Internationalization
  9. Rotations
  10. Tables
  11. Title creation
  12. Page Layout
  13. Importing Graphics
  14. Floats, Figures and Captions
  15. Footnotes and Margin Notes
  16. Hyperlinks
  17. Labels and Cross-referencing

Mechanics

  1. Errors and Warnings
  2. Lengths
  3. Counters
  4. Boxes
  5. Rules and Struts

Technical Texts

  1. Mathematics
  2. Advanced Mathematics
  3. Theorems
  4. Chemical Graphics
  5. Algorithms
  6. Source Code Listings
  7. Linguistics

Special Pages

  1. Indexing
  2. Glossary
  3. Bibliography Management
  4. More Bibliographies

Special Documents

  1. Letters
  2. Presentations
  3. Teacher's Corner
  4. Curriculum Vitae

Creating Graphics

  1. Introducing Procedural Graphics
  2. MetaPost
  3. Picture
  4. PGF/TikZ
  5. PSTricks
  6. Xy-pic
  7. Creating 3D graphics

Programming

  1. Macros
  2. Plain TeX
  3. Creating Packages
  4. Themes

Miscellaneous

  1. Modular Documents
  2. Collaborative Writing of LaTeX Documents
  3. Export To Other Formats

Help and Recommendations

  1. FAQ
  2. Tips and Tricks

Appendices

  1. Authors
  2. Links
  3. Package Reference
  4. Sample LaTeX documents
  5. Index
  6. Command Glossary

edit this boxedit the TOC

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.

Paragraph alignment[edit]

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.

Paragraph indent and break[edit]

By default, the first paragraph after a heading follows the standard Anglo-American publishers' practice of no indentation. The size of subsequent paragraph indents is 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 it by using the \setlength command. This will set paragraph indents to 1cm:

\setlength{\parindent}{1cm} % Default is 15pt.

White-space in LaTeX can also be made flexible (what Lamport calls "rubber" lengths). This means that values such as extra vertical space inserted before a paragraph \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: once loaded, the beginning of any chapter/section is indented by the usual paragraph indentation.

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.

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. If you want both indent and break, use

\usepackage{parskip}
\setlength{\parindent}{15pt
}

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.

\paragraph line break[edit]

Default style for \paragraph may seem odd in the first place, as it writes the following text next to the title. If you do not like it, use a class other than the traditional article/book, or use ConTeXt or PlainTeX. Hacking of the class in use is really not the way LaTeX is intended to be used, and you may encounter a lot of frustrating issues.

Anyway, let's analyse the problem. If you add a manual line break with \\, LaTeX will complain that

There's no line here to end.

Simply adding an empty space will do it:

\paragraph{Title} \hspace{0pt} \\
Text...

Alternatively you can use the shorter, yet not completely equivalent syntax:

\paragraph{Title} ~\\
Text...

Line spacing[edit]

To change line spacing in the whole document use the command \linespread covered in Text Formatting.

Alternatively, you can use the \usepackage{setspace} package, which is also covered in Text Formatting. This package provides the commands \doublespacing, \onehalfspacing, \singlespacing and \setstretch{baselinestretch}, which will specify the line spacing for all sections and paragraphs until another command is used. Furthermore, the package provides the following environments in order to change line spacing within the document but not document-wide:

  • doublespace: lines are double spaced;
  • onehalfspace: line spacing set to one-and-half spacing;
  • singlespace: normal line spacing;
  • spacing: customizable line spacing, e.g. \begin{spacing}{baselinestretch} ... \end{spacing}.

See the section on customizing lists for information on how to change the line spacing in lists.

Manual breaks[edit]

LaTeX takes care of the formatting, breaks included. You should avoid manual breaking as much as possible, or it could lead to very bad formatting.

Controlling the breaks should be reserved to macro and package writers. Here follows a quick reference.

\newline Breaks the line at the point of the command.
\\ Breaks the line at the point of the command; it is usually a shorter version of the previous command, but LaTeX sometimes redefines it for several environments. This command also features the vertical space as optional parameter.
\\* Breaks the line at the point of the command and additionally prohibits a page break after the forced line break. This command also features the vertical space as optional parameter.
\linebreak[number] Breaks the line at the point of the command. The number you provide as an argument represents the priority of the command in a range from 0 (it will be easily ignored) to 4 (do it anyway). LaTeX will try to produce the best line breaks possible. If it cannot, it will decide whether including the linebreak or not according to the priority you have provided.
\break (TeX) Breaks the line without filling the current line. This will result in an underful badness if you do not fill the line yourself, i.e. ...\hfill\break .... Actually \hfill\break produces the same as \newline and \\.
\par (TeX) Starts a new paragraph. It is a horizontal mode command, so you can only use it in a paragraph.

The page breaks are covered in Page Layout. More details on manual spaces between paragraphs (such as \bigskip) can be found in Lengths.

Special paragraphs[edit]

Verbatim text[edit]

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}
The verbatim environment
  simply reproduces every
 character you input,
including all  s p a c e s!
\end{verbatim
}

Verbatim.svg

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 a problem, you can use the alltt package instead, providing an environment with the same name:

\begin{alltt}
Verbatim extended with the ability
to use normal commands.  Therefore, it
is possible to \emph{emphasize} words in
this environment, for example.
\end{alltt
}

Alltt.svg

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 except *; \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.

Typesetting URLs[edit]

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} inside the \hypersetup{}. (Alternately pdfborder = {0 0 0} might work if the four zeroes do not.)

You can put the following code into your preamble to change the style, how URLs are displayed to the normal font:

\urlstyle{same}

See also Hyperlinks

Listing environment[edit]

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.

Multi-line comments[edit]

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, unless you're using an editor that automates this process. Alternatively, you can use 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
\begin{comment}
rather stupid,
but helpful
\end{comment
}
example for embedding
comments in your document.

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.} But it won't show.

But it won't show.

This approach can, however, produce unwanted spaces in the document, so it may work better to use

\newcommand{\comment}[2]{#2}

Then if you supply only one argument to \comment{}, this has the desired effect without producing extra spaces.

Another drawback is that content is still parsed and possibly expanded, so you cannot put anything you want in it (such as LaTeX commands).

Quoting text[edit]

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 \\.

Abstracts[edit]

In scientific publications it is customary to start with an abstract which gives the reader a quick overview of what to expect. See Document Structure.

Notes and References[edit]

This page uses material from Andy Roberts' Getting to grips with LaTeX with permission from the author.


Previous: Text Formatting Index Next: Colors