3

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.

3

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):

\documentclass{article}
\usepackage{listings}

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

\begin{document}
\inputmatlabcode{hello_world.m}
\end{document}

The result will be like: enter image description here

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.