[Tex/LaTex] How to make listings display characters special characters

font-encodingslistingslistingsutf8unicode

I am trying to show a directory structure of a tree using the listings package like so:

├── controllers
│   ├── application_controller.rb

what I get looks like this:

    controllers
            application_controller.rb

when I use

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

the error given is:

Package inputenc Error: Unicode char \u8:\lst@FillFixed@\lst@EC not set up for use with LaTeX. [├── ]

trying with utf8x gives:

./directory_listing.txt:1: Package utf8x Error: MalformedUTF-8sequence. [├── ]

and

[U+FFFD][U+FFFD][U+FFFD]
1
controllers
[U+FFFD][U+FFFD][U+FFFD]
2
[U+FFFD][U+FFFD][U+FFFD]
a p p l i c a t i o n _ c o n t r o l l e r . rb

and

âŤIJâŤĂâŤĂ controllers
âŤĆÂăÂă âŤIJâŤĂâŤĂ application_controller.rb

If I do not include the utf8 line.

Is there a way to get it to correctly encode the emdashes, pipes and three way pipes so that they show in the output? (P.s. I need some form of [T1]fontenc or underscores are not displayed correctly elsewhere.)

Best Answer

There is a work-around, using the pmboxdraw package and listings' escapechar command, that allows to insert Latex code in listings environments:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{pmboxdraw}
\usepackage{listings}
\begin{document}
Here is my code (note that I can insert \LaTeX code between ``@''):

\begin{lstlisting}[escapechar=@]
for i in @\LaTeX@:
    print @$\sum_i$@
\end{lstlisting}

Now, let's print the characters you are interrested in:

\begin{lstlisting}[escapechar=@]
@\pmboxdrawuni{251D}\pmboxdrawuni{2501}@ controllers
@\textSFxi@  @\pmboxdrawuni{251D}\pmboxdrawuni{2501}\pmboxdrawuni{2501}@ application_controller.rb
\end{lstlisting}

\end{document}

enter image description here

There might be Lualatex or XeLaTex solutions to use unicode characters: would that be an option for you?