4.2 Simulando texto escrito
It is often necessary to display information verbatim — that is, “as entered at the terminal”. This ability is provided by the standard LaTEX environment verbatim. However, to guide the reader it might be useful to highlight certain textual strings in a particular way, such as by numbering the lines. Over time a number of packages have appeared that address one or the other extra feature — unfortunately, each with its own syntax. Note that, just like the basic \verb and the verbatim environment, they all assume that the input is restricted to ASCII; anything else only works in a few circumstances, unless otherwise noted.
In this section we first review a few such smaller packages offering solutions to specific problems. We then concentrate on the package fancyvrb written by Timothy Van Zandt, which combines all such features and many more under the roof of a single, highly customizable package. This coverage is followed by a discussion of the listings package, which provides a versatile environment in which to prettyprint computer listings for a large number of computer languages.
4.2.1 Mostrar espacios en material textual
When typesetting material verbatim, it is sometimes necessary to show the exact number of spaces used, and for this LaTEX offers \verb* and verbatim*. They show spaces as “␣”, but they were originally coded under the assumption that the position of the space character (i.e., ASCII 32) in a typewriter font contains such a visible space glyph. This is correct for pdfTEX with the most used font encodings OT1 and T1.
4.2.2 Extensiones textuales simples
The package alltt (by Leslie Lamport) defines the alltt environment. It acts like a verbatim environment except that the backslash “” and braces “{” and “}” retain their usual meanings. Thus, other commands and environments can appear inside an alltt environment. A similar functionality is provided by the fancyvrb environment key commandchars (see page 313).
4.2.3 upquote - Citas de estilo de programa de computadora
The Computer Modern Typewriter font that is used by default for typesetting “verbatim” is a very readable monospaced typeface. Due to its small running length, it is very well suited for typesetting computer programs and similar material. See Section 10.9 for a comparison of this font with other monospaced typefaces.
There is, however, one potential problem when using this font to render computer program listings and similar material: most people expect to see a (right) quote in a computer listing represented with a straight quote character (i.e., ’) and a left or back quote as a kind of grave accent on its own (i.e., `).
The Computer Modern Typewriter font, however, displays real left and right curly quote characters (as one would expect in a normal text font). In fact, most other typewriter fonts when set up for use with LaTEX follow this pattern. This produces somewhat unconventional results that many people find difficult to understand. Consider the following example, which shows the standard behavior for three major typewriter fonts: LuxiMono,1 Courier (or rather TEX Gyre Cursor), and Computer Modern Typewriter.
4.2.4 fancyvrb, fvextra - Ambientes textuales con esteroides
The fancyvrb package by Timothy Van Zandt (maintained by Herbert Voß) offers a highly customizable set of environments and commands to typeset and manipulate verbatim text. It had its first public release already in 1998 and quickly became one of the dominant packages in this space and has largely remained unchanged since then.
It works by parsing one line at a time from an environment or a file (a concept pioneered by the verbatim package), thereby allowing you to preprocess lines in various ways. By incorporating features found in various other packages it provides a truly universal production environment under a common set of syntax rules.
Fairly recently (in TEX terms) in 2016 Geoffrey Poore published the fvextra package, which is intended to be a drop-in replacement for fancyvrb while offering a number of additional features. In this section we describe both package together. All examples that can be used with either package use fancyvrb, while those that need fvextra will obviously use that package. Often there are only some aspects that require fvextra or make the input more convenient; those are then explicitly mentioned in the text.
4.2.5 listings - Código de programa de impresión bonita
A common application of verbatim typesetting is presenting program code. While one can successfully deploy a package like fancyvrb to handle this job, it is often preferable to enhance the display by typesetting certain program components (such as keywords, identifiers, and comments) in a special way.
Two major approaches are possible: one can provide commands to identify the logical aspects of algorithms or the programming language, or the application can (try to) analyze the program code behind the scenes. The second cases can be subdivided into complete processing within LaTEX or processing the algorithm with some external program and (automatically) including the results.
The advantage of the first approach is that you have potentially more control over the presentation; however, your program code is intermixed with TEX commands and thus may be difficult to maintain, is unusable for direct processing, and is often rather complicated to read in the source. Examples of packages classified into this category are algorithms (using fully uppercased command names) and algorithmicx and its offsprings like algpseudocode. Here is an example: