{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

1
vote
1answer
2k views

Listing of a file with underscores in its name/path

I am writing a report with LaTeX and I need to list some source code. One of the files has some underscores in its path/name. When compiling the following line : ...
1
vote
1answer
377 views

Why does pdfLaTeX fail when I try to use `begin{lstlisting}` inside a user-defined environment?

I'm having trouble with the listings package. I can't compile the following minimal example through pdfLaTeX: \documentclass[a4paper, 11pt, twoside, openright, english]{memoir} \usepackage{listings} ...
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 ...
16
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 ...
12
votes
1answer
955 views

Automatic background coloring in listings using tikzmark

I am authoring Beamer presentations that feature numerous code listings, and I am required to highlight portions of code with semi-transparent balloons. Yet I've come up with a solution that uses a ...
16
votes
1answer
158 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. ...
11
votes
2answers
675 views

tcolorbox - list of listings?

In a book with figures, tables and listings, I figured out that all listings and some tables and figures look better when presented in a tcolorbox. The book has the usual tables of contents: ...
6
votes
2answers
2k views

Copy-pasting leading whitespace and blank lines in listings package (pdf) [duplicate]

I'm trying to use the listings package to typeset some code in a PDF document, in a way that allows me to copy-paste the resulting code. This mostly works well, if ...
6
votes
1answer
567 views

How to Format C Program Code Block Nicely

I am using the following settings to format my code blocks: % xxxxxxxxxxxxxxxxxxxxxxxxx Code Snippet STARTS xxxxxxxxxxxxxxxxxxxxxx \lstset{ language=C, % choose the language of ...
6
votes
2answers
2k views

Create a \lstnewenvironment that creates named listings

I am using the \lstnewenvironment to format a set of code listings in my document. \lstnewenvironment{code} {\lstset{language=haskell, basicstyle=\small\ttfamily, numbers=left, ...
12
votes
3answers
4k views

Avoid page breaks in \lstlistings

I'm trying to avoid page breaks in my lstlisting (seen in section 0.0.2) I know I can remedy this by floating my lstlisting, but it adds some unwanted white space (seen in section 0.0.1). Is ...
10
votes
2answers
177 views

Maintainable typesetting of placeholders in code snippets

I'm looking for a maintainable way of typesetting placeholders in code snippets. In my listings, I would like that anything contained between the two delimiters @< and >@ be typeset in ...
10
votes
2answers
5k views

How to align source code produced by listings package with main paragraph text

I'm using the latex listings package for the first time to format some HTML. I'm noticing that by default, the html code is being pushed further left than the main paragraph text of my document. Is ...
9
votes
2answers
398 views

How to deal with very long lstinline-phrases like long class names?

According to this Question: listing: How to deal with very long lines that do not contain spaces? My problem is that my \lstinline-phrases does not contain any special character like "-". In my ...
9
votes
1answer
11k views

How do I add syntax coloring to my C++ source code in beamer

I have just learned how to insert source code into beamer with the listings package. However I dont know how to go about adding any syntax coloring on the C++ source code. How do I do that? Here ...
6
votes
1answer
582 views

Unequal letter spacing in 'listings'

I am wondering why in the pdf of the example below the letters of the word "function" and the word "sum" seem to have different spacing. This somehow looks ugly. Apart from that, I am wondering if ...
5
votes
1answer
960 views

Code listing in subfigure

I am trying to use the subfigure package of LaTeX to display a code listing. Here was my first attempt: \begin{figure} \subfigure[Standard approach]{ {\footnotesize \begin{verbatim} var caps = new ...
3
votes
1answer
320 views

Background images for code listings in beamer

I asked this question originally in attempt to get background images behind my code listings. Background images for code listings What I did not mention is that I am using the beamer package for ...
14
votes
1answer
205 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}{ ...
11
votes
1answer
1k views

listings package: no numbering in case the code has only one line

I have a custom style in lstlisting with numbers on the left and the number step is set every line. I would want to know how the listings could appear without number in case the listing has only one ...
11
votes
3answers
584 views

Why can't I use \newfloat more than once?

I've found the float package, which lets me specify \newfloat{program}{...} to get other named floats than Table and Figure. However, if I try to use this more than once, I get a cryptic error: ! You ...
10
votes
1answer
479 views

Draw comparison lines between two listings

I have a LaTeX code set up as so: \documentclass[a4paper,10pt]{report} \usepackage[utf8]{inputenc} \usepackage{fullpage} \usepackage{listings} \usepackage{graphicx} \usepackage{float} ...
10
votes
1answer
6k views

How to make inline listings wrap nicely?

Does anyone know how to make inline listings wrap nicely? Take a look at the screenshot that I attached below. The code example in the second line goes outside the text margin. Also, any ideas why ...
9
votes
3answers
3k views

Typesetting UTF8 listings with German Umlaute

I'm having some issues with listings that include German Umlaute. The following example code generates a completely messed up PDF. \documentclass{article} \usepackage[utf8]{inputenc} ...
8
votes
2answers
729 views

Highlighting haskell listings in large *.tex document [duplicate]

Suppose, there is a large *.tex document, which has many nested \input directives with many Haskell listings surrounded by \begin{code} \end{code} commands. How can all this code listings be ...
8
votes
3answers
5k views

How to put C++ source code into beamer slides [duplicate]

Possible Duplicate: How display LaTeX code in LaTeX document? How do I fit java style source code in one frame in beamer? How do I add C++ formatted and coloured source code to my beamer ...
8
votes
1answer
2k views

How to define a new environment with a fancy box (tikz) around sample source-code (listings)?

This is what I want to do: \documentclass{beamer} \begin{document} \begin{frame}[fragile]{Frame 1} Some \structure{Python} code here: % I want to define a new "pythoncode" environment, so ...
8
votes
1answer
865 views

Emphasize word beginning with uppercase letters in code with lstlisting package

Hi I am using the lstlisting package to format my Prolog code within my tex file. In Prolog all variables begin with an uppercase letter. I would like to emphasize them. Is there a way to for example ...
7
votes
1answer
868 views

Source Code Margin Comments

In the paper "Beautiful Code Compelling Evidence", by J.R. Heard -- Code is formated in a particular way which includes margin comments. I am looking for a way to do this as I think it is a very clear ...
6
votes
1answer
107 views

Aligning and centering a lstlisting with two columns on a page

Consider the following code taken from here to produce a fancy listing : % PACKAGES \documentclass[11pt,a4paper]{book} \usepackage[top=1in, bottom=1in, left=1in, right=1in]{geometry} ...
6
votes
1answer
2k views

Problems with the lstlisting environment (margin and white line)

Consider the following example: \documentclass{article} \usepackage{listings} \usepackage{color} \begin{document} \definecolor{light-gray}{gray}{0.95} \lstset{columns=fullflexible, ...
6
votes
1answer
4k views

First line number in lstinputlisting environment

I use the following code to display line 23-45 of sort.cpp: \lstinputlisting[language=C++,linerange=23-45]{sort.cpp} However, the line numbering starts with 1 while I want it start at 23. I found ...
6
votes
3answers
3k views

LaTeX beamer: Code listings in notes

How can I add a code listing to a beamer note slide? I tried the following which doesn't work: \note{{\tiny{This is a note \begin{lstlisting} foo bar \end{lstlisting}}} Any ...
5
votes
1answer
594 views

Remove\begin{Schunk} \begin{Sinput}

I am using Sweave to produce my tex. In addition I use lstlisting to highlight the source code as below. \begin{lstlisting}[caption={Computation $\widehat Y_4$ and its ...
4
votes
1answer
943 views

Custom line numbers in lstlisting environment

I am using the lstlisting environment to display code and it works great, however for my current application I need something like: line 1; 3.1: line 2; line 3; 3.1.5: line 4; ...
3
votes
1answer
64 views

listings using tcolorbox package without Chapter and Section environment effects

I want to get listings using tcolorbox package but without counting Chapter and Section environments. See in MWE, I used section environment withing listings which effects the counter for following ...
2
votes
0answers
85 views

break line at comma inside listings [closed]

I'm trying to do some listings environments, but I'm encountering problems. The ideal output is this: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,⤦ ⤷ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
2
votes
1answer
270 views

How to define HTML keywords in \listings

I'm working on the implementation of a HTML5 code style for \listings. Currently I have the problem that there are different ways to define HTML tags. In my example, I don't get /p in ...
2
votes
1answer
597 views

vim type Display for C Code in Latex

Target Display: My Latex Code: % xxxxxxxxxxxxxxxxxxxxxxxxx Code Snippet STARTS xxxxxxxxxxxxxxxxxxxxxx \lstset{ language=C, % choose the language of the code stepnumber=1, ...
1
vote
1answer
422 views

Listings: new environments with arguments

I have this basic question. How can use arguments with listings? I tried this and it doesn't work: \lstnewenvironment{cor}[2][] {\linerange{#1-#2},\lstset{language=[Sharp]C,,basicstyle=\scriptsize, ...
15
votes
1answer
712 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 ...
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 ...
10
votes
2answers
2k views

Listings: different counters for different listing environments

How do I create two lstlisting environments that each have its own counter? If I use for example \lstnewenvironment{algorithm}[2]{ \renewcommand\lstlistingname{Algorithm} \lstset{ ... } } ...
9
votes
1answer
91 views

How can I highlight \[…\] properly in LaTeX listings?

The listings package provides the option moredelim, allowing for custom styles for words enclosed by given delimiters. I would like to give $...$, \[...\] and $$...$$ custom colors. \[...\], however, ...
9
votes
2answers
235 views

Fixing lstlisting inside exsheets question and solution environments

I am trying to use exsheets to prepare the tests for Foundation of Programming exam. I need to include snippets of C code in some question and solution and I am going to use listings (with the options ...
9
votes
1answer
425 views

Label inside a code listing

Does anybody know how to use labels inside a code listing that it looks something similarly like this: I know that it's easier to use the line number but I prefer using some labels.
8
votes
1answer
253 views

How can I automatically highlight SuperCollider symbols and environment variables?

This question led to a new package: sclang-prettifier I've been poring over the listings manual, trying to figure out how to get some very specific types of formatting for the SuperCollider ...
8
votes
1answer
130 views

Hyperref adds vertical space to listings

When I input a listing with \lstinputlisting from the listings package, I get an additional vertical space, when I use this in combination with the hyperref package. As the picture shows, a certain ...
8
votes
1answer
219 views

Line breaks in listings

Is there a way like wrap the line after at most 60 letters but it should not break inside some commands like listings does? Example: \documentclass[a5paper]{article} \usepackage[T1]{fontenc} ...
8
votes
2answers
1k views

Full width code listing within two column text

I am using multicols to create two columns for body text. For code typesetting I am using the listings package. For the latter I have created an environment using \lstnewenvironment command, used as ...