3

I'm trying to do pretty much the same thing as in here Typeset functions with PythonTeX and execute them in console afterwards, except that I'd like to use some unicode characters (French accents: é, è, etc.). However, the answer provided there fails to work in that case (during the pythontex run).

See the following MWE (where if I change Résultat in Resultat, it works fine):

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

\newenvironment{pyconcodeblock}%
 {\VerbatimEnvironment
  \begin{VerbatimOut}{temp.py}}%
 {\end{VerbatimOut}%
  \pyconc{exec(compile(open('temp.py', 'rb').read(), 'temp.py', 'exec'))}%
  \inputpygments{python}{temp.py}}

\begin{document}

\begin{pyconcodeblock}
def foo(x):
    print("Résultat :", 2*x)
\end{pyconcodeblock}

\begin{pyconsole}
x = 10
foo(x)
\end{pyconsole}

\end{document}

(I'm using PDFLaTeX and Python 3 provided by WinPython.)

Would you have any ideas to make this works?

PS: the entire message I get when running pythontex is:

This is PythonTeX 0.15
multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
  File "C:\python\WinPython-64bit-3.4.3.2\python-3.4.3.amd64\lib\multiprocessing
\pool.py", line 119, in worker
    result = (True, func(*args, **kwds))
  File "c:\texlive\2016\texmf-dist\scripts\pythontex\pythontex3.py", line 2273,
in do_pygments
    content = f.read()
  File "C:\python\WinPython-64bit-3.4.3.2\python-3.4.3.amd64\lib\codecs.py", lin
e 319, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 24: invalid
 continuation byte
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "c:\texlive\2016\texmf-dist\scripts\pythontex\pythontex.py", line 62, in
<module>
    pythontex.main()
  File "c:\texlive\2016\texmf-dist\scripts\pythontex\pythontex3.py", line 2676,
in main
    do_multiprocessing(data, temp_data, old_data, engine_dict)
  File "c:\texlive\2016\texmf-dist\scripts\pythontex\pythontex3.py", line 1369,
in do_multiprocessing
    result = task.get()
  File "C:\python\WinPython-64bit-3.4.3.2\python-3.4.3.amd64\lib\multiprocessing
\pool.py", line 599, in get
    raise self._value
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 24: invalid
 continuation byte
C:\texlive\2016\bin\win32\runscript.tlu:679: command failed with exit code 1:
python.exe c:\texlive\2016\texmf-dist\scripts\pythontex\pythontex.py test.tex
  • I am a Python ignorant but at least in the pycode environment the stringu"Résultat :" (note the prefix u) is printed correctly, so I see Résultat : 20 in the PDF. – Fran Sep 11 '16 at 19:12
2

This is due to the VerbatimOut environment (from fancyvrb) not properly supporting UTF-8. You can patch it by adding this to your preamble:

\makeatletter
\def\FVB@VerbatimOut#1{%
  \@bsphack
  \begingroup
  \FV@UseKeyValues
  \FV@DefineWhiteSpace
  \def\FV@Space{\space}%
  \FV@DefineTabOut
  \def\FV@ProcessLine##1{\immediate\write\FV@OutFile{\detokenize{##1}}}%
  \immediate\openout\FV@OutFile #1\relax
  \let\FV@FontScanPrep\relax
  \let\@noligs\relax
  \FV@Scan}
\makeatother
  • No, it doesn't work; in the code part I get Resultat without the accented letter and, worse, the console prints R\xc3\xa9sultat – egreg Sep 11 '16 at 9:14
  • @egreg This is working well for me (I do have, however, to manually delete the pythontex-files directory after any change in the pyconcodeblock environment, otherwise the change doesn't appear). – mathieu2l Sep 11 '16 at 10:42
  • @mathieu2l I still get R\xc3\xa9sultat in the console output. – egreg Sep 11 '16 at 10:48
  • @egreg Could this be related to the version of Python? (I never used Python 2 but it is my understanding that Python 2 doesn't provide natively a full UTF-8 support... not sure) – mathieu2l Sep 11 '16 at 11:04
  • 1
    @mathieu2l Having the code update automatically isn't possible with the temp file approach. I hope to add proper support for mixing normal code and console code eventually, but until then these sorts of things are really forcing the console support to do things it was never designed for. – G. Poore Sep 14 '16 at 1:03

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.