2

I am inserting a Python source code in a LaTeX document using the fancyvrb package. Here is a MWE:

\documentclass{article}

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

\usepackage{fancyvrb}

\begin{document}

\VerbatimInput[frame=single, numbers=left, numbersep=2pt]{test.py}

\end{document}

test.py is

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

# Il s'agit d'un texte à la fois très accentué et très court.

The issue is that upon compilation I obtain the following.

enter image description here

How can I get the accents right?

4

The Python source code is encoded in UTF-8, not latin1. This can be managed by changing the input encoding via \inputencoding.

Also the babel's frenchb.ldf interferes. This can be circumvented by setting a different language.

Full example:

\documentclass{article}

\usepackage[utf8,latin1]{inputenc}

\usepackage[T1]{fontenc}
\usepackage[english,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}

Result

utf8 must be loaded in the preamble once, therefore the two encoding options for inputenc. Also \inputencoding{utf8} contains global assignments, thus it cannot be limited by a group unlike the other encodings.

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.