我正尝试做和这里差不多的事情使用 PythonTeX 排版函数然后在控制台中执行它们pythontex
,但我想使用一些 unicode 字符(法语重音符号:é、è 等)。但是,在这种情况下(在运行期间),那里提供的答案不起作用。
请参阅以下 MWE(如果我更改Résultat
,Resultat
它可以正常工作):
\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}
(我正在使用 WinPython 提供的 PDFLaTeX 和 Python 3。)
您有什么想法可以让它发挥作用吗?
PS:运行 pythontex 时我收到的完整消息是:
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
答案1
这是因为VerbatimOut
环境(来自fancyvrb
)没有正确支持 UTF-8。您可以通过在前言中添加以下内容来修补它:
\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