0

I am trying to use unicode box drawing characters inside a tcblisting. First I used the pmboxdraw library to define what those characters should be mapped to, so that they could be drawn. I was able to use the characters outside of the listing environment, but not inside it. The error that appears is Invalid UTF-8 byte sequence.

Here is a minimal example of what I mean:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{pmboxdraw}
\usepackage{newunicodechar}
\newunicodechar{└}{\textSFii}
\newunicodechar{├}{\textSFviii}
\newunicodechar{─}{\textSFx}
\usepackage{tcolorbox}
%\tcbuselibrary{listingsutf8,breakable,skins}
\tcbuselibrary{listings,breakable,skins}
\definecolor{codebgcolor}{cmyk}{0,0.05,0.05,0}
\makeatletter
\newtcblisting{coloredlisting}{
    breakable,enhanced,
    listing inputencoding=utf8,
    listing only,
    frame hidden,
    colback=codebgcolor,
    % == indented nested Verbatim ==
    grow to left by=0pt,
    left=0pt,
    grow to right by=0pt,
    right=0pt,
    arc=0pt
}
\makeatother
\begin{document}
    \begin{verbatim}
    ├── a
    └── b
    \end{verbatim}
    
    \begin{coloredlisting}
    ├── a
    └── b
    \end{coloredlisting}
    
\end{document}

And here is what I got:

resulting rendering

What I tried:

I looked inside the manual for tcolorbox at https://osl.ugr.es/CTAN/macros/latex/contrib/tcolorbox/tcolorbox.pdf and found something. First I tried to use listingsutf8 like this:

\tcbuselibrary{listingsutf8,breakable,skins}

This made the errors disappear, but the characters were not drawn.

Then I looked for instances of the string "utf" inside the manual, and found this (at page 319, section 16.4 Option Keys of the listingsutf8 Library):

If you want to use the listings package and «real» UTF-8 source code, then do not use listingsutf8 but listings with /tcb/listing inputencoding=utf8 and with specific manual hacks for specific UTF-8-encoded characters.

I tried that, and the error Invalid UTF-8 byte sequence came back. Now, there is that hint in the manual about specific manual hacks for specific UTF-8-encoded characters... what do they mean with that. I think that these manual hacks is what I need, but don't know how continue from there.

1 Answer 1

1

You can setup the chars with the literate key of listings:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{pmboxdraw}
\usepackage{newunicodechar}
\newunicodechar{└}{\textSFii}
\newunicodechar{├}{\textSFviii}
\newunicodechar{─}{\textSFx}
\usepackage{tcolorbox}
%\tcbuselibrary{listingsutf8,breakable,skins}
\tcbuselibrary{listings,breakable,skins}
\lstset{%
  literate={└}{\textSFii}1
           {├}{\textSFviii}1
           {─}{\textSFx}2
       }           
\definecolor{codebgcolor}{cmyk}{0,0.05,0.05,0}
\makeatletter
\newtcblisting{coloredlisting}{
    breakable,enhanced,
    listing inputencoding=utf8,
    listing only,
    frame hidden,
    colback=codebgcolor,
    % == indented nested Verbatim ==
    grow to left by=0pt,
    left=0pt,
    grow to right by=0pt,
    right=0pt,
    arc=0pt
}
\makeatother
\begin{document}
    \begin{verbatim}
    ├── a
    └── b
    \end{verbatim}

    \begin{coloredlisting}
    ├── a
    └── b
    \end{coloredlisting}

\end{document}

enter image description here

3
  • Oh my... thanks! How on Earth is someone supposed to learn that kind of thing? It isn't even mentioned in the manual. I see that asking here is a way to learn... would you mind if I asked if there is anything that I could be missing? Or is it experience? Commented Jun 30, 2020 at 15:39
  • 1
    Well literate is documented in the manual (of listings), but as far as I remember I learned that one can use it like this because someone told me once, so I don't think it is a problem if you learn it in the same way. See also tex.stackexchange.com/a/541136/2388 Commented Jun 30, 2020 at 16:04
  • Hum, that answer was deep =) Thanks! I think that one reason that makes me fell not very comfortable in asking for help is that I can find out answers for programming questions, be it Python, C# or Javascript. I can even help people with their own problems, and sometimes try to invent something new. That didn't translate very well for LaTeX... but you are right, if people can learn from me, why not the other way around. Thanks for the double lesson! Commented Jun 30, 2020 at 16:31

You must log in to answer this question.

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