3

I've written a program in python, about 2500 lines in total, that I wish to include segments from into my report. Normally I use listings to do that but in this case, the .py files include many unicode characters which are necessary for the understanding of the files.

These characters, e.g. ⇒, cause these errors in latex

! Package inputenc Error: Unicode char �\lst@FillFixed@ (U+5B4) (inputenc) not set up for use with LaTeX.
! Package inputenc Error: Keyboard character used is undefined (inputenc) in inputencoding `utf8'.

MWE:

\documentclass[a4paper, 11pt]{report}

\usepackage[utf8]{inputenc} 
\usepackage[english]{babel} % English language/hyphenation
\usepackage{xcolor}
\usepackage{listings}
\usepackage[T1]{fontenc}

\begin{document}
\chapter{Fists Chapter}
Some text
\lstinputlisting[language=Python, inputencoding=utf8, extendedchars=true]{code.py}
\end{document}

.py file

def Code(self):
    print("Some code with comments"):
    # Comments with unicode characters 
    # β∠ζπ⇒
  • It's not easy. check for example tex.stackexchange.com/questions/378600/… (I have a literate set for Spanish language, that's quite easy --- but doing that for a lot of chars is difficult). – Rmano Nov 16 '18 at 11:29
  • 4
    For full Unicode support you should probably be using XeLaTeX or LuaLaTeX instead of pdfLaTeX. If I remove \usepackage[utf8]{inputenc} and \usepackage[T1]{fontenc}, switch to \usepackage{fontspec}\setmainfont{Linux Libertine O} and compile with LuaLaTeX the code prints as expected. Oh, and if you are a Python person anyway, you may want to try minted instead of listings: tex.stackexchange.com/q/300719/35864 – moewe Nov 16 '18 at 11:31
  • Related: tex.stackexchange.com/q/25391/35864 – moewe Nov 16 '18 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.