我尝试在 listingsutf8 中定义一种自定义语言(该文件的 inputenc 是 utf8),它使用节符号 § 作为注释分隔符。我这样定义语言(我只会粘贴重要部分,而不是关键字和其他内容):
\lstdefinelanguage{Simple}{
morecomment=[l]{§},
morecomment=[n]{§(}{§)}
}
我是这样使用的:
\begin{lstlisting}[language=Simple]
Test § Test
\end{lstlisting}
问题是,在列表中使用部分符号会导致以下错误:
Package inputenc Error: Unicode char �\lst@EC� (U+9E67)(inputenc) not set up for use with LaTeX. Test §
我尝试使用语言定义中的文学选项来屏蔽字符,但注释不再被识别为注释。
\lstdefinelanguage{Simple}{
morecomment=[l]{§},
morecomment=[n]{§(}{§)},
literate={§}{\S}{1}
}
我也尝试使用 将unicode字符9E67定义为节符号\DeclareUnicodeCharacter{9E67}{\S}
,但这也不起作用。
完整示例:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listingsutf8}
\usepackage{color}
\definecolor{orange}{rgb}{0.9,0.4,0}
\definecolor{lorange}{rgb}{1,0.5,0}
\definecolor{dblue}{rgb}{0,0,0.4}
\definecolor{dgreen}{rgb}{0,0.3,0}
\definecolor{dpurple}{rgb}{0.4,0,0.4}
\lstdefinelanguage{Simple}{
morekeywords={void, boolean, bool, int, float, include, from, as, direct, if, while, do, for, loop, switch, else, case, break, continue, return, test, step, in},
sensitive=true,
morecomment=[l]{§},
morecomment=[n]{§(}{§)},
morecomment=[n][\color{lorange}\itshape]{§\{}{§\}},
morecomment=[n]{§[}{§]},
morecomment=[n]{§<}{§>},
morestring=[b]{"},
morestring=[b][numberstyle]{'}
}
\lstdefinestyle{simple}{
language=Simple,
basicstyle=\ttfamily\mdseries,
keywordstyle=\bfseries\color{dpurple},
commentstyle=\itshape\color{orange},
identifierstyle=\color{dgreen},
stringstyle=\color{dblue},
numberstyle=\itshape\color{dblue},
showstringspaces=false,
breaklines=true,
frame=l,
columns=flexible
}
\begin{document}
\begin{lstlisting}[style=Simple]
§{
§ Multi line comment
§}
+ main():
§ Single line comment
return 0
.
\end{lstlisting}
\end{document}
有什么想法可以修复此问题吗?
答案1
包listingsutf8
要求将列表放在单独的文件中,因为这样包才能捕获文件,将其转换为指定的 8 位编码并将结果传递给包listings
。因此,listings
将看到 8 位编码,样式定义必须遵循此编码。由于 TeX 文档采用 UTF-8 编码,因此大多数编辑器在处理不同编码的字符时都会遇到问题。但 TeX 有一种 ASCII 方法,可以使用^^
两位十六进制数来指定字符。§
可以使用 来指定^^a7
:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listingsutf8}
\usepackage{filecontents}
\usepackage{color}
\definecolor{orange}{rgb}{0.9,0.4,0}
\definecolor{lorange}{rgb}{1,0.5,0}
\definecolor{dblue}{rgb}{0,0,0.4}
\definecolor{dgreen}{rgb}{0,0.3,0}
\definecolor{dpurple}{rgb}{0.4,0,0.4}
\lstdefinelanguage{Simple}{
morekeywords={void, boolean, bool, int, float, include, from,
as, direct, if, while, do, for, loop, switch, else, case,
break, continue, return, test, step, in},
sensitive=true,
morecomment=[l]{^^a7},
morecomment=[n]{^^a7(}{^^a7)},
morecomment=[n][\color{lorange}\itshape]{^^a7\{}{^^a7\}},
morecomment=[n][\color{lorange}\itshape]{^^a7\{}{^^a7\}},
morecomment=[n]{^^a7[}{^^a7]},
morecomment=[n]{^^a7<}{^^a7>},
morestring=[b]{"},
morestring=[b][numberstyle]{'}
}
\lstdefinestyle{simple}{
language=Simple,
basicstyle=\ttfamily\mdseries,
keywordstyle=\bfseries\color{dpurple},
commentstyle=\itshape\color{orange},
identifierstyle=\color{dgreen},
stringstyle=\color{dblue},
numberstyle=\itshape\color{dblue},
showstringspaces=false,
breaklines=true,
frame=l,
columns=flexible
}
\begin{document}
\begin{filecontents*}{SimpleTest.txt}
§{
§ Multi line comment
§}
+ main():
§ Single line comment
return 0
.
\end{filecontents*}
\lstinputlisting[
style=Simple,
inputencoding=utf8/latin1,
]{SimpleTest.txt}
\end{document}