Tips and tools for managing LaTeX

Powerful Format

Article from Issue 191/2016
Author(s):

LaTeX has a reputation for being difficult to use, but mainly it just requires patience. We provide a quick overview to get you up and running.

Lamport TeXt (LaTeX) is one of free software's legendary applications [1]. Before LibreOffice or AbiWord, it was the most advanced tool on Linux for formatting text. Even today, it is widely used in the academic sciences and in some publishing houses. Users speak of compiling their first lengthy document in LaTeX in the same way others talk of compiling their first Linux kernel, and several professors tell me that students regularly have to be prevented from obsessing over LaTeX at the expense of their research.

LaTeX has a reputation for being difficult, but not because of its complexity. Instead, the difficulty lies in the size of the project. Adding to the difficulty is the number of editors designed for it, including extensions for both Vim and Emacs [2], as well as LyX, a graphical interface [3]. The difficulty in learning LaTeX does not lie in the concepts, as much as in finding the right tools.

At its heart, though, LaTeX is actually simple. Basically, it is a combination of a markup language similar to HTML and a collection of formatting macros. Creating a document in LaTeX consists of three steps:

  • Using Tex or some other editor to write the markup.
  • Compiling the LaTeX document.
  • Exporting to a usable file format with another script

Despite the many tools available for working with LaTeX, all these steps can be done using applications that are part of the default installation for most distributions. What follows is a quick overview to get you up and running.

Basic Structure of a LaTeX Document

To create a LaTeX document, you begin by opening any text editor. Figure 1 shows the structure of a simple TeX document. The first thing to notice is that the percentage sign (%) at the start of a line indicates a comment that is not printed. A backslash (\) at the start indicates a piece of markup. Often, the markup is a reference to a formatting macro and which formatting choice(s) are being used, with the macro coming first and the choice following in curly braces. For instance, \documentclass{article} defines the type of document as an article.

Figure 1: All TeX documents have the same minimal structure.

The next thing to notice is that the document is divided into sections, with the beginning and end of each section clearly marked. Everything above \begin{document} is known as the preamble or the top matter.

The preamble is the place for meta-information – not only introductory comments, but also information like the author, title, and the date, such as \author{Bruce Byfield}. The preamble also includes high-level formatting for the entire document (except where overridden by a section), including the default font size and the paper size, such as \documentclass[12pt,letterpaper,oneside,draft]{report}, the formatting being added in square brackets after the macro. For convenience, you can divide this information into stanzas with a space between each one. The average preamble then would probably look more like Figure 2.

Figure 2: A typical preamble in a document.

You can start adding content below \begin{document}. It can be further subdivided by other section types. For the most part, the content is simply typed, with a blank line indicating a new paragraph. None of the content, of course, will be formatted until you compile. And, if you actually want to print a backslash, you must add another backslash (\\) so that it is not interpreted as markup. That covers most of the mechanics of writing markup that you need to know.

Basic Formatting

The rest of using LaTeX is almost entirely looking up which macros you need to get the results you want. Fortunately, LaTeX is exhaustively documented and, with some patience, you should be able to find the information you need. All the same, when drafting, you should compile frequently and be prepared, if necessary, to continually tweak your markup. If you do the same type of document repeatedly, save satisfactory results as templates so you don't need to test more than once.

You can usually save time by defining a document type and its formatting options (see Table 1) in the preamble and letting the macro do most of the work for you. For general purposes, use article as the document type. Other common classes include book, letter, report (including thesis), and beamer (presentations).

Table 1

Common Options for Most Document Classes

Formatting

Markup

Comments

Paper size

\usepackage[a4paper]{geometry}

Paper sizes include letterpaper & legalpaper

Orientation

\usepackage[landscape]{geometry}

Values are landscape & portrait

Margins

\usepackage[top=length, bottom=length, left=length, right=length]{geometry}

Length is in centimeters or inches

Headers and footers

\pagestyle{style}

Applies to current and subsequent pages. Choices are empty (no header or footer), plain (foot includes page number), or headings (header contains information defined by document class)

Headers and footers

\this pagestyle{style}

Same as \pagestyle, but only for current page

For more control over page appearance, you can also add some non-standard document classes [4] by hunting down their packages on the Internet and placing those packages in the same directory as the Tex file. If some sections define their own pages, you can override them by placing \pagestyle{empty} in the preamble and defining the pagestyle as need throughout the document.

Another way to reduce the amount of formatting is to define a section, such as \chapter{''TITLE''} or \section{''TITLE''}. But, using sections can become complicated, especially if you want to repress the numbering that many sections automatically add.

For the equivalent of word processor character styles, you can use \textit for italics, \textbf for bold, and \underline for underlining. In each case, follow the basic command with the text to format in curly braces. In other words, to place "equivalent" in the first sentence of this paragraph in italics, you would enter:

For the \textit{equivalent} of word processor character styles

Ordered lists start with \begin{enumerate} and end with \end{enumerate}, whereas unordered lists are marked by \begin{itemize} and \end{itemize}. Both types of list can have as many entries as required, but each item must begin on a separate line that begins with \item.

To use graphics, place \usepackage{graphicx} and \graphicspath/{ {FOLDER} } in the preamble, being careful to end the graphics path with a forward slash. At the position where you want to place the graphic, add \include[FILE].

Modern LaTex supports both EPS and PNG files, but you do not need to include any extension when adding the image to the document. You can change the size of the graphic with \includegraphics[scale = RaTIO], where 1.0 is the original size or change the dimensions with \includegraphics[width=SIZE height=SIZE].

These are far from the only formatting options you are likely to want to use with LaTeX, and they do not address challenges such as positioning a page break where you want it. However, they should be enough for many documents.

Compiling and Outputting

When you are finished creating the file, save it and then run the command latex FILE. The result is a file of the same name, except with a DVI extension. A logfile of the same name is also created, allowing you help with troubleshooting the markup. You can also find various viewers, such as advi, to inspect the file visually.

If there are no errors, you can use the DVI file to export to four formats: postscript (PS), PDF, RTF, and HTML. There are many scripts for these exports, but div2ps, ps2pdf, latext2rtf, and latext2html should do for most purposes.

Although many instructions warn of a possible loss of formatting when exporting to RTF or HTML, I have personally never encountered any. If you do, however, a postscript or PDF export should be more reliable.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Asciidoctor

    The popular AsciiDoc documentation system still has a lot to offer, but more experienced users should check out Asciidoctor, which has some additional new features.

  • AsciiDoc

    AsciiDoc syntax along with its eponymous command lets users create a text document with unobtrusive markup and convert it to a variety of output formats.

  • TeXmacs

    If you need to lay out a text document, you can opt for a WYSIWYG application, like OpenOffice, or a genuine layout system such as TeX. TeXmacs combines the advantages of both approaches: premium document quality and intuitive controls.

  • Workspace: txt2tags

    How would you like write text using whatever text editor you like and then publish the results in a variety of formats, including (X)HTML, MoinMoin wiki, LaTeX, and SGML? txt2tags allows you to write once and publish
    anywhere.

  • Swiss Army Knife

    Pandoc lets you convert files from one markup format to another at the command line.

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

News