Search code examples
latex

Chinese Character in JSON code listing in LaTex returns errors


I use the following LaTex codes to show JSON in the document:

\begin{lstlisting}[language=json,firstnumber=1]
{"src": "123", "dst": "345"}
\end{lstlisting}

It displays fine. However, when I change the values to Chinese characters, it shows 3 repeating errors:

  • Improper alphabetic constant
  • Missing number, treated as zero
  • Missing = inserted for \ifnum

I have no clue what they are talking about.

screenshots of error

Additional Information:

Here are my related document headers:

\documentclass{sigchi}

\usepackage{balance}       % to better equalize the last page
\usepackage{graphics}      % for EPS, load graphicx instead 
\usepackage[T1]{fontenc}   % for umlauts and other diaeresis
\usepackage{txfonts}
\usepackage{mathptmx}
\usepackage[pdflang={en-US},pdftex]{hyperref}
\usepackage{color}
\usepackage{booktabs}
\usepackage{textcomp}
\usepackage{CJKutf8}
\usepackage{microtype}
\usepackage{ccicons}
\usepackage{todonotes}
\usepackage{bera}
\usepackage{listings}
\usepackage{xcolor}
\definecolor{eclipseStrings}{RGB}{42,0.0,255}
\definecolor{eclipseKeywords}{RGB}{127,0,85}
\colorlet{numb}{magenta!60!black}

\lstdefinelanguage{json}{
    basicstyle=\normalfont\ttfamily,
    commentstyle=\color{eclipseStrings}, % style of comment
    stringstyle=\color{eclipseKeywords}, % style of strings
    numbers=left,
    numberstyle=\scriptsize,
    stepnumber=1,
    numbersep=8pt,
    showstringspaces=false,
    breaklines=true,
    frame=lines,
    backgroundcolor=\color{white}, %only if you like
    string=[s]{"}{"},
    comment=[l]{:\ "},
    morecomment=[l]{:"},
    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}
}

\begin{document}
\begin{CJK*}{UTF8}{bsmi}

\begin{lstlisting}[language=json,firstnumber=1]
{"src": "中文字", "dst": "中文字"}
\end{lstlisting}

\underline{Example 1}
English: 中文字

\balance{}

\end{CJK*}
\end{document}

p.s. I'm using Overleaf online LaTex editor.


Solution

  • If you compile with lualatex or xelatex, you could use the following code to choose a font which has the Chinese characters you use. An overview of the available fonts on Overleaf is available at https://www.overleaf.com/learn/latex/Questions/Which_OTF_or_TTF_fonts_are_supported_via_fontspec%3F

    % !TeX TS-program = lualatex
    
    \documentclass{sigchi}
    
    \usepackage{balance}       % to better equalize the last page
    \usepackage{graphics}      % for EPS, load graphicx instead 
    %\usepackage[T1]{fontenc}   % for umlauts and other diaeresis
    %\usepackage{txfonts}
    %\usepackage{mathptmx}
    \usepackage[pdflang={en-US}
    %,pdftex
    ]{hyperref}
    \usepackage{color}
    \usepackage{booktabs}
    \usepackage{textcomp}
    %\usepackage{CJKutf8}
    %\usepackage{microtype}
    \usepackage{ccicons}
    \usepackage{todonotes}
    %\usepackage{bera}
    \usepackage{listings}
    \usepackage{xcolor}
    \definecolor{eclipseStrings}{RGB}{42,0.0,255}
    \definecolor{eclipseKeywords}{RGB}{127,0,85}
    \colorlet{numb}{magenta!60!black}
    
    \usepackage{fontspec}
    \setmonofont{Noto Sans Mono CJK SC}
    \setmainfont{Noto Sans CJK SC}
    
    \lstdefinelanguage{json}{
        basicstyle=\normalfont\ttfamily,
        commentstyle=\color{eclipseStrings}, % style of comment
        stringstyle=\color{eclipseKeywords}, % style of strings
        numbers=left,
        numberstyle=\scriptsize,
        stepnumber=1,
        numbersep=8pt,
        showstringspaces=false,
        breaklines=true,
        frame=lines,
        backgroundcolor=\color{white}, %only if you like
        string=[s]{"}{"},
        %comment=[l]{:\ "},
        %morecomment=[l]{:"},
        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}
    }
    
    \begin{document}
    
    \begin{lstlisting}[language=json,firstnumber=1]
    {"src": "中文字", "dst": "中文字"}
    \end{lstlisting}
    
    \underline{Example 1}
    English: 中文字
    
    \balance
    
    
    \end{document}
    

    MWE on overleaf: https://www.overleaf.com/read/jzndwywmyngg

    enter image description here