• 검색 결과가 없습니다.

The listings Package

문서에서 The Not So Short Introduction to LATEX (페이지 61-67)

2.17 Code Listings

2.17.2 The listings Package

While the verbatim environment is fine for small snippets, they are very crude and look basic. In order to make them a bit more fancy, you can use the listings [23] package. It defines the

\lstinlinecommand that works like\verb lstlisting environment that works like verbatim

\lstinputlisting command that works like\verbatiminput

\lstinline|\LaTeX{}|

\begin{lstlisting}

Here is some \LaTeX{} code.

\end{lstlisting}

\lstinputlisting{hello.c}

\LaTeX{}

Here i s some \LaTeX{} code . // A simple program

#i n c l u d e ” s t d i o . h”

i n t main ( ) {

p r i n t f (” Hello , World ! \ n ” ) ; return 0 ;

}

As you can see, by default it isn’t much different from the verbatim environment. The big difference is that every command accepts optional argument that allows you to customise the output. The argument accepts a comma-delimited key-value list. For example, to automatically highlight

the code, we may pass the language key.

\lstinputlisting[language=C]{hello.c}

// A simple program

#include ” s t d i o . h”

int main ( ) {

p r i n t f ( ” Hello , ␣World ! \ n” ) ; return 0 ;

}

For many, the default style of the listings may look a bit strange. No worries, this is easily fixed. The output can be customized using the following keys:

basicstyle, keywordstyle, identifierstyle and commentstyle Note that you will need to use some font changing commands, when configuring the styles. More about font changing command can be found inSection 7.2.

\lstinputlisting[

language=SQL,

basicstyle=\ttfamily, commentstyle=\color{gray}, keywordstyle=\itshape, ]{employee.sql}

-- A simple query

SELECT * FROM employees WHERE salary > 2000.00 ORDER BY last_name;

If you intend to use similar options for many listings in the document, you can use the \lstset command to set them beforehand. In this way, you will avoid repeating them every time you want to typeset some code.

\lstset{

language=C, numbers=left, breaklines,

basicstyle=\ttfamily, commentstyle=\color{gray}, prebreak=\textrightarrow, }

\lstinputlisting{hello.c}

The statement

\lstinline|int x = 1;|

assigns 1 to variable

\lstinline|x|.

1 // A simple program 2 #include "stdio.h"

34 int main() {

5 printf("Hello , World→

!\n");

6 return 0;

7 }

The statement int x = 1; assigns 1 to variable x.

2.17 Code Listings 45

If a language has several dialects, you may specify which inside square brackets before the language name. You must enclose the value in curly brackets.

\lstinputlisting[language={[LaTeX]TeX}]{hello.tex}

\documentclass{article}

\begin{document}

Hello , World!

\end{document}

When presenting code, it is often that we want to point to a specific line. To save the reader from counting which line we mean, it is possible to print line numbers by using the numbers key. Its possible values are none, left and right.

\lstinputlisting[language=C, numbers=left]{hello.c}

1 // A simple program 2 #include "stdio.h"

34 int main() {

5 printf("Hello , World!\n");

6 return 0;

7 }

The appearance of numbers may be further customised with the following keys:

firstnumber allows you to specify the first number. Besides numbers, this key can also contain two special values: last and auto (the default). last continues numbering from the last listing, while auto continues it from the last listing with the same name argument, or starts over if no name is present.

stepnumber prints only every 𝑛-th number. For example, if you pass stepnumber=7, then only line numbers 1, 8, 15, 22, … will be printed.

numberblanklines controls whether or not line numbers are printed on empty lines. Either true or false.

numberstyle allows you to customise the font for printing numbers. It accepts switch commands, as described inSection 7.2.

\lstinputlisting[

language=C, numbers=left, stepnumber=2,

numberblanklines=false, firstnumber=4,

numberstyle=\tiny, ]{hello.c}

4 // A simple program

#include "stdio.h"

int main() {

8 printf("Hello , World!\n");

return 0;

10 }

If you have a long file and want to show it piecewise, you can use the firstline and lastline keys.

The \lstinline|main|

function body consists of:

\lstinputlisting[

language=C, firstline=5, lastline=6, ]{hello.c}

The main function body consists of:

printf("Hello , World!\n");

return 0;

If you are trying to typeset long lines, you may order the listings to automatically break them by using the breaklines key.

\lstinputlisting[

language=python, numbers=left, breaklines, ]{factorial.py}

1 import math

23 def oneline_factorial(n):

4 return math.prod(i for

i in range(1, n + 1) 5 )

6 print(oneline_factorial(5))

You can customise the indentation width with the breakindent key.

It may be useful to indicate that a line break has occurred by using the prebreak and postbreak keys, which print their value respectively before and after an artificial line break. Listing 2.1shows an example of this.

2.17 Code Listings 47

\lstinputlisting[

language=python, numbers=left, breaklines, breakindent=1cm,

prebreak=\textrightarrow, postbreak=\textleftarrow, ]{factorial.py}

1 import math

23 def oneline_factorial(n):

4 return math.prod(i for→

← i in range(1, n →

←+ 1))

56 print(oneline_factorial(5)→

←)

Listing 2.1: An example of marking the artificial linebreaks in listing package.

One of the neat features of the listings package is that you can evaluate LATEX code inside the listing. The easiest way is to pass the texcl key, which enables LATEX syntax within comments.

\begin{lstlisting}[

texcl,

language=haskell,

]-- I can use \LaTeX{} here.

-- The cost is in \texteuro{}

cost x = show ( foldr (+) 0 x ) ++ " eur"

\end{lstlisting}

-- I can use LATEX here.

-- The cost is in € cost x = show (

foldr (+) 0 x ) ++ " eur"

This feature is especially useful in combination with the \label command. We can use it to point to a specific line of code without hardcoding it into the document.

\begin{lstlisting}[

texcl,

numbers=left, language=haskell, ]cost x = show (

foldr (+) 0 x

) ++ " eur" -- \label{concat}

\end{lstlisting}

The \lstinline|++| in Haskell as seen in line~\ref{concat}

means string concatenation.

1 cost x = show (

2 foldr (+) 0 x

3 ) ++ " eur"

--The ++ in Haskell as seen in line 3 means string concatenation.

This has the downside that it introduces empty comments that are just there to include label. To fix this we may use the escapeinside key that accepts sets the two delimiters between which LATEX code can be typed. The delimiters themselves will not be printed.

\begin{lstlisting}[

escapeinside={(*}{*)}, numbers=left,

language=haskell,

]cost x = show ( (*\label{show}*) foldr (+) 0 x

) ++ " eur" (*(\texteuro)*)

\end{lstlisting}

The \lstinline|show| in Haskell as seen in line~\ref{show}

converts its argument to a string.

1 cost x = show (

2 foldr (+) 0 x

3 ) ++ " eur" (€)

The show in Haskell as seen in line 1 converts its argument to a string.

2.17 Code Listings 49

문서에서 The Not So Short Introduction to LATEX (페이지 61-67)