TransWikia.com

listings - ! Package inputenc Error: Invalid UTF-8 byte

TeX - LaTeX Asked on April 10, 2021

I have to use unicode char in listings package, but error reported in below example:

! Package inputenc Error: Invalid UTF-8 byte "94.

How to resolve this error:

documentclass{standalone}
usepackage{tikz}
usetikzlibrary{positioning}
usepackage{listings}
usepackage[T1]{fontenc}
usepackage[utf8]{inputenc}
usetikzlibrary{fit,calc}
newcounter{T}
newcommandmakenode[2]{%
    tikz[baseline=0pt, remember picture] { 
        node[anchor=base,#1/.try] (m-thevalue{T}) {#2};}%
    %node[fill=red!10,label=thevalue{T},anchor=base,#1/.try] (m-thevalue{T}) {#2}; }%
    stepcounter{T}%
}
lstdefinelanguage{myLang}{
    alsoletter=0123456789+=*/<(){};,
}
lstset{
    identifierstyle=makenode{identifier},
    inputencoding = utf8,  % Input encoding
    extendedchars = true,  % Extended ASCII
}
lstset{label= ,caption= ,captionpos=b,numbers=left}
begin{document}
    begin{lstlisting}[language=myLang, numbers=none, escapechar=!,
    basicstyle=ttfamilybfseries, linewidth=0.82linewidth] 
    
    └── rootdir
    ├── product
    │   ├── app
    │   ├── etc
    │   └── priv-app
    ├── system
    │   ├── app
    │   ├── etc
    │   └── priv-app
    └── vendor
    ├── app
    ├── etc
    └── priv-app
    
end{lstlisting}
begin{tikzpicture}[remember picture, overlay,
    every edge/.append style = {->, thick, >=latex,
        line width = 1pt },
    box/.style = {align=center, minimum height = 0pt,inner sep=0,
        font = bfseries},
    text width = 2.5cm ]
    node[box,fit=(m-4)(m-6), draw] {};
    foreach x/y in {18/6,21/9,25/13,18/29,21/31,25/34} {
        draw[-latex] (m-x) ..controls ([xshift=2cm]m-x) (m-y);
    }
end{tikzpicture} 
end{document}

One Answer

AFAIK, package listings does not support multi-byte encodings with 8-bit TeX engines. Workarounds:

  • As workaround package listingsutf8 can be used, if the UTF-8 characters can be converted to the slots of a 8-bit encoding, supported by LaTeX. The environment lstlistings is not supported, the contents goes into a separate file that is read by lstinputlisting with the correct encoding options. Then package listingsutf8 automatically converts the file for package listings.

    The box drawing characters complicate the matters because the code page 437 has them, but cp437.def misses them.

  • But maybe there is an easier way. Package pmboxdraw provides these characters, if you do not have a font that provides them with a .dfu file for LaTeX's UTF-8 support. If you do not need the features of package listings for this "ASCII art", then some of the verbatim environments will work (plain verbatim, package verbatim, package alltt with better escaping support, ...).

Example:

documentclass{article}% standalone does not work in this case
usepackage[utf8]{inputenc}
usepackage[T1]{fontenc}
usepackage[variablett]{lmodern}% with proportional type writer font
usepackage{pmboxdraw}
usepackage{alltt}
begin{document}
begin{alltt}
└── rootdir
├── product
│   ├── app
│   ├── etc
│   └── priv-app
├── system
│   ├── app
│   ├── etc
│   └── priv-app
└── vendor
├── app
├── etc
└── priv-app
end{alltt}
end{document}

enter image description here

Correct answer by Heiko Oberdiek on April 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP