3

I want to write some unicode characters to lstlisting environment but they are displayed inline in the output which isn't what I am expecting. How can I make it work like standard characters? Thanks.

\documentclass{article}

\usepackage{fontspec}
\setmonofont{Consolas}

\usepackage{listings}
\lstset{
    basicstyle=\ttfamily
}

\begin{document}
    \begin{lstlisting}
    └
    └
    └
    \end{lstlisting}    
\end{document}
5

I am sorry if you find my answer inappropriate, but after playing around for a while with your code the easiest variant I found is to switch from lstlisting package to minted.

Why minted?

Pros:

  • Works great with pdflatex, xelatex and so on
  • Supports much more languages to highlight, than lstlisting
  • Sometimes performs highlighting look better
  • Has no problems with UTF-8 characters out-of-the-box

Cons:

  • Requires additional software (though it takes 2-5 minutes to install)
  • Needs to edit the compilation command flags (another 30 seconds)

The good news is that additional software mentioned above is Python, which is likely to come with your system if you use OS X or any popular Linux distro, and its package Pygments, which can be installed with one line in the shell.

You can head to minted manual and check section 2.1, the instructions are very short and informative.

Also you need to check your TeX editor settings and change something like

/Library/tex/texbin/xelatex -synctex=1 %.tex

adding -shell-escape flag, so now it looks something like

/Library/tex/texbin/xelatex -shell-escape -synctex=1 %.tex

This is to allow XeLaTeX call stuff outside its "sandbox" (in our case to call Python).

After these steps you are ready to use minted. No setup will be ever needed again.


Now your MWE, adapted for minted:

\documentclass{article}

\usepackage{fontspec}
\setmonofont{Consolas}

\usepackage{minted}

\begin{document}
    \begin{minted}{text}
    └
    └
    └
    \end{minted}    
\end{document}

Notice the option {text} — it is to specify language (in our case plain text).

And the result:

enter image description here


P.S. I have never worked with minted before, but trying to solve you problem I spent over 40 minutes playing with lstlisting and only 2 minutes installing and getting used to minted, so it definitely worth a try.

  • Thanks, minted looks good but it has one big disadvantage for me. Compilation with many code fragments is now much slower than before, using lstlisting. – sssss Mar 25 '16 at 17:18
  • Hey, two weeks passed :-) Have you found any solution? If so, you can post your workaround as your own answer and mark it as accepted to help future generations. Otherwise, you can accept my answer, if you find it useful despite longer compilation time. – Eugene F Apr 6 '16 at 22:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.