ΠΊΠ°ΠΊ Π²ΡΡΠ°Π²ΠΈΡΡ ΠΊΠΎΠ΄ latex Π² latex
ΠΡΠΎΡΠΌΠ»Π΅Π½ΠΈΠ΅ ΠΈΡΡ ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΠΊΠΎΠ΄Π° Π² Π΄ΠΎΠΊΡΠΌΠ΅Π½ΡΠ°Ρ LaTeX
verbatim
ΠΡΠΎΡΡΠ΅ΠΉΡΠΈΠΉ ΡΠΏΠΎΡΠΎΠ± β ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΠΎΠΊΡΡΠΆΠ΅Π½ΠΈΠ΅ \begin
Π£ΠΏΡΠ°Π²Π»Π΅Π½ΠΈΠ΅ ΡΠ°Π·ΠΌΠ΅ΡΠΎΠΌ ΡΡΠΈΡΡΠ° Π² ΠΎΠΊΡΡΠΆΠ΅Π½ΠΈΠΈ verbatim (ΡΡΡΠ°Π½ΠΎΠ²Π»Π΅Π½ \footnotesize ):
listings
Π€ΡΠ°Π³ΠΌΠ΅Π½Ρ ΠΊΠΎΠ΄Π° Π²Π½ΡΡΡΠΈ ΡΡΡΠΎΠΊΠΈ ΠΎΡΠΎΡΠΌΠ»ΡΠ΅ΡΡΡ ΡΠ»Π΅Π΄ΡΡΡΠΈΠΌ ΠΎΠ±ΡΠ°Π·ΠΎΠΌ
ΠΡΠΎΠ±Π»Π΅ΠΌΠ° Ρ UTF-8
ΠΡΠΈ ΠΏΠΎΠΏΡΡΠΊΠ΅ Π²ΡΡΠ°Π²ΠΈΡΡ Π² ΠΊΠΎΠ΄ ΡΠ΅ΠΊΡΡ Π½Π° ΠΊΠΈΡΠΈΠ»Π»ΠΈΡΠ΅ (Π½Π΅ Π² ΠΊΠΎΠΌΠΌΠ΅Π½ΡΠ°ΡΠΈΠΉ, Π° ΠΈΠΌΠ΅Π½Π½ΠΎ Π² ΠΊΠΎΠ΄ β ΡΠ΅ΠΊΡΡ Π΄ΠΎΠ»ΠΆΠ΅Π½ Π±ΡΠ» Π²ΡΠ²ΠΎΠ΄ΠΈΡΡΡΡ Π² Π³ΡΠ°ΡΠΈΡΠ΅ΡΠΊΠΎΠΌ ΠΎΠΊΠ½Π΅) Π±ΡΠ»ΠΎ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΎ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅ ΠΎΠ± ΠΎΡΠΈΠ±ΠΊΠ΅ Π²ΠΈΠ΄Π°:
ΠΡΠΈ ΡΡΠΎΠΌ ΠΊΠΎΠ΄ΠΈΡΠΎΠ²ΠΊΠ° ΠΎΡΠ½ΠΎΠ²Π½ΠΎΠ³ΠΎ Π΄ΠΎΠΊΡΠΌΠ΅Π½ΡΠ° β UTF-8, ΡΡΠΎ ΡΠΊΠ°Π·Π°Π½ΠΎ Π² ΠΏΡΠ΅Π°ΠΌΠ±ΡΠ»Π΅:
Π‘ΠΎΠΎΠ±ΡΠ΅Π½ΠΈΡ ΠΎΠ± ΠΎΡΠΈΠ±ΠΊΠ°Ρ ΠΈΡΡΠ΅Π·Π½ΡΡ, Π΅ΡΠ»ΠΈ ΡΠΊΠ°Π·Π°ΡΡ Π² Π½Π°ΡΡΡΠΎΠΉΠΊΠ°Ρ \lstset ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΠ΅ Π½Π΅-ASCII ΡΠΈΠΌΠ²ΠΎΠ»ΠΎΠ²:
ΠΠ΄Π½Π°ΠΊΠΎ ΠΏΡΠΈ ΡΡΠΎΠΌ ΠΎΡΡΠ°Π΅ΡΡΡ ΠΏΡΠΎΠ±Π»Π΅ΠΌΠ° Ρ ΡΡΡΡΠΊΠΎΡΠ·ΡΡΠ½ΡΠΌΠΈ ΠΊΠΎΠΌΠΌΠ΅Π½ΡΠ°ΡΠΈΡΠΌΠΈ, Π² ΠΊΠΎΡΠΎΡΡΡ ΡΠ»ΠΎΠ²Π° ΠΎΠΊΠ°Π·ΡΠ²Π°ΡΡΡΡ ΡΠΊΠ»Π΅Π΅Π½Ρ. ΠΠΊΠΎΠ½ΡΠ°ΡΠ΅Π»ΡΠ½ΠΎ ΡΠ΅ΡΠ°Π΅Ρ ΡΡΡ ΠΏΡΠΎΠ±Π»Π΅ΠΌΡ ΡΠ»Π΅Π΄ΡΡΡΠΈΠΉ ΠΏΠ°ΠΊΠ΅Ρ.
minted
ΠΡΡΠ°Π²ΠΈΠΌ Π² Π΄ΠΎΠΊΡΠΌΠ΅Π½Ρ ΠΊΠΎΠ΄ Π½Π° C++ ΡΡΠΈΡΡΠΎΠΌ ΡΠ°Π·ΠΌΠ΅ΡΠ° \footnotesize :
ΠΈ ΠΏΠΎΠ»ΡΡΠΈΠΌ ΠΏΠΎΡΠ»Π΅ ΡΡΠ°Π½ΡΠ»ΡΡΠΈΠΈ
Π’Π΅ΠΏΠ΅ΡΡ Π²ΡΠ΅ Π±ΡΠ΄Π΅Ρ ΡΠ°Π±ΠΎΡΠ°ΡΡ.
ΠΡΠ΅ ΡΠ°Π· Π½Π°ΠΏΠΎΠΌΠ½Ρ, Π² Π²Π΅ΡΡΠΈΡΡ minted > 2.0 Π²ΡΠ΅ ΡΠ°Π±ΠΎΡΠ°Π΅Ρ «ΠΈΠ· ΠΊΠΎΡΠΎΠ±ΠΊΠΈ».
ΠΡΠΎΡΠΌΠ»Π΅Π½ΠΈΠ΅ Π»ΠΈΡΡΠΈΠ½Π³ΠΎΠ² Π² Π²ΠΈΠ΄Π΅ ΡΠΈΡΡΠ½ΠΊΠΎΠ²
ΠΠ»Ρ ΡΡΠΎΠ³ΠΎ ΠΏΠΎΠΌΠ΅ΡΡΠΈΠΌ Π»ΠΈΡΡΠΈΠ½Π³, ΠΎΡΠΎΡΠΌΠ»Π΅Π½Π½ΡΠΉ Ρ ΠΏΠΎΠΌΠΎΡΡΡ minted Π² ΠΎΠΊΡΡΠΆΠ΅Π½ΠΈΠ΅ figure :
ΠΠ°ΡΡΠΈΠ½ΠΊΠ°-Π»ΠΈΡΡΠΈΠ½Π³ Π±ΡΠ΄Π΅Ρ Π²ΡΡΠΎΠ²Π½Π΅Π½Π° ΠΏΠΎ Π»Π΅Π²ΠΎΠΌΡ ΠΊΡΠ°Ρ ΡΡΡΠ°Π½ΠΈΡΡ
ΠΠ»Ρ Π²ΡΡΠ°Π²Π½ΠΈΠ²Π°Π½ΠΈΡ ΠΏΠΎ ΡΠ΅Π½ΡΡΡ Π½ΡΠΆΠ½ΠΎ ΠΏΠΎΡΡΠ°Π²ΠΈΡΡ Π² ΠΏΡΠ΅Π°ΠΌΠ±ΡΠ»Π΅ Π΄ΠΎΠΊΡΠΌΠ΅Π½ΡΠ°:
ΠΠΎΡΠΎΡΠΊΠΎΠ΅ ΠΈΠΌΡ ΠΈ Π½Π°ΡΡΡΠΎΠΉΠΊΠ° ΠΌΠ°ΠΊΡΠΎΡΠΎΠ²
Π§ΡΠΎΠ±Ρ Π½Π΅ ΠΏΠΈΡΠ°ΡΡ Π²ΡΡΠΊΠΈΠΉ ΡΠ°Π·
ΠΈΠ»ΠΈ Π½Π΅ΡΡΠΎ ΠΏΠΎΠ΄ΠΎΠ±Π½ΠΎΠ΅, ΠΌΠΎΠΆΠ½ΠΎ Π²Π²Π΅ΡΡΠΈ ΡΠΎΠ±ΡΡΠ²Π΅Π½Π½ΠΎΠ΅ Π½Π°Π·Π²Π°Π½ΠΈΠ΅ Π΄Π»Ρ ΠΌΠ°ΠΊΡΠΎΡΠΎΠ² Π²ΡΡΠ°Π²ΠΊΠΈ ΠΊΠΎΠ΄Π° Π½Π° Π·Π°Π΄Π°Π½Π½ΠΎΠΌ ΡΠ·ΡΠΊΠ΅ Ρ Π·Π°Π΄Π°Π½Π½ΡΠΌΠΈ Π½Π°ΡΡΡΠΎΠΉΠΊΠ°ΠΌΠΈ. ΠΠ΅Π»Π°Π΅ΡΡΡ ΡΡΠΎ ΠΏΡΠΈ ΠΏΠΎΠΌΠΎΡΠΈ ΠΊΠΎΠΌΠ°Π½Π΄Ρ \newminted :
ΠΠ°ΠΏΡΠΈΠΌΠ΅Ρ, ΡΠΊΠ°Π·Π°Π² Π² ΠΏΡΠ΅Π°ΠΌΠ±ΡΠ»Π΅ Π΄ΠΎΠΊΡΠΌΠ΅Π½ΡΠ°
ΠΌΡ ΡΠΌΠΎΠΆΠ΅ΠΌ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ Π΄Π»Ρ Π²ΡΡΠ°Π²ΠΊΠΈ ΠΊΠΎΠ΄Π° Π½ΠΎΠ²ΠΎΠ΅ ΠΎΠΊΡΡΠΆΠ΅Π½ΠΈΠ΅ mycode
ΠΠ°ΠΌΠ΅ΡΠΈΠΌ, ΡΡΠΎ Π΄Π°ΠΆΠ΅ Π΅ΡΠ»ΠΈ Π²Ρ Π½Π΅ Π·Π°Π΄Π°Π΅ΡΠ΅ Π½ΠΈΠΊΠ°ΠΊΠΈΡ Π½Π°ΡΡΡΠΎΠ΅ΠΊ, Π° ΠΏΡΠΎΡΡΠΎ Ρ ΠΎΡΠΈΡΠ΅ ΠΏΠ΅ΡΠ΅ΠΎΠΏΡΠ΅Π΄Π΅Π»ΠΈΡΡ ΠΈΠΌΡ ΠΌΠ°ΠΊΡΠΎΡΠ°, ΠΏΡΡΡΡΠ΅ ΡΠΊΠΎΠ±ΠΊΠΈ, ΠΎΠ±ΠΎΠ·Π½Π°ΡΠ°ΡΡΠΈΠ΅ ΠΌΠ΅ΡΡΠΎ Π²ΡΡΠ°Π²ΠΊΠΈ Π½Π°ΡΡΡΠΎΠ΅ΠΊ, Π΄ΠΎΠ»ΠΆΠ½Ρ ΠΏΡΠΈΡΡΡΡΡΠ²ΠΎΠ²Π°ΡΡ
ΠΠΎΠ΄ Π²Π½ΡΡΡΠΈ ΡΡΡΠΎΠΊΠΈ ΡΠ΅ΠΊΡΡΠ°
Π§ΡΠΎΠ±Ρ Π²ΡΡΠ°Π²ΠΈΡΡ ΠΊΠΎΠ΄ ΠΏΡΡΠΌΠΎ Π² ΡΡΡΠΎΠΊΡ ΡΠ΅ΠΊΡΡΠ°, ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΠΌ ΠΊΠΎΠΌΠ°Π½Π΄Ρ \mintinline
ΠΠ°ΠΏΡΠΈΠΌΠ΅Ρ, ΡΠΎΠ·Π΄Π°Π΄ΠΈΠΌ Π½ΠΎΠ²ΠΎΠ΅ ΠΎΠΊΡΡΠΆΠ΅Π½ΠΈΠ΅ Π΄Π»Ρ Π²ΡΡΠ°Π²ΠΊΠΈ Perl-ΠΊΠΎΠ΄Π°:
ΠΠΎΡ ΠΊΠ°ΠΊ ΡΡΠΎ ΡΠ°Π±ΠΎΡΠ°Π΅Ρ:
ΠΠ°ΠΊΠΈΠ΅ ΡΠ·ΡΠΊΠΈ ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΈΠ²Π°Π΅Ρ minted
Π£Π·Π½Π°ΡΡ, ΠΊΠ°ΠΊΠΈΠ΅ ΡΠ·ΡΠΊΠΈ ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΈΠ²Π°Π΅Ρ minted ΠΌΠΎΠΆΠ½ΠΎ, Π½Π°Π±ΡΠ°Π² Π² ΠΊΠΎΠΌΠ°Π½Π΄Π½ΠΎΠΉ ΡΡΡΠΎΠΊΠ΅
ΠΠ° Π΄Π°Π½Π½ΡΠΉ ΠΌΠΎΠΌΠ΅Π½Ρ ΡΠ°ΠΊΠΈΡ ΡΠ·ΡΠΊΠΎΠ² Π±ΠΎΠ»Π΅Π΅ 300.
Π§ΠΈΡΠ°ΠΉΡΠ΅ ΡΠ°ΠΊΠΆΠ΅
ΠΠΎΠΌΠΌΠ΅Π½ΡΠ°ΡΠΈΠΈ
ΠΠΌΠΈΡΡΠΈΠΉ Π₯ΡΠ°ΠΌΠΎΠ²
ΠΠΎΠΌΠΏΡΡΡΠ΅ΡΠ½ΠΎΠ΅ ΠΌΠΎΠ΄Π΅Π»ΠΈΡΠΎΠ²Π°Π½ΠΈΠ΅ ΠΈ Π²ΡΠ΅, ΡΡΠΎ Ρ Π½ΠΈΠΌ ΡΠ²ΡΠ·Π°Π½ΠΎ: ΡΠ±ΠΎΡ Π΄Π°Π½Π½ΡΡ
, ΠΈΡ
Π°Π½Π°Π»ΠΈΠ·, ΡΠ°Π·ΡΠ°Π±ΠΎΡΠΊΠ° ΠΌΠ°ΡΠ΅ΠΌΠ°ΡΠΈΡΠ΅ΡΠΊΠΈΡ
ΠΌΠΎΠ΄Π΅Π»Π΅ΠΉ, ΡΠΎΡΡ Π΄Π»Ρ ΠΌΠΎΠ΄Π΅Π»ΠΈΡΠΎΠ²Π°Π½ΠΈΡ, Π²ΠΈΠ·ΡΠ°Π»ΠΈΠ·Π°ΡΠΈΠΈ ΠΈ ΠΎΡΠΎΡΠΌΠ»Π΅Π½ΠΈΡ ΠΏΡΠ±Π»ΠΈΠΊΠ°ΡΠΈΠΉ. ΠΡ ΠΈ Π·Π° ΠΆΠΈΠ·Π½Ρ Π½Π΅ΠΌΠ½ΠΎΠ³ΠΎ.
Code listing
Contents
Introduction
L a T e X is widely used in science and programming has become an important aspect in several areas of science, hence the need for a tool that properly displays code. This article explains how to use the standard verbatim environment as well as the package listings, which provide more advanced code-formatting features. This separate article discusses the minted package, which performs syntax-highlighting using Python’s pygmentize library.
The verbatim environment
The code above produces the following output:
Just as in the example at the introduction, all text is printed keeping line breaks and white spaces. There’s a starred version of this command whose output is slightly different.
The code above produces the following output:
Verbatim-like text can also be used in a paragraph by means of the \verb command.
The code above produces the following output:
Using listings to highlight code
To use the lstlisting environment you have to add the following line to the preamble of your document:
Here’s an example of using the lstlisting environment from the listings package:
The code above produces the following output:
In this example, the output ignores all L a T e X commands and the text is printed keeping all the line breaks and white spaces typed. Let’s see a second example:
The code above produces the following output:
The additional parameter inside brackets [language=Python] enables code highlighting for this particular programming language (Python), special words are in boldface font and comments are italicized. See the reference guide for a complete list of supported programming languages.
Importing code from a file
Code is usually stored in a source file, therefore a command that automatically pulls code from a file becomes very handy.
The command \lstinputlisting[language=Octave]
If firstline or lastline is omitted, it’s assumed that the values are the beginning of the file, or the bottom of the file, respectively.
Code styles and colours
Code formatting with the listing package is highly customisable. Let’s see an example
The code above produces the following output:
As you see, the code colouring and styling greatly improves readability.
In this example the package xcolor is imported and then the command \definecolor<><><> is used to define new colours in rgb format that will later be used. For more information see: using colours in L a T e X
There are essentially two commands that generate the style for this example:
\lstdefinestyle