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:
- Start with any document class:
\documentclass{article}
- Load the listings package:
\usepackage{listings}
- Begin the document:
\begin{document}
- Begin a lstlisting environment with an option for the language:
\begin{lstlisting}[language = C++]
- 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...