Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
LaTeX Cookbook

You're reading from   LaTeX Cookbook Over 90 hands-on recipes for quickly preparing LaTeX documents to solve various challenging tasks

Arrow left icon
Product type Paperback
Published in Oct 2015
Publisher Packt
ISBN-13 9781784395148
Length 378 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Stefan Kottwitz Stefan Kottwitz
Author Profile Icon Stefan Kottwitz
Stefan Kottwitz
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. The Variety of Document Types 2. Tuning the Text FREE CHAPTER 3. Adjusting Fonts 4. Working with Images 5. Beautiful Designs 6. Designing Tables 7. Contents, Indexes, and Bibliographies 8. Getting the Most out of the PDF 9. Creating Graphics 10. Advanced Mathematics 11. Science and Technology 12. Getting Support on the Internet 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 to do it...

How it works...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image