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 :


I get the following error :

! Package Listings Error: File `abcd/test\unhbox \voidb@x \penalty \@M \hskip \ z@skip \unhbox \voidb@x \kern .06em\vbox {\hrule width.3em}\discretionary {-}{} {}\penalty \@M \hskip \z@skip file(.c)' not found.

The underscore is obviously not handled correctly, but I have not been able to find how to correct this so far (tried escaping it with "\_"). Does anyone have any idea on how to solve this?

Test code (works in minimal version, stopped working with "underscore" package) :

% Declarations
\documentclass[a4paper, 11pt,twoside]{article}

% Code listing

% Begin document

Listing of test\_file.c :



The "underscore" package can help you when you have a lot of underscores to escape in your text, but you have to be careful with file names, cite or ref tags :

The skips and penalties ruin any kerning with the underscore character (when a character is used). However, there doesn’t seem to be much, if any, such kerning in the EC fonts, and there is never any kerning with a rule. You must avoid “_” in file names and in cite or ref tags, or you must use the babel package, with its active-character controls, or you must give the [strings] option, which attempts to redefine several commands (and may not work perfectly). Even without the [strings] option or babel, you can use occasional underscores like: “\include{file\string_name}”.



Escape the underscore in the caption argument, but not in the mandatory argument:

%                                            ^^^^^             ^^^^^
| improve this answer | |
  • I already tried this and it did not work. I should have said this in the description :-s – BenC Nov 17 '11 at 17:37
  • OK, fixed. Only escape the caption argument. – Kerrek SB Nov 17 '11 at 17:40
  • It still does not work for me. I've tried "\lstinputlisting{abcd/test_file.c}" as a minimum command, with or without escaping the underscore, and I still get the same error. Could it have something to do with the encoding packages I use? ([utf8x]{inputenc}, [french]{babel}) – BenC Nov 17 '11 at 17:46
  • 3
    My answer stands; don't escape the underscore in the argument. – Kerrek SB Nov 17 '11 at 19:04
  • 1
    Haha. The joys, eh? – Kerrek SB Nov 17 '11 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.