1

Im writing a report in overleaf about a programming language im making, I want to be able to use listings to have the syntax of the language highlighted in the report, however since the programming language have keywords in danish, some words are special characters, how do I make the special characters æ, ø and å highlighted, as part of a keyword?

An excerpt of my file is shown below, with an example of text. It should include all from my preamble that are relevant for this question.

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

Output:

Output

As can be seen FÆRDIG and GØR are not highlighted as keywords, eventhough they are specified using morekeywords and I have added them as letters using alsoletter. The exerpt above, have been made by looking at solutions to similar problems, where the special characters are - or . instead.

Does anyone know how to make it work in my case with the danish keywords containing special characters?

3

I don't think that it is possible to combine the literate option with the keyword detection - literate doesn't leave the input in a "simple" state. If you really want automatic keyword detection you will imho have to switch to one of the unicode engines (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}

enter image description here

  • Thanks, it works in the example above, using xelatex instead and removing the literate option. xelatex makes a lot of new compile errors all around my document though, so I think I will have to do without highlighting for now. You don't think it would help using \O or \AE instead of Ø and Æ in the morekeywords option? I just dont know how to do it, because the escape is inside a word so it detects the following characters as being part of the escape. – tralala Apr 19 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.