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

Qohelet
  • 113
  • 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). – Ulrike Fischer Jan 19 '23 at 11:14
  • @UlrikeFischer - would you mind telling me, how? – Qohelet Jan 19 '23 at 11:19
  • 3
    sure, if you provide a small but complete example for a test. I don't have the time to assemble your snippets. – Ulrike Fischer Jan 19 '23 at 11:20
  • @UlrikeFischer: https://www.overleaf.com/read/fkkcsyfppppj – Qohelet Jan 19 '23 at 11:25
  • 3
    no I won't start to debug some external overleaf projects, provide the example here. – Ulrike Fischer Jan 19 '23 at 11:30
  • @UlrikeFischer - I edited the post already. What other information do you require? – Qohelet Jan 19 '23 at 11:34
  • 1
    What is required is that you put, in this post, one code block that starts with \documentclass and ends with \end{document}, and that when compiled returns exactly the error that you are asking about. In this case you of course also need the contents of the JSON file but you already provided that at the end of the question, that code block should stay in as well. – Marijn Jan 19 '23 at 12:03
  • 1
  • @Marijn - added – Qohelet Jan 19 '23 at 13:47
  • @Qohelet thank you but this kind of misses the point. If I run your MWE I get ! LaTeX Error: File 'JsonEnv.tex' not found.. The goal is to provide code, in one code block, that can be compiled without doing anything else. This helps to minimize the effort for people on the site that want to help you. So please edit again, and integrate all the code blocks into two (one for the complete LaTeX code and one for the json data example). – Marijn Jan 19 '23 at 14:42
  • @Marijn - then just remove the input and instead use the big code block that I provided (\usepackage{bera} ......). I prefer to separate any kind of logic from the text. It doesn't matter if it's done by input or just written directly. (https://www.overleaf.com/read/fkkcsyfppppj) – Qohelet Jan 19 '23 at 14:55
  • 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. – Ulrike Fischer Jan 19 '23 at 15:05
  • 1
    I can completely understand that in your real-world document another approach makes more sense. For questions on this site, however, it is much more useful to try and provide a single code-block with one copy-and-pasteable self-contained example document. People who read (and try to answer) several questions a day will lose quite a lot of time that could be spent thinking about answer if they have to fit together separate blocks of code and identify which bit goes where. A self-contained example also increases the chances that everyone really tests the exact same thing. – moewe Jan 19 '23 at 15:16
  • @UlrikeFischer - added it. However I can't put the Vertices.json into the block of code as it's intended to be an external file. I assume this is part of the error. Check the latest Edit – Qohelet Jan 19 '23 at 15:19
  • 1
    You could use a lstlisting environment instead of an external file to make a nice minimal working example. – samcarter_is_at_topanswers.xyz Jan 19 '23 at 15:25
  • You can generate external files inline with \begin{filecontents}{<your file name here>}...\end{filecontents} (see e.g. https://tex.meta.stackexchange.com/q/228/35864), but in this case it appears to me that same effect can be reproduced with \begin{lstlisting}[language=json,firstnumber=1]...\end{lstlisting} instead of \lstinputlisting. – moewe Jan 19 '23 at 15:25
  • Have you considered switching to an unicode aware engine like lualatex to avoid all such problems? – samcarter_is_at_topanswers.xyz Jan 19 '23 at 15:26
  • 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 Jan 19 '23 at 15:27
  • @moewe - this! I just had to add this line in my JsonEnv at the end. Everything works! Thank you! – Qohelet Jan 20 '23 at 08:59

1 Answers1

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.
moewe
  • 175,683