Latex 定义列表语言

问题描述

我在为 Latex 列表定义自己的语言时遇到问题:

\usepackage{color}

\definecolor{sh_comment}{rgb}{0.12,0.38,0.18 } 
\definecolor{sh_keyword}{rgb}{0.37,0.08,0.25}  
\definecolor{sh_string}{rgb}{0.06,0.10,0.98} 

% Define Language RPG
\lstdefinelanguage{RPGLE}
{
  % list of keywords
  morekeywords=[1]{
    extproc,export,dcl-pi,end-pi,dcl-pr,end-pr,dcl-proc,end-proc,dcl-s,dcl-c,dcl-ds
  },sensitive=false,% keywords are not case-sensitive
  morecomment=[l]{//},% l is for line comment
  morecomment=[s]{/*}{*/},% s is for start and end delimiter
  morestring=[b]',% defines that strings are enclosed in double quotes
  commentstyle=\color{sh_comment},% style of comments
  keywordstyle=[1]\color{sh_keyword},% style of keywords
  stringstyle=\color{sh_string},% style of strings
}
% Set Language
\lstset{
  language={RPGLE},basicstyle=\small\ttfamily,% Global Code Style
  captionpos=b,% Position of the Caption (t for top,b for bottom)
  extendedchars=true,% Allows 256 instead of 128 ASCII characters
  tabsize=2,% number of spaces indented when discovering a tab 
  columns=fixed,% make all characters equal width
  keepspaces=true,% does not ignore spaces to fit width,convert tabs to spaces
  showstringspaces=false,% lets spaces in strings appear as real spaces
  breaklines=true,% wrap lines if they don't fit
  frame=trbl,% draw a frame at the top,right,left and bottom of the listing
  frameround=tttt,% make the frame round at all four corners
  framesep=4pt,% quarter circle size of the round corners
  numbers=left,% show line numbers at the left
  numberstyle=\tiny\ttfamily,% style of the line numbers
  inputencoding=latin1,escapeinside={\%*}{*)},literate=% Allow for German characters in lstlistings.
  {Ö}{{\"O}}1
  {Ä}{{\"A}}1
  {Ü}{{\"U}}1
  {ß}{{\ss}}2
  {ü}{{\"u}}1
  {ä}{{\"a}}1
  {ö}{{\"o}}1,}

\lstinputlisting[language=RPGLE]{code-snippets/firstPrototypeOfOneLayer.rpgle}

输入文件内容

// this is some Comment
dcl-c RPDV9999_ERROR_ONE '10';
dcl-c RPDV9999_ERROR_TWO '11';

但是,注释和字符串确实突出显示,关键字没有 :/

dcl-c not highlighted

此外,Latex 会抛出很多这些错误

errors

非常感谢您的帮助!

编辑:

最小可编译版本: test.tex:

\documentclass[%
paper=A4,% alle weiteren Papierformat einstellbar
fontsize=11pt,% Schriftgröße (12pt,11pt (Standard))
BCOR12mm,% Bindekorrektur,bspw. 1 cm
DIV14,% breiter Satzspiegel
parskip=half*,% Absatzformatierung s. scrguide 3.1
headsepline,% Trennline zum Seitenkopf  
%footsepline,% Trennline zum Seitenfuß
%normalheadings,% Überschriften etwas kleiner (smallheadings)
listof=totoc,% Tabellen & Abbildungsverzeichnis ins Inhaltsverzeichnis      
%bibtotoc,% Literaturverzeichnis im Inhalt 
%draft            % Überlangen Zeilen in Ausgabe gekennzeichnet
footinclude=false,% Fußzeile in die Satzspiegelberechnung einbeziehen 
headinclude=true,% Kopfzeile in die Satzspiegelberechnung einbeziehen 
final             % draft beschleunigt die Kompilierung
]
{scrartcl}

%\setuptoc{toc}{totoc} % Inhaltsverzeichnis ins Inhaltsverzeichnis

% Neue Deutsche Rechtschreibung und Deutsche Standardtexte
\usepackage[ngerman]{babel} 

% Umlaute können verwendet werden
\usepackage[utf8]{inputenc}   

% Echte Umlaute
\usepackage[T1]{fontenc} 

% Latin Modern Font,Type1-Schriftart für nicht-englische Texte
\usepackage{lmodern} 
\usepackage{color}
\usepackage{listings}

\definecolor{sh_comment}{rgb}{0.12,}


\begin{document}
\lstinputlisting[language=RPGLE]{test.rpgle}
\end{document}

test.rpgle(utf-8 文件):

// this is some Comment
dcl-c RPDV9999_ERROR_ONE '10';
dcl-c RPDV9999_ERROR_TWO '11';

// more comments
// Some Umlauts äüöÜÖÄß
dcl-pr somePr ind extproc('sth');
    sth char(3) const;
end-pr;

不兼容的颜色错误在这个最小版本中消失了,但“dcl-pr”仍然没有突出显示

解决方法

问题在于关键字中的 -,但您可以告诉您的新语言将其视为普通字母:

\documentclass{scrartcl}

\usepackage{color}
\usepackage{listings}

% Define Language RPG
\lstdefinelanguage{RPGLE}
{
  alsoletter=-,% list of keywords
  morekeywords={
    dcl-c
  },keywordstyle=\color{red},% style of keywords
}
% Set Language
\lstset{
  language={RPGLE},}


\begin{document}

\begin{lstlisting}[language=RPGLE]
// this is some Comment
dcl-c RPDV9999_ERROR_ONE '10';
dcl-c RPDV9999_ERROR_TWO '11';
\end{lstlisting}

\end{document}

enter image description here