0

The following code works fine with xelatex. It gives an invalid character error ^^00 with pdflatex. If I comment the line out it works fine. Probably an input encoding problem but can not find the answer.

% !TeX encoding = UTF-8
% !TeX program  = pdflatex
\documentclass{minimal}
\usepackage{iftex}
\ifxetex
    \usepackage{fontspec}
    \setmonofont{Fira Mono}
 \else
    \usepackage[utf8]{inputenc}
    \usepackage[T1]{fontenc}
\fi

\usepackage{listings}
\lstset{inputencoding=utf8, 
        extendedchars=true,
        basicstyle=\ttfamily}

\ifxetex
    \begingroup% ......... by Heiko Oberdiek
       \catcode0=12 %
       \makeatletter         
        \g@addto@macro\lst@DefEC{%
            \lst@CCECUse\lst@ProcessLetter
            μΔΩθφ% ... add Unicode characters
            ^^00%  ... end marker [This line cause error with pdflatex]
            }%
    \endgroup
\else
    \usepackage{textgreek}
    \lstset{literate= 
        {μ}{{\textmu}}1
        {Δ}{{\textDelta}}1
        {Ω}{{\textOmega}}1
        {θ}{{\texttheta}}1
        {φ}{{\textphi}}1
        {°}{{$^{\circ}$}}1}
\fi

\begin{document}
\begin{lstlisting}
  label = 'Δφ=6°'
\end{lstlisting}
\end{document}

1 Answer 1

1

I can see that you have combined the two patches to listings package in the following

to make it work with both XeLaTeX and LuaLaTeX.


The reason for the error message is "obvious" (something with catcode 0 = 15 being illegal and such characters cannot appear in skipped-through text), but requires knowledge of how the TeX engine works. (resources: e.g. Where do I start LaTeX programming?)

Anyway, below I give a reasonably robust workaround:

%! TEX program = pdflatex

\documentclass{minimal}
\usepackage{iftex}
\ifxetex
    \usepackage{fontspec}
    %\setmonofont{Fira Mono}  % my machine doesn't have this font either
\else
    \usepackage[utf8]{inputenc}
    \usepackage[T1]{fontenc}
\fi

\usepackage{listings}
\lstset{inputencoding=utf8, 
        extendedchars=true,
        basicstyle=\ttfamily}


\ifxetex
    \begingroup
       \catcode0=12 %
       \catcode`!=7 %
       \makeatletter
        \g@addto@macro\lst@DefEC{%
            \lst@CCECUse\lst@ProcessLetter
            μΔΩθφ% ... add Unicode characters
            !!00%
            }%
    \endgroup
\else
    %\usepackage{textgreek}  % unfortunately on my machine this package doesn't exist so I use a rudimentary substitute below
    %\lstset{literate= 
    %   {μ}{{\textmu}}1
    %   {Δ}{{\textDelta}}1
    %   {Ω}{{\textOmega}}1
    %   {θ}{{\texttheta}}1
    %   {φ}{{\textphi}}1
    %   {°}{{$^{\circ}$}}1}
    % note that \lstset must be outside the group for it to be effective!
    \lstset{literate= 
        {μ}{{$\mu$}}1
        {Δ}{{$\Delta$}}1
        {Ω}{{$\Omega$}}1
        {θ}{{$\theta$}}1
        {φ}{{$\phi$}}1
        {°}{{$^{\circ}$}}1}
\fi



\begin{document}
\begin{lstlisting}
  label = 'Δφ=6°'
\end{lstlisting}
\end{document}
2
  • It's recommended to link to the source, so there may be more context of what the code does instead of just giving the code.
    – user202729
    Jan 28 at 9:38
  • Thank you. The \catcode!=7 %` does the trick.
    – Danie Els
    Jan 28 at 9:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .