# UTF-8 error with verbments

I'm writing a book using Latex in what I want to write code in Python. My operating system is Linux and my editor is Kile with:

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{verbments}


But I can not see the characters in Spanish

\begin{pyglist}[language = python, encoding = utf8]
print ('Tomorrow is another day')
\end{pyglist}


I perform the compilation with the command

pdflatex -shell-escape -interaction = nonstopmode


but I get the following error.

*** Error while highlighting:
UnicodeDecodeError: 'utf-8' codec can not decode byte 0xf1 in position 9: invalid continuation byte
(file "/usr/lib/python3.3/site-packages/pygments/lexer.py", line 164, in get_tokens)


I also have observed that verbments generates a file called name.pyg.sty that curiously is codified in ISO-8859-15 instead of UTF-8 as all my other files.

verbments, like minted and pythontex, uses fancyvrb behind the scenes. pythontex patches fancyvrb to support Unicode, as does the development version of minted (but not the version on CTAN). You can use pythontex or the development version of minted. Or you can just copy over the fancyvrb patch. Basically, when fancyvrb writes the code to a temp file, it needs to \detokenize everything so that Unicode works.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{verbments}

\makeatletter
\newcommand{\verbments@write@detok}[1]{%
\immediate\write\FV@OutFile{\detokenize{#1}}}
\newcommand{\verbments@FVB@VerbatimOut}[1]{%
\@bsphack
\begingroup
\FV@UseKeyValues
\FV@DefineWhiteSpace
\def\FV@Space{\space}%
\let\FV@ProcessLine\verbments@write@detok
\immediate\openout\FV@OutFile #1\relax
\let\FV@FontScanPrep\relax
\let\@noligs\relax
\FV@Scan}
\let\FVB@VerbatimOut\verbments@FVB@VerbatimOut
\makeatother

\begin{document}

\begin{pyglist}[language = python]
print('Tomorrow is another day é ñ')
\end{pyglist}

\end{document}

• Thank you very much, it seems you have solved my problem. – Mig Aug 23 '14 at 14:06

I've not been able to compile successfully with either verbments or minted. However, if you have a fairly recent TeX distribution, pythontex works:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{pythontex}

\begin{document}
\begin{pygments}{python}
print ('Tomorrow is another day é ñ')
\end{pygments}
\end{document}


You have to run pdflatex and then pythontex over the main file. Another run of pdflatex will include the formatted listing.

• Thank you very much, your suggestion works, but I prefer to patch the verbments package. – Mig Aug 23 '14 at 14:11

I had no luck with verbments on pdflatex. The error message was very close to the one seen by the original poster. Also an ISO 8859-1(5) coded <name>.pyg.lst file was generated.

Changing the compiler to lualatex --shell-escape worked fine with the following document:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{luainputenc}
\usepackage{verbments}

\begin{document}
\begin{pyglist}[language = python, encoding = utf8]
print ('Tomorrow is another day é ñ')
\end{pyglist}
\end{document}