{listings} is a package that extends LaTeX's {verbatim} features. A variety of syntax highlighting options are available. For general questions about {verbatim} or {highlighting} use the respective tag. For the unrelated concept of list structures, use {lists} instead.

learn more… | top users | synonyms

61
votes
3answers
2k views

Is there a LaTeX trick to prevent a PDF viewer from copying the line number?

A PDF output is obtained by compiling the following code. \documentclass{article} \usepackage{xcolor} \usepackage{listings} \lstset { language={[LaTeX]TeX}, numbers=left, ...
50
votes
6answers
11k views

Syntax Coloring in LaTeX

Is there any way to include Python code in a LaTeX file and automatically color its syntax? Is it possible to do syntax coloring with any other language? If yes what packages can I use to do that?
44
votes
1answer
2k views

Creating a zebra effect using listings

This question led to a new package: lstlinebgrd (lstaddons bundle) Friends, consider the following example using the listings package: \documentclass{article} \usepackage[T1]{fontenc} ...
37
votes
4answers
2k views

Has the time come for a LaTeX3 successor to 'listings'?

I'm currently looking for a nice and challenging open-source project to work/collaborate on after my thesis. I don't have a Comp. Sci. background, but I'd like to learn more about compiler ...
34
votes
1answer
460 views

listings: Copy to clipboard feature?

I was just wonder if it would be possible to have a "Copy to Clipboard Button" next to each listing which would just copy the verbatim content of the lstlistings environment to the clipboard. An ...
32
votes
2answers
2k views

minted vs. texments vs. verbments

Regarding the typesetting of syntax-colored source code, I know that listings is inferior because it doesn't utilize a full lexer. Instead the Python-based solution is recommended. It seems there are ...
31
votes
4answers
17k views

How to prevent lstlisting from splitting code between pages?

Is it possible to prevent lstlisting from splitting a code between pages if it does not fit on one page? Instead splitting I would like to have the code on the next page. I tried to put all ...
30
votes
3answers
18k views

What can I use to typeset MATLAB code in my document?

This question led to a new package: matlab-prettifier Does anybody have a quick and easy way to typeset some relatively long MATLAB code in the appendix? I looked up a few methods, and the ...
27
votes
5answers
23k views

XML syntax highlighting

I use the following code to include some XML code: \lstset{language=XML} \begin{lstlisting} <?xml version="1.0" encoding="utf-8"?> <xs:schema attributeFormDefault="unqualified" ...
22
votes
4answers
7k views

Having problems with listings and UTF-8. Can it be fixed?

I'm having some problems with listings and UTF-8 in my document. Maybe someone can help me? Some characters work, like é and ó, but á and others appear at the beginning of words... ...
22
votes
2answers
8k views

How best to include programming source code in LaTeX documents? [duplicate]

Possible Duplicate: Syntax Coloring in LaTeX I'm relatively new to LaTeX, and am starting to use it for academic paper writing. I'm doing a lot of computer programming as part of my ...
22
votes
3answers
5k views

How can I highlight some lines from source code?

I am preparing a presentation (using beamer) and I'll need to include many snippets of source-code. For including source code, I'll probably use listings package. (I know there is also minted package, ...
22
votes
2answers
10k views

Extend a language with additional keywords?

I'm using listings and I want to extend a language with additional keywords for highlightning. I know there is an option morekeywords available, but that only works for a particular lstset declaration ...
22
votes
2answers
2k views

Ensure verbatim code block is copy/paste-able

After rendering a document containing this code block \begin{verbatim} if [ ! -d .git ]; then git init; fi # Initialises a new Git repository, if doesn't already exist. if [ ! -f README.md ]; ...
21
votes
4answers
11k views

Using \ttfamily with \bfseries (or how to enable bold in fixed-width font)

I'm using listings package, and I've added basicstyle=\ttfamily\footnotesize. However, when I do that, the keywords from the embedded source-code are not bold anymore. I figured that \ttfamily ...
21
votes
1answer
6k views

How to center a listing?

I'm trying to get some console output in my homework. I put it in a listings environment but I want it to be centered horizontally on the page. The only thing I get centered is the caption ;-/ ...
20
votes
2answers
606 views

Problem with literate and breaklines=true in listings package

As the title says, the option breaklines=true seems to have an undesired interaction when using literate; in the following example the closing parenthesis doesn't get colorized ...
19
votes
3answers
2k views

Typesetting LaTeX Source Code with the listings package

I am writing some documentation for a LaTeX package, in which I am going to be including some example LaTeX code. What is the best way to typeset that code? I know I could just use verbatim, but it ...
18
votes
4answers
4k views

LaTeX Source Code Listing

I want a code listing, but the code I'm listing is LaTeX. When I simply use: \usepackage{listings} ... \begin{lstlisting} ...LaTeX document goes here... \end{lstlisting} The listing looks like ...
18
votes
2answers
344 views

How can I redefine my macro to accept a charcode instead of a character?

I recently posted this answer to How to highlight all words of the form [0-9][A-Za-z0-9]* immediately following an equal sign?. It works, but it suffers from (a lot) of code duplication. What I have ...
18
votes
1answer
22k views

Listings caption [duplicate]

I'm using the listings package with this \begin command: \begin{lstlisting}[caption=blabla, label=amb] This yields a nice code listing with a caption like "Listing 1.1 blabla". Is there a way to ...
18
votes
1answer
733 views

Latex and code blocks: how to mark statements that way?

I’d like to show you a nice technique used in this book to describe code listings (see pag. 6 and 7 of the PDF -- 116 and 117 of the book), in which relevant statement are labeled with special ...
18
votes
3answers
3k views

Listings, Literate Programming, and Beautiful Typography

Now that I'm procrastinating with tweaking LaTeX, instead of finishing my Ph.D. thesis, I'm wondering what's the most beautiful way to typeset source code (C#, C++, LISP, XML, BNF) in LaTeX. ...
17
votes
3answers
12k views

Shrinking monospace style for listings package

I am working on a thesis and putting together a "pretty" listing. I'm looking at setting up a small font style for a code view. What I have here is: \usepackage{listings} \lstset{language=C, ...
17
votes
6answers
6k views

Highlight text in code listing while also keeping syntax highlighting

I’m using the listings package for code listings. Now I try to highlight some part of the code using \hl from the soul package. Here’s an example of a resulting query where the injected part of a SQL ...
16
votes
3answers
9k views

Using \lstinputlisting to include a file but only certain lines or line ranges

I wanted to tidy up a TeX document and was curious if you can use \lstinputlisting or a similar function to include a certain part of a source-code file. Example, I have a 500 or so line source file ...
16
votes
4answers
3k views

How to automatically skip leading white spaces in listings

This question led to a new package: lstautogobble (lstaddons bundle) For my programming lecture slides I heavily use the listings package with \lstnewenvironment to typeset source code. It's a ...
16
votes
2answers
8k views

How to highlight Python syntax in LaTeX Listings \lstinputlistings command

I am using package listings to import my Python source code into my LaTeX document. I use the command \lstinputlistings. I have a Python source like class MyClass(Yourclass): def __init__(self, ...
16
votes
2answers
7k views

Indent a code listing in LaTeX

For a little LaTeX document I am writing I would like to insert source code fields (in SQL) into my paper. I found out that I can use the listings package for this, which works well so far: ...
16
votes
5answers
11k views

Using bold/italic text inside listings?

How can I use bold text inside a code listing? I wanted to make some parts of the code bold.
16
votes
1answer
863 views

In the minted package, how do I enforce straight single quotes?

Basically I want \mint{python}|'banana'| to produce straight single quotes, rather than the curly ones it normally produces (in pdflatex).
16
votes
1answer
156 views

How to strip a listing of all (line and block) comments without leaving white space?

I'm looking for an easy and robust way to strip all the comments from a listing. My approach (passing \@gobble to the commentstyle key) correctly removes all the one-line comments and block comments. ...
16
votes
1answer
393 views

How to remove the leading unnecessary white spaces when using linerange option?

To be honest, I am looking for people who already read the listings documentation completely. If those people also cannot solve it, then the documentation does not mention anything about it. Sorry for ...
15
votes
1answer
371 views

Is there a listings configuration for TikZ as used in PGF/TikZ manual?

I want to create a tutorial how to use TikZ now. Is there a listings configuration for TikZ as used in PGF/TikZ manual? For example, I want to make \ as an integral part of tikz in \tikz, relevant ...
15
votes
2answers
2k views

Colored background in inline listings

I would like to make inline code more distinguishable from the text surrounding it, and I thought that using a background might work (like it is done on the stackexchange pages.) Since I use the ...
15
votes
3answers
4k views

Phantom spaces in listings (pdf)

I have a problem with SQL code in a PDF document. For code representation I'm using the listings package. Everything works perfect until I copy-paste that code from the created PDF document. I get ...
15
votes
2answers
3k views

\listings code style for HTML5 (CSS, HTML, JavaScript)

I would like to create a code style for HTML5 that can be used with the \listings package. An HTML5 document consists of three languages: CSS, HTML and JavaScript. That makes the whole thing a bit ...
15
votes
2answers
2k views

Coloring digits with the listings package

I'm trying to typeset some python code, and I would like integer literals to be highlighted. I've come up, after about two hours of fighting with the documentation and various other questions here and ...
15
votes
1answer
704 views

listings package changes hyphens to minus signs

The listings package transforms source-code hyphens (-, U+002D) into minus signs (−, U+2212) when using the default proportional fonts, though not when using the default monospaced typewriter font. A ...
15
votes
3answers
3k views

Preferred method for inline code samples

I tend to use the minted package for including code samples within my documents, which produces very nice output. However, because Pygments always produces a verbatim environment there's no way to ...
15
votes
1answer
890 views

How to make listings code correct copyable from PDF and with hyperlink

If I generate listings code with line numbers and copy the result from the PDF I can not use it again as tex code. First the line numbers are included which they should not, second spaces are included ...
15
votes
1answer
238 views

Why are lstlang*.sty files from listings package loaded 3 times?

During compilation of the following MCE: \documentclass{article} \usepackage{listings} \lstset{language=TeX} \begin{document} \begin{lstlisting} Foo \end{lstlisting} \end{document} the lstlang*.sty ...
14
votes
3answers
5k views

How to make overlay still work inside lstlisting environment?

Problem I want to explain C# programming language step by step using overlay in beamer.cls. But it does NOT works as shown in the following figure. How to solve this problem? Code Snippet ...
14
votes
1answer
284 views

Can the listings package automatically indent source code?

The listings package automatically highlights keywords in source code and makes it look nice. Is it possible to automatically indent the program? For example, in listings, if the following code is ...
14
votes
1answer
1k views

Why does LaTeX replace some spaces with brackets or underscores in the listings environment?

I'm using LaTeX on Windows with MikTeX and I'm seeing some odd behaviour. In the output, some of the spaces are replaced with bracket-like underscores. This is the code: \documentclass[10pt]{article} ...
14
votes
1answer
10k views

Source code listing with frame around code?

I'd like to make my code listing looking more sexy. I found a good example on SO, but one problem is remaining: I would like to not only have a horizontal line at the bottom of the listing but also ...
14
votes
1answer
4k views

How can I use LaTeX-commands inside of a listing?

I'm using the listings package in a LaTeX document. It works great, but it doesn't allow me to put LaTeX commands inside of the listing. I would like to use a counter to enumerate some of the ...
14
votes
1answer
202 views

Help defining PDF syntax with listings package

I'm trying to make a pdf language definition for the listings package, but I can't get the string definition to work correctly. This is what I have defined: \lstdefinelanguage{pdf}{ ...
13
votes
3answers
2k views

How to insert a nice tilde in a lstlisting?

How do I insert a nice tilde that is centered in the middle in a lstlisting? I found another thread about tildes but I can't apply it because the method described there only works outside of ...
13
votes
2answers
573 views

Escaping from the escaping hell

Given the following MWE, where the argument for the code function contains an underscore: \documentclass [11pt,oneside,onecolumn]{article} \usepackage{listings} \usepackage{filecontents} ...