I am currently writing a paper where I need to put full Python source code files into the appendix. Since I do not want to bloat my TeX file, I include source code with \lstinputlisting
. The source code contains a few strings with HEX-Characters (e.g. input = "\x41\x42"
) and regular "code characters" like _
, '
and $
.
When running the TeX compiler (tried with Kile's xelatex and pdflatex) I get errors like "Undefined control sequence hex = '\" or even "Package inputenc Error: Invalid UTF-8 char" when the compiler tries to parse Python comments containng umlauts.
I already tried to use rangeprefix = \\
in \lstset
but to no avail. Any pointers on how to successfully include Python source code with HEX-chars (and some umlauts)?
(And I'd love to avoid switching to minted
due to the apparent configuring overhead)
Sample .tex
\documentclass [11pt,oneside,onecolumn]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{csquotes}
\usepackage{hyperref}
\usepackage[final]{listings}
\lstset{
basicstyle = \footnotesize\ttfamily,
breaklines = true,
captionpos = b,
numbers = left,
stepnumber = 1,
numberfirstline = true,
numberstyle = \footnotesize,
xleftmargin = 1.0ex,
upquote = true,
showlines = true,
frame = single,
escapeinside = {(*@}{@*)},
showspaces = false,
showstringspaces= false,
rangeprefix = \\,
morecomment = [s]{"""}{"""}
}
\lstloadlanguages{
Python
}
\begin{document}
\section{Source code}
\section{sample.py}
\lstinputlisting[
language = Python,
caption = {sample.py},
label = {lst:sample}
]{code/sample.py}
\end{document}
sample.py
my_start = 2550
some_chars = '\x41' * my_start
Sample Errors
paper.tex:85:Undefined control sequence chars = '\x41'
upquote = true
. If you put it equal tofalse
, the compilation will be successful. Probably, this option is to turn LaTeX from "verbatim mode" to normal mode for quoted text.upquote = true
option and included thetextcomp
package to keep the straight quotes within the source code.