4

I am trying to insert a Python code into a LaTeX document. The Python code (an example) is the following and contains many special characters (>, <, °):

valeur = input('Veuillez entrer une valeur numérique ° : ')

if '.' in valeur
    print('Hourra')

        if x<0 or x>10:

Here is a MWE:

\documentclass{article}

\usepackage[latin1]{inputenc} 
\usepackage[T1]{fontenc} 
\usepackage[francais]{babel} 

\usepackage{fancyvrb}

\begin{document}

\inputencoding{utf8}
\begin{otherlanguage*}{english}
\VerbatimInput[frame=single, numbers=left, numbersep=2pt]{test.py}
\end{otherlanguage*}
\inputencoding{latin1}

\end{document}

Because of the special characters, I am getting this error:

enter image description here

How can I fix it?

This question is closely related to the post Issue with accents from Python source code but it is no duplicate since I was dealing with accents and now I am seemingly dealing with mathematical symbols used outside math mode...

The answer to Issue with accents from Python source code does not solve my current problem.

  • possible duplicate of Issue with accents from Python source code – Romain Picot Sep 10 '15 at 14:29
  • It is not a duplicate since the problem is different. – Melian Sep 10 '15 at 14:29
  • In Issue with accents from Python source code, the problem was with the accents. Here, it is with math symbols used outside math mode... – Melian Sep 10 '15 at 14:30
  • Ok, I was missinterpretead. You should modify your MWE to use only math character that causes the failure without the accent – Romain Picot Sep 10 '15 at 14:32
  • It's my interpretation of MWE perhaps. Everything that can be removed because not causing the failure should be removed – Romain Picot Sep 10 '15 at 14:38
5

You have to declare that you want to use UTF-8. For the degree symbol you also need textcomp.

\documentclass{article}

\usepackage[T1]{fontenc} 
\usepackage[utf8,latin1]{inputenc}  % last one is default
\usepackage[english,francais]{babel}
\usepackage{textcomp}

\usepackage{fancyvrb}

\begin{document}

\inputencoding{utf8}
\begin{otherlanguage*}{english}
\VerbatimInput[frame=single, numbers=left, numbersep=2pt]{test.py}
\end{otherlanguage*}
\inputencoding{latin1}

\end{document}

enter image description here

Why not using UTF-8 also for the other files?

  • It does not work with my LaTeX editor. I don't know why... – Melian Sep 10 '15 at 14:35
  • Did not know I could mix input encodings within a document. Great solution, but the listings package may be better for typesetting code than fancyvrb as listings can mark keywords and more. – Rüdiger Voigt Sep 10 '15 at 16:45
  • out of curiosity, how would you disable the pretty-quoting for code blocks? – Nick T Sep 10 '15 at 19:33
  • @NickT \usepackage{upquote} – egreg Sep 10 '15 at 19:35
1

Don't use plain verbatim for code, use one of the specialized packages. I'm partial to listings, but there are many others; minted uses Python's Pygments to typeset code and is reported to be very flexible.

  • That is what I mentioned in a previous comment, there is packages for this kind of things – DRi Sep 11 '15 at 7:55

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.