0

I have the following LaTeX code:

% !TeX TS-program = xelatex
\documentclass{article}
\usepackage[EU1]{fontenc}
\usepackage{fontspec}
\setmainfont{Times New Roman}
\setsansfont{Arial}
\setmonofont{Courier New}
\usepackage{listings}  % or listingsutf8
\lstset{%
    language=Python,
    basicstyle=\small\ttfamily,
    numbers=left,
    tabsize=4,
    showstringspaces=false,
    firstnumber=last,
    frame=leftline,
    inputencoding=utf8,
}

\begin{document} \lstinputlisting[linerange={1-24}]{./code.py} \lstinputlisting[linerange={25-31}]{./code.py} \end{document}

The code.py is the following (I've added comments to clarify the symbols used):

class Config:
    @staticmethod
    def _unit_to_inches(s: str) -> float:
        s = s.strip()
        conversions: Final[Dict[str, float]] = {
            '″': 1.0,  # one double prime mark (U+2033)
            '′′': 1.0,  # two prime marks (U+2032)
            '"': 1.0,  # quotation mark (U+0022)
            "''": 1.0,  # two apostrophes (U+0027)
            'in': 1.0,
            'inch': 1.0,
            'inches': 1.0,
            'ft': 1.0 / 12.0,
            'yd': 1.0 / 36.0,
            'mm': 25.4,
            'cm': 2.54,
            'm': 0.0254,
            'pt': 72.0,
        }
        for key, value in conversions.items():
            if s.endswith(key):
                return float(s[:-len(key)].strip()) / value
        return float(s)
def __init__(self, config_filename: Union[str, Path]):
    self.averaged_peak_plots_width: Final[float] = \
        self._unit_to_inches(c.get('averaged peaks plots', 'width', fallback='6.4″'))
    self.averaged_peak_plots_height: Final[float] = \
        self._unit_to_inches(c.get('averaged peaks plots', 'height', fallback='4.8″'))

However, XeLaTeX produces the following documents regardless of the listing package used:

malformed document screenshot

There, at the line 25 and at the end of the line 26, there are extra prime marks that are not in the Python code. At the lines 28 and 30, the prime marks appeared before the numbers.

What is going on?

StSav012
  • 125
  • 1
    Listings generally does not like utf8. Using a unicode checker it seems the " after 6.4 and 4.8 are not an ascii doubleqoute but instead unicode double prime. That probably messes up everything. Listings can handle some utf8 if it can be mapped to latin1, this cannot. – daleif Jul 10 '20 at 08:24
  • You can probably define the as a listings literal and get it to work correctly (untested) – daleif Jul 10 '20 at 08:25
  • Unfortunately, alsoletter={″} causes an error in XeLaTeX. – StSav012 Jul 10 '20 at 08:26
  • That assumes it is an ascii. I wrote wrongly, the option is called literate, see this answer, https://stackoverflow.com/questions/1116266/listings-in-latex-with-utf-8-or-at-least-german-umlauts, you'll have to specify what the latex equivalet for this char is. – daleif Jul 10 '20 at 08:36
  • The problem here is that the unicode double prime char is multy byte and listings generally cannot handle those without some help. – daleif Jul 10 '20 at 08:37
  • You must add the quotes to the processing list: https://tex.stackexchange.com/questions/25391/the-listings-package-and-utf-8/25396#25396. The alternative is the literate option. – Ulrike Fischer Jul 10 '20 at 08:43
  • @daleif, literate={’}{{'}}1 {″}{{"}}1 did nothing. @UlrikeFischer, your comment just blows my mind: > With xelatex + lualatex chars with unicode positions below 256 will work out of the box – StSav012 Jul 10 '20 at 08:56

0 Answers0