[Tex/LaTex] lstinline has Invalid UTF-8 byte 167

listingsunicode

Possible duplicate of this question

MWE:

\documentclass[%
  parskip=full,% empty line after each paragraph.
  twoside=false% not a 2-sided print for a book.
]{scrbook}

% \usepackage[utf8]{inputenc} % For non-English languages
\usepackage[latin1]{inputenc} % listings needs this encoding instead!

\usepackage{listings, tcolorbox}

\usepackage{tcolorbox}
\tcbuselibrary{listings,skins,theorems,xparse}
\NewTotalTCBox{\inlinebox}{ O{white} v}
{ tcbox raise base,
  arc=2pt,
  nobeforeafter,% makes the box inline
  colback=#1!10!white,% pales given color for background
  boxsep=0pt,left=1pt,right=1pt,top=2pt,bottom=2pt,
  boxrule=0pt}
{\lstinline[style=tcblatex,texcsstyle=*\color{blue}\bfseries]§#2§}
\newcommand\cil{\inlinebox[green]}

\begin{document}

\cil|\TeX| (\TeX) is a control word because it is made up of letters. Any non-letter will end the sequence.

\end{document}

This all works if I do \UseRawInputEncoding, as per this answer.

Should I do that? Or is there something more correct that I should do?

\usepackage[latin1]{inputenc} also works, by the way.

Best Answer

The problematic part is:

\lstinline[...]§#2§

The character § is not an ASCII character.

8-bit TeX engines (TeX, pdfTeX)

Since package listings does not support multi-byte encodings, the text encoding is limited to an 8-bit encoding as latin1. This encoding should then be declared by

\usepackage[latin1]{inputenc}

For greater portability, I recommend to use a different fence character from the ASCII range (|, &, ...). This avoids problems, when the input file encoding is changed to UTF-8.

Unicode engines (LuaTeX, XeTeX)

Here, the character § is just one character (big char) and it can be used as fence character for \lstinline of package listings.

However, the best supported input encoding for LuaTeX or XeTeX is UTF-8. Therefore, the input file should be re-encoded from Latin-1 to UTF-8.

Related Question