Given the following files:
book.tex
\documentclass{memoir}
\usepackage{listings}
\begin{document}
\lstlistoflistings
\chapter{Hello, world!}
\lstinputlisting{C\#/hello.cs}
\end{document}
C#/hello.cs
using System;
class Hello {
static void Main(string[] args) {
Console.WriteLine("Hello, world!");
}
}
What is the correct syntax to include file C#/hello.cs
as a listing in book.tex
?
I tried these variants, both fail:
\lstinputlisting{C#/hello.cs}
\lstinputlisting{C\#/hello.cs}
When I try the first variant, \lstinputlisting{C#/hello.cs}
, I get the following error:
! Illegal parameter number in definition of \reserved@a.
<to be read again>
/
l.7 \lstinputlisting{C#/hello.cs}
?
When I try the second variant, \lstinputlisting{C\#/hello.cs}
, I get the following error:
! Missing \endcsname inserted.
<to be read again>
#
l.7 \lstinputlisting{C\#/hello.cs}
?
I get the same behavior with (almost) identical error messages in lualatex
, pdflatex
, and xelatex
.
I've tried to find a solution by reading the documetnation of the listings package https://mirror.mwt.me/ctan/macros/latex/contrib/listings/listings.pdf. Alas, in vain.
What is the correct way to use the listings package to include a file that has a #
-character in its pathname? (As a workaround, I could of course rename the directory from C#
to Csharp
, but I'd like to solve the problem, not avoid it.)