Book Image

LaTeX Cookbook

By : Stefan Kottwitz
Book Image

LaTeX Cookbook

By: Stefan Kottwitz

Overview of this book

LaTeX is a high-quality typesetting software and is very popular, especially among scientists. Its programming language gives you full control over every aspect of your documents, no matter how complex they are. LaTeX's huge amount of customizable templates and supporting packages cover most aspects of writing with embedded typographic expertise. With this book you will learn to leverage the capabilities of the latest document classes and explore the functionalities of the newest packages. The book starts with examples of common document types. It provides you with samples for tuning text design, using fonts, embedding images, and creating legible tables. Common document parts such as the bibliography, glossary, and index are covered, with LaTeX's modern approach.You will learn how to create excellent graphics directly within LaTeX, including diagrams and plots quickly and easily. Finally, you will discover how to use the new engines XeTeX and LuaTeX for advanced programming and calculating with LaTeX. The example-driven approach of this book is sure to increase your productivity.
Table of Contents (19 chapters)
LaTeX Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Printing a code listing


Documentation may contain code samples. The same applies to theses in computer science. While pseudocode of algorithms was covered in the previous recipe, we now like to typeset real code. To save space, we will use a small "hello world" program as an example.

How to do it...

We will use the listings package, which has been designed for that purpose. Follow these steps:

  1. Start with any document class:

    \documentclass{article}
  2. Load the listings package:

    \usepackage{listings}
  3. Begin the document:

    \begin{document}
  4. Start an lstlisting environment with an option for the language:

    \begin{lstlisting}[language = C++]
  5. Continue with the code you would like to print:

    // include standard input/output stream objects:
    #include <iostream>
    // the main method:
    int main() {
      std::cout << ""Hello TeX world!"" << std::endl;
    }
  6. End the lstlisting environment and the document:

    \end{lstlisting}
    \end{document}
  7. Compile the document and have a look:

How it works...

The basic usage is simple...