0

I have an external JSON-File that contains the character Δ. A document can be built but I get the error: LaTeX Error: Invalid UTF-8 byte sequence (�\lst@FillFixed@). As it's external I am using:

 \lstinputlisting[language=json,firstnumber=1]{content/future/json/Vertices.json}

It is shown and formatted. However the Deltas are not there. Just shown as empty brackets.

As suggested here I tried adding \UseRawInputEncoding - the brackets are not empty any more, now they are filled with: "ΡT"

Here it's recommended to add \usepackage[utf8]{inputenc} \usepackage{listingsutf8} Brackets are empty again. Using \usepackage[greek]{babel} doesn't do anything either.

There are some posts dealing specifically with the listing like this. Of course I replaced the entire line of Umlaute with: literate={Δ}{{\"Delta}}. However, this just results in no document being built and the error: LaTeX Error: Invalid UTF-8 byte sequence (�\lst@FillFixed@). This here (doing the same, just also replacing portuguese with greek) modified the font a bit but still, the character can't be displayed.

I managed to display a Δ inside regular text using (not math mode):

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\DeclareUnicodeCharacter{0394}{\ensuremath{\Delta}}

However, this doesn't affect the box of code.

The full code:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{bera}% optional: just to have a nice mono-spaced font
\usepackage{listings}
\usepackage{xcolor}

\colorlet{punct}{red!60!black}
\definecolor{background}{HTML}{EEEEEE}
\definecolor{delim}{RGB}{20,105,176}
\colorlet{numb}{magenta!60!black}

\lstdefinelanguage{json}{
    basicstyle=\normalfont\ttfamily,
    numbers=left,
    numberstyle=\scriptsize,
    stepnumber=1,
    numbersep=8pt,
    showstringspaces=false,
    breaklines=true,
    frame=lines,
    backgroundcolor=\color{background},
    literate=
     *{0}{{{\color{numb}0}}}{1}
      {1}{{{\color{numb}1}}}{1}
      {2}{{{\color{numb}2}}}{1}
      {3}{{{\color{numb}3}}}{1}
      {4}{{{\color{numb}4}}}{1}
      {5}{{{\color{numb}5}}}{1}
      {6}{{{\color{numb}6}}}{1}
      {7}{{{\color{numb}7}}}{1}
      {8}{{{\color{numb}8}}}{1}
      {9}{{{\color{numb}9}}}{1}
      {:}{{{\color{punct}{:}}}}{1}
      {,}{{{\color{punct}{,}}}}{1}
      {\{}{{{\color{delim}{\{}}}}{1}
      {\}}{{{\color{delim}{\}}}}}{1}
      {[}{{{\color{delim}{[}}}}{1}
      {]}{{{\color{delim}{]}}}}{1},
}
\begin{document}
 \lstinputlisting[language=json,firstnumber=1]{Vertices.json}
\end{document}

The JSON in Vertices.json (Which is an external file):

{
  "_key":"9374773",
  "_id":"V1/9374773",
  "_rev":"_fEczBK----",
  "Description":"2018 maximum braking distance",
  "xpl_isGroupNode":"Δ",
  "xpl_isGeneric":"Δ"
}

And here to view: https://www.overleaf.com/read/fkkcsyfppppj

19
  • 4
    don't show lots of snippets. Combine your problem into a small but complete example. That makes it much easier to test. (probably you only need to add your char with a suitable definition to the literate list). Commented Jan 19, 2023 at 11:14
  • 3
    sure, if you provide a small but complete example for a test. I don't have the time to assemble your snippets. Commented Jan 19, 2023 at 11:20
  • 3
    no I won't start to debug some external overleaf projects, provide the example here. Commented Jan 19, 2023 at 11:30
  • 3
    sorry but it is not about what you prefer generally in your documents. You can do there what you want. But if you want help from us, you should put the code in the question in a format that makes it for us easy and comfortable to help you. Commented Jan 19, 2023 at 15:05
  • 3
    literate={Δ}{{\ensuremath{\Delta}}}{1}, appears to work for me. \Delta alone is not enough, because we need to go into math mode. {\ensuremath{\Delta}} does not appear to work, because the listings documentation says "Each ‘printing unit’ in <replacement text> must be in braces unless it’s a single character." So we end up with {\ensuremath{\Delta}} as the contents of the replacement text braces.
    – moewe
    Commented Jan 19, 2023 at 15:27

1 Answer 1

4

You'll need

literate={Δ}{{\ensuremath{\Delta}}}{1},

The literate is needed because listings can't natively deal with non-ASCII characters and needs help (UTF8 for listings).

The form of the second argument is explained as follows

  • \Delta is not enough, because it needs to be in math mode
  • \ensuremath{\Delta} does not work because according to the listings documentation (p. 52)

    Each ‘printing unit’ in <replacement text> must be in braces unless it’s a single character.

  • so {\ensuremath{\Delta}} it is.

You must log in to answer this question.

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