Search code examples
latexpdflatex

Latex Define Language for Listing


I'm having trouble with defining my own language for a Latex Listing:

\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}

Content of the input File:

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

However, the comments and strings do get highlighted, the keywords don't :/

dcl-c not highlighted

Additionally, Latex throws out alot of these errors:

errors

Thansk alot for your help!

EDIT:

Minimal Compilable Version: 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, 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
  ,  
}


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

test.rpgle (utf-8 file):

// 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;

The incompatible color errors disappeared in this minimal version, but 'dcl-pr' still doesn't get highlighted.


Solution

  • The problem is the - in your keywords, but you can tell your new language to treat it like a normal letter:

    \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