Book Image

LaTeX Cookbook - Second Edition

By : Stefan Kottwitz
Book Image

LaTeX Cookbook - Second Edition

By: Stefan Kottwitz

Overview of this book

The second edition of LaTeX Cookbook offers improved and additional examples especially for users in science and academia, with a focus on new packages for creating graphics with LaTeX. This edition also features an additional chapter on ChatGPT use to improve content, streamline code, and automate tasks, thereby saving time. This book is a practical guide to utilizing the capabilities of modern document classes and exploring the functionalities of the newest LaTeX packages. Starting with familiar document types like articles, books, letters, posters, leaflets, and presentations, it contains detailed tutorials for refining text design, adjusting fonts, managing images, creating tables, and optimizing PDFs. It also covers elements such as the bibliography, glossary, and index. You’ll learn to create graphics directly within LaTeX, including diagrams and plots, and explore LaTeX’s application across various fields like mathematics, physics, chemistry, and computer science. The book’s website offers online compilable code, an example gallery, and supplementary information related to the book, including the author’s LaTeX forum, where you can get personal support. By the end of this book, you’ll have the skills to optimize productivity through practical demonstrations of effective LaTeX usage in diverse scenarios.
Table of Contents (16 chapters)

Printing a code listing

Documentation often includes code snippets, as well as computer science theses. While the first recipe of this chapter handed pseudocode for algorithms and the subsequent recipe did actual programming, our focus now shifts to typesetting the code. To keep it concise, we’ll use a simple “hello world” program as an example.

How to do it...

We’ll utilize the listings package initially written by Carsten Heinz and designed explicitly for this task. 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. Begin a 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...