我正在 overleaf 上写一份关于我正在开发的编程语言的报告,我希望能够使用列表在报告中突出显示该语言的语法,但是由于该编程语言在丹麦语中有关键字,所以有些词是特殊字符,我如何使特殊字符 æ、ø 和 å 作为关键字的一部分突出显示?
下面是我的文件摘录,并附有文本示例。它应该包括我的序言中与此问题相关的所有内容。
\documentclass[12pt,oneside,a4paper,openright]{report}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[sc]{mathpazo}
\usepackage[utf8]{inputenc}
\usepackage[danish, english]{babel}
\usepackage{xcolor}
\usepackage{graphicx}
% Colors for Code
\definecolor{grayC}{RGB}{250,250,250}
\definecolor{Dgray}{RGB}{200,200,200}
\definecolor{bluekeywords}{rgb}{0,0,1}
\definecolor{greencomments}{rgb}{0,0.5,0}
\definecolor{redstrings}{rgb}{0.64,0.08,0.08}
\definecolor{xmlcomments}{rgb}{0.5,0.5,0.5}
\definecolor{types}{rgb}{0.17,0.57,0.68}
\usepackage{listings}
\lstset{
literate= {Ø}{{\O}}1 {Æ}{{\AE}}1 {Å}{{\AA}}1
}
\lstdefinelanguage{BABs} {
alsoletter={\O,\AE,\AA},
morekeywords={
HVIS,
GØR,
FÆRDIG,
ER,
IKKE,
OG,
ELLER,
GENTAG,
OPDATER,
TABEL,
RÆKKE,
VIS,
INDLÆS,
SANDHEDSVÆRDI,
TEKST,
DECIMALTAL,
HELTAL,
SAND,
FALSK
},
sensitive = false, % No case sensitivity of keywords
morecomment = [s]{\#}{\#}, % Comment with start and end
morestring = [b]" % Strings are enclosed with "
}
% Some additional settings for BABs
\lstset{language=BABs,
backgroundcolor=\color{grayC},
basicstyle=\ttfamily\small,
breakatwhitespace=false,
breaklines=true,
commentstyle=\color{greencomments},
deletekeywords={...},
escapeinside={(*@}{@*)},
extendedchars=true,
frame=single,
keepspaces=true,
keywordstyle=\color{bluekeywords},
numbers=left,
numbersep=5pt,
numberstyle=\tiny\color{black},
rulecolor=\color{Dgray},
showspaces=false,
showstringspaces=false,
showtabs=false,
stepnumber=1,
stringstyle=\color{redstrings},
tabsize=2
}
\begin{document}
This is a test to show that \lstinline[language=BABs]|GØR{<Stmts>}| and \lstinline[language=BABs]{FÆRDIG} are not properly highlighted, however \lstinline[language=BABs]{HVIS(<BoolExpr>)} is.
\end{document}
输出:
可以看出,虽然使用FÆRDIG
和GØR
指定了它们,但它们并未突出显示为关键字,morekeywords
我已使用 将它们添加为字母alsoletter
。上面的摘录是通过查看类似问题的解决方案而得出的,其中特殊字符是 - 或 .。
有人知道在我的情况下如何使用包含特殊字符的丹麦关键字来使其发挥作用吗?
答案1
我认为不可能将 literate 选项与关键字检测结合起来 - literate 不会让输入处于“简单”状态。如果你真的想要自动关键字检测,我认为你必须切换到其中一个 unicode 引擎 (xelatex/lualatex):
\documentclass[12pt,oneside,a4paper,openright]{report}
\usepackage{fontspec}
\setmainfont{TeX Gyre Pagella}
\usepackage[danish, english]{babel}
\usepackage{xcolor}
\usepackage{graphicx}
% Colors for Code
\definecolor{bluekeywords}{rgb}{0,0,1}
\usepackage{listings}
\lstdefinelanguage{BABs} {
morekeywords={
HVIS,
GØR,
FÆRDIG,
},
sensitive = false, % No case sensitivity of keywords
morecomment = [s]{\#}{\#}, % Comment with start and end
morestring = [b]" % Strings are enclosed with "
}
% Some additional settings for BABs
\lstset{language=BABs,
basicstyle=\ttfamily\small,
keywordstyle=\color{bluekeywords},
}
\begin{document}
This is a test to show that \lstinline|GØR{<Stmts>}|
and \lstinline[language=BABs]{FÆRDIG} are not properly highlighted,
however \lstinline[language=BABs]{HVIS(<BoolExpr>)} is.
\end{document}