gpoore/pythontex

Package inputenc Error: Unicode character when using pythontex + siunitx

Grimler91 opened this issue · 2 comments

Hi,

Consider this mwe (compiled with texlive 2020 and pythontex 0.17):

\documentclass{article}
\usepackage{pythontex}
\usepackage{siunitx}
\DeclareDocumentCommand{\pySI}{ m m }
{%
\py|'\\SI{' + str(round(#1,2)).replace('(','').replace(')','') + '}{#2}'|%
}
\begin{document}
\pySI{1}{\volt}
\end{document}

When compiling it fails with

! Package inputenc Error: Unicode character � (U+000B)
(inputenc)                not set up for use with LaTeX.

See the inputenc package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.9 \pySI{1}{\volt}

The culprit is \v, which can be seen by visiting pythontex-files-mwe/mwe.pytxmcr which contains:

%Last time of file creation:  1599140181.6780906

\pytx@SVMCR{pytx@MCR@py@default@default@0}
\SI{1}{�olt }\endpytx@SVMCR

Other units (that does not start with "v") work fine.
My current workaround is to do \pySI{1}{\\volt} instead of \pySI{1}{\volt}.

Why is \v an issue, and not for example \m in \meter? Is there a list of problematic characters somewhere?

Full pythontex + latex log
This is PythonTeX 0.17

--------------------------------------------------
PythonTeX:  mwe - 0 error(s), 0 warning(s)

This is pdfTeX, Version 3.14159265-2.6-1.40.21 (TeX Live 2020) (preloaded format=pdflatex)
 restricted \write18 enabled.
entering extended mode
(./mwe.tex
LaTeX2e <2020-02-02> patch level 5
L3 programming layer <2020-06-03>
(/usr/local/texlive/2020/texmf-dist/tex/latex/base/article.cls
Document Class: article 2019/12/20 v1.4l Standard LaTeX document class
(/usr/local/texlive/2020/texmf-dist/tex/latex/base/size10.clo))
(/usr/local/texlive/2020/texmf-dist/tex/latex/pythontex/pythontex.sty
(/usr/local/texlive/2020/texmf-dist/tex/latex/fvextra/fvextra.sty
(/usr/local/texlive/2020/texmf-dist/tex/latex/base/ifthen.sty)
(/usr/local/texlive/2020/texmf-dist/tex/latex/etoolbox/etoolbox.sty)
(/usr/local/texlive/2020/texmf-dist/tex/latex/fancyvrb/fancyvrb.sty
(/usr/local/texlive/2020/texmf-dist/tex/latex/graphics/keyval.sty))
(/usr/local/texlive/2020/texmf-dist/tex/latex/upquote/upquote.sty)
(/usr/local/texlive/2020/texmf-dist/tex/latex/lineno/lineno.sty))
(/usr/local/texlive/2020/texmf-dist/tex/generic/xstring/xstring.sty
(/usr/local/texlive/2020/texmf-dist/tex/generic/xstring/xstring.tex))
(/usr/local/texlive/2020/texmf-dist/tex/latex/pgfopts/pgfopts.sty
(/usr/local/texlive/2020/texmf-dist/tex/latex/pgf/utilities/pgfkeys.sty
(/usr/local/texlive/2020/texmf-dist/tex/generic/pgf/utilities/pgfkeys.code.tex
(/usr/local/texlive/2020/texmf-dist/tex/generic/pgf/utilities/pgfkeysfiltered.code.tex)))) 
(/usr/local/texlive/2020/texmf-dist/tex/latex/newfloat/newfloat.sty) 
(/usr/local/texlive/2020/texmf-dist/tex/latex/currfile/currfile.sty
(/usr/local/texlive/2020/texmf-dist/tex/latex/kvoptions/kvoptions.sty
(/usr/local/texlive/2020/texmf-dist/tex/generic/ltxcmds/ltxcmds.sty)
(/usr/local/texlive/2020/texmf-dist/tex/generic/kvsetkeys/kvsetkeys.sty))
(/usr/local/texlive/2020/texmf-dist/tex/latex/filehook/filehook.sty)))
(/usr/local/texlive/2020/texmf-dist/tex/latex/siunitx/siunitx.sty
(/usr/local/texlive/2020/texmf-dist/tex/latex/l3kernel/expl3.sty
(/usr/local/texlive/2020/texmf-dist/tex/latex/l3backend/l3backend-pdfmode.def))
(/usr/local/texlive/2020/texmf-dist/tex/latex/l3packages/xparse/xparse.sty
(/usr/local/texlive/2020/texmf-dist/tex/latex/l3packages/xparse/xparse-generic.tex)) 
(/usr/local/texlive/2020/texmf-dist/tex/latex/amsmath/amstext.sty
(/usr/local/texlive/2020/texmf-dist/tex/latex/amsmath/amsgen.sty))
(/usr/local/texlive/2020/texmf-dist/tex/latex/tools/array.sty)
(/usr/local/texlive/2020/texmf-dist/tex/latex/l3packages/l3keys2e/l3keys2e.sty)
(/usr/local/texlive/2020/texmf-dist/tex/latex/translator/translator.sty))
(/usr/local/texlive/2020/texmf-dist/tex/latex/xcolor/xcolor.sty
(/usr/local/texlive/2020/texmf-dist/tex/latex/graphics-cfg/color.cfg)
(/usr/local/texlive/2020/texmf-dist/tex/latex/graphics-def/pdftex.def))
(./mwe.aux) (./pythontex-files-mwe/mwe.pytxmcr)
(/usr/local/texlive/2020/texmf-dist/tex/latex/translator/translator-basic-dictionary-English.dict)
(/usr/local/texlive/2020/texmf-dist/tex/latex/siunitx/siunitx-abbreviations.cfg) 
(/usr/local/texlive/2020/texmf-dist/tex/context/base/mkii/supp-pdf.mkii
[Loading MPS to PDF converter (version 2006.09.02).]
) (./pythontex-files-mwe/mwe.pytxpyg)

! Package inputenc Error: Unicode character � (U+000B)
(inputenc)                not set up for use with LaTeX.

See the inputenc package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.9 \pySI{1}{\volt}

The issue here is the '}{#2}'. You are putting backslashes into a Python string, and they will escape the next character if that creates a valid escape sequence. If you want backslashes to be literal, then you'll want a raw string like r'}{#2}'. Of course, that can have its own issues under some circumstances, but if #2 is more or less typical, you probably won't need to worry about it.

Thanks! Changing to raw strings everywhere for all strings where I might use \ works indeed.

Thanks for the quick response!