2

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}
\newcommand\makenode[2]{%
    \tikz[baseline=0pt, remember picture] { 
        \node[anchor=base,#1/.try] (m-\the\value{T}) {#2};}%
    %\node[fill=red!10,label=\the\value{T},anchor=base,#1/.try] (m-\the\value{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=\ttfamily\bfseries, linewidth=0.82\linewidth] 
    
    └── 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}
4

2 Answers 2

2

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

0

So, i had the same problem. An option was, to use \begin{verbatim}, but i realy liked to customize my listing e.g. with a caption. So what i did, was to add \usepackage{pmboxdraw} and in the listing literate= .... Example:

\documentclass{article}% standalone does not work in this case
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{caption}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{pmboxdraw}
\begin{document}
\begin{lstlisting}[language=myLang, caption=myCaption, label=lst:myLabel, numbers=none, escapechar=!, basicstyle=\ttfamily\bfseries, linewidth=0.82\linewidth,literate = {├}{{\textSFviii}}1 {─}{{\textSFx}}1 {└}{{\textSFii}}1 {│}{{\textSFxi}}1] 
        └── rootdir
        ├── product
        │   ├── app
        │   ├── etc
        │   └── priv-app
        ├── system
        │   ├── app
        │   ├── etc
        │   └── priv-app
        └── vendor
        ├── app
        ├── etc
        └── priv-app
\end{lstlisting}

enter image description here

All symbols can be found here: https://ctan.mirror.norbert-ruehl.de/macros/latex/contrib/pmboxdraw/pmboxdraw.pdf

You must log in to answer this question.

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