I use this macro to include Matlab code in my document:

\newrobustcmd{\inputmatlabcode}[1]{\lstinputlisting[basicstyle={\scriptsize \ttfamily}, caption=#1, language = matlab]{#1}}

It fails when I try to include code files with underscores in their names. I saw this question but since I'm referring to the file name in both the caption and the argument to listings, and only need to escape the underscore in the caption.

The underscore package will break the rest of my document, I think, so I'm trying to avoid using that.


1 Answer 1


Maybe you don't need underscore package. You can use \detokenize to process underscores properly:

If you create a hello_world.m file with some lines in your work directory, here is an MWE (also I'd like to remind you to attach an MWE when you're asking a question):




The result will be like: enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .