0

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'

4
  • Welcome to TeX.SX! Could you maybe provide a minimal working example, i.e. a compilable document that can be used to reproduce your issue? This way, people may be better able to help you. Aug 24, 2018 at 18:33
  • Done, hope that makes things more clear. Aug 24, 2018 at 18:47
  • The compile error appears because of the option upquote = true. If you put it equal to false, the compilation will be successful. Probably, this option is to turn LaTeX from "verbatim mode" to normal mode for quoted text.
    – Vladimir
    Aug 24, 2018 at 19:48
  • Thank you, that seems to be exactly the problem! I omitted the upquote = true option and included the textcomp package to keep the straight quotes within the source code. Aug 24, 2018 at 20:01

1 Answer 1

1

After using the solution from @Vladimir yesterday, I managed to get my TeX to work. I still want to document it in a thorough way and add a small "lesson learned" here:

  1. As Vladimir correctly pointed out, upquote = true in \lstset was the root cause of this problem. Setting upquote = false solved the issue, but seemed to cause other issues (non-straight upquotes)
  2. To solve the new issue of non-straight upquotes, I included the package textcomp - Now everything works just like it should.
  3. To write HEX-characters in the text (e.g. "This is some text explaining \x41.") \textbackslash works fine: \texttt{\textbackslash x41}

I hope this helps in case someone runs into similar issues.

You must log in to answer this question.

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