在LaTeX中的JSON代码清单中的中文字符会返回错误。

huangapple go评论68阅读模式
英文:

Chinese Character in JSON code listing in LaTex returns errors

问题

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

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.

在LaTeX中的JSON代码清单中的中文字符会返回错误。

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=
展开收缩
{"}{"}, 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.

答案1

得分: 1

% !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=
展开收缩
{"}{"},
%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}
英文:

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=
展开收缩
{"}{"}, %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

在LaTeX中的JSON代码清单中的中文字符会返回错误。

huangapple
  • 本文由 发表于 2023年5月22日 17:39:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76304843.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定