[Tex/LaTex] Getting Swedish letters to work in LaTex

babelinput-encodingslanguageslistingslistingsutf8

Problem:

  • Issue 1 (solved): The Swedish letters ÅÄÖ/åäö do not print out correctly when used with listings package.
  • Issue 2: How can it be solved so that the Swedish letters also get orange color like the rest of the letters?
  • Issue 3: How can it be solved to get the PHP-line (line 10) to be on the same line as line 9?

Minimal Working Example (MWE):

% ----------------------------------------------------------------------
%  Latex template for coding in PHP
% ----------------------------------------------------------------------

\documentclass{scrreprt}
\usepackage{xcolor}
\usepackage{upquote}
\usepackage{listings}
\usepackage[utf8]{inputenc}
\usepackage[most]{tcolorbox}
\usepackage{calc}
\usepackage{xparse}
\usepackage{ragged2e}

% ----------------------------------------------------------------------
%  Definition of colors for code highlight
% ----------------------------------------------------------------------

\definecolor{editorLightGray}{cmyk}{0.05, 0.05, 0.05, 0.1}
\definecolor{editorGray}{cmyk}{0.6, 0.55, 0.55, 0.2}
\definecolor{editorPurple}{cmyk}{0.4,0.9,0,0}
\definecolor{editorWhite}{cmyk}{0, 0, 0, 0}
\definecolor{editorBlack}{cmyk}{1, 1, 1, 1}
\definecolor{editorOrange}{cmyk}{0, 0.65, 1, 0}
\definecolor{editorBlue}{cmyk}{1, 0.5, 0, 0}
\definecolor{editorPink}{cmyk}{0, 1, 0, 0}

% ----------------------------------------------------------------------
%  Defining parameters/keywords for PHP
% ----------------------------------------------------------------------

\lstdefinelanguage{HTML5}{
  language=html,
  sensitive=true,
  tagstyle=\color{editorBlue},
  markfirstintag=true,
  morecomment=[s]{<!-}{-->},
  alsoletter={!},
  keywords={},
  %alsoletter={<>=-+},
  %tag=[s],
}

\lstdefinelanguage{PHP}{
        morestring=[s]{'}{'},
        morestring=[b]",
        morecomment=[l]{//},
        sensitive=true,
        alsoletter={::},
        keywords=[1]{require_once, try, new, catch, die, echo, sprintf},
        keywords=[2]{setAttribute, getMessage, PDO, PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION},
        keywords=[3]{PDOException, php},
        keywords=[4]{athletics}
}

% ----------------------------------------------------------------------
%  Design of listings for PHP
% ----------------------------------------------------------------------

\lstset{%
    % Basic design
    backgroundcolor=\color{editorWhite},
    basicstyle={\footnotesize\ttfamily},   
    frame=l,
    % Line numbers
    xleftmargin={20pt},
    numbers=left,
    stepnumber=1,
    firstnumber=1,
    numberfirstline=true,
    % Code design
    identifierstyle=\color{editorOrange},
    keywordstyle=[1]\color{editorPink}\bfseries,
    keywordstyle=[2]\color{editorBlue}\bfseries,
    keywordstyle=[3]\color{editorBlack}\bfseries,
    keywordstyle=[4]\color{editorBlue}\bfseries,
    commentstyle=\color{editorGray}\ttfamily,
    stringstyle=\color{editorPurple},
    % Code
    language=php,
    alsodigit={.:},
    tabsize=2,
    showtabs=false,
    showspaces=false,
    showstringspaces=false,
    extendedchars=true,
    breaklines=false,
    % Support for German umlauts
    literate=%
    {Ö}{{\"O}}1
    {Ä}{{\"A}}1
    {Å}{{\AA{}}}1
    {Ü}{{\"U}}1
    {ß}{{\ss}}1
    {ü}{{\"u}}1
    {ö}{{\"o}}1
    {ä}{{\"a}}1
    {å}{{\aa{}}}1
}

% ----------------------------------------------------------------------
%  Formatted code in document
% ----------------------------------------------------------------------

\begin{document}
\begin{lstlisting}[language=HTML5,numbers=left,firstnumber=1]
<!DOCTYPE html>
<html>
  <head>
    <title>Utskrift av datum</title>
    <meta charset="UTF-8">
  </head>
  <body>
    <!-- Skriver ut årtal, månad och dag enligt YYYY-MM-DD -->
    <p>Dagens datum är
\end{lstlisting}
\vspace*{-12pt}
\begin{lstlisting}[language=PHP,numbers=left,firstnumber=last]
<?php echo date('Y-m-d'); ?>
\end{lstlisting}
\vspace*{-12pt}
\begin{lstlisting}[language=HTML5,numbers=left,firstnumber=last]
  </p>
  </body>
</html>
\end{lstlisting}
\vspace*{-12pt}
\end{document}

The errors indicate issues with the Swedish letters. I used literate for the listings package to solve this issue but without success.

Current outcome:

enter image description here

Desired outcome:

The letter ä to be the same color as the rest (i.e. orange) and the PHP-code to be on the same line as the <p> element.

Best Answer

Using a Unicode-aware engine

If changing engine to either XeTeX or LuaTeX is an option, then you can remove the literate stuff from the \lstset, remove inputenc and add fontspec. With those modifications it just works.

Note that in the code below I set belowskip and aboveskip to 0pt where necessary to remove the gap between the different listings, rather than using \vspace{-12pt}.

enter image description here

% !TeX program = xelatex

\documentclass{scrreprt}
\usepackage{fontspec}
\usepackage{xcolor}
\usepackage{upquote}
\usepackage{listings}
\usepackage[most]{tcolorbox}
\usepackage{calc}
\usepackage{xparse}
\usepackage{ragged2e}


% ----------------------------------------------------------------------
%  Definition of colors for code highlight
% ----------------------------------------------------------------------

\definecolor{editorLightGray}{cmyk}{0.05, 0.05, 0.05, 0.1}
\definecolor{editorGray}{cmyk}{0.6, 0.55, 0.55, 0.2}
\definecolor{editorPurple}{cmyk}{0.4,0.9,0,0}
\definecolor{editorWhite}{cmyk}{0, 0, 0, 0}
\definecolor{editorBlack}{cmyk}{1, 1, 1, 1}
\definecolor{editorOrange}{cmyk}{0, 0.65, 1, 0}
\definecolor{editorBlue}{cmyk}{1, 0.5, 0, 0}
\definecolor{editorPink}{cmyk}{0, 1, 0, 0}

% ----------------------------------------------------------------------
%  Defining parameters/keywords for PHP
% ----------------------------------------------------------------------

\lstdefinelanguage{HTML5}{
  language=html,
  sensitive=true,
  tagstyle=\color{editorBlue},
  markfirstintag=true,
  morecomment=[s]{<!-}{-->},
  alsoletter={!},
  keywords={},
  %alsoletter={<>=-+},
  %tag=[s],
}

\lstdefinelanguage{PHP}{
        morestring=[s]{'}{'},
        morestring=[b]",
        morecomment=[l]{//},
        sensitive=true,
        alsoletter={::},
        keywords=[1]{require_once, try, new, catch, die, echo, sprintf},
        keywords=[2]{setAttribute, getMessage, PDO, PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION},
        keywords=[3]{PDOException, php},
        keywords=[4]{athletics}
}

% ----------------------------------------------------------------------
%  Design of listings for PHP
% ----------------------------------------------------------------------

\lstset{%
    % Basic design
    backgroundcolor=\color{editorWhite},
    basicstyle={\footnotesize\ttfamily},   
    frame=l,
    % Line numbers
    xleftmargin={20pt},
    numbers=left,
    stepnumber=1,
    firstnumber=1,
    numberfirstline=true,
    % Code design
    identifierstyle=\color{editorOrange},
    keywordstyle=[1]\color{editorPink}\bfseries,
    keywordstyle=[2]\color{editorBlue}\bfseries,
    keywordstyle=[3]\color{editorBlack}\bfseries,
    keywordstyle=[4]\color{editorBlue}\bfseries,
    commentstyle=\color{editorGray}\ttfamily,
    stringstyle=\color{editorPurple},
    % Code
    language=php,
    alsodigit={.:},
    tabsize=2,
    showtabs=false,
    showspaces=false,
    showstringspaces=false,
    extendedchars=true,
    breaklines=false,
}

% ----------------------------------------------------------------------
%  Formatted code in document
% ----------------------------------------------------------------------

\begin{document}
\begin{lstlisting}[language=HTML5,numbers=left,firstnumber=1,belowskip=0pt]
<!DOCTYPE html>
<html>
  <head>
    <title>Utskrift av datum</title>
    <meta charset="UTF-8">
  </head>
  <body>
    <!-- Skriver ut årtal, månad och dag enligt YYYY-MM-DD -->
    <p>Dagens datum är
\end{lstlisting}
\begin{lstlisting}[language=PHP,numbers=left,firstnumber=last,belowskip=0pt,aboveskip=0pt]
<?php echo date('Y-m-d'); ?>
\end{lstlisting}%
\begin{lstlisting}[language=HTML5,numbers=left,firstnumber=last,aboveskip=0pt]
  </p>
  </body>
</html>
\end{lstlisting}
\end{document}

Using pdflatex

While the following works with pdflatex it is limited. This is taken more or less directly from section 9 of the listings manual. The literate stuff is again removed, and two lines are added to \lstset:

escapeinside=`',
escapebegin={\color{editorGray}}

The comments with the Scandinavian characters are typed between ` and '. escapeinside means that between the two characters specified, listings jumps back to 'normal' LaTeX mode. The escapebegin line means that the \color specification will be added to the start of each escaped part, and because LaTeX renders this as usual, the text is coloured.

You see where the limitation lies -- as used here all such text blocks will have the same colour. If you only have such characters in comments, then it's no problem. If you have them elsewhere, you will have to add an extra \color{..} at the start of every escaped sequence that should have a different colour, as with \color{editorOrange} in this case. Hence it means a lot of extra fiddling.

(Note that I am by no means an expert when it comes to listings, so there may well be better ways of doing this.)

% !TeX program = pdflatex

\documentclass{scrreprt}
\usepackage{xcolor}
\usepackage{upquote}
\usepackage{listings}
\usepackage[utf8]{inputenc}
\usepackage[most]{tcolorbox}
\usepackage{calc}
\usepackage{xparse}
\usepackage{ragged2e}

% ----------------------------------------------------------------------
%  Definition of colors for code highlight
% ----------------------------------------------------------------------

\definecolor{editorLightGray}{cmyk}{0.05, 0.05, 0.05, 0.1}
\definecolor{editorGray}{cmyk}{0.6, 0.55, 0.55, 0.2}
\definecolor{editorPurple}{cmyk}{0.4,0.9,0,0}
\definecolor{editorWhite}{cmyk}{0, 0, 0, 0}
\definecolor{editorBlack}{cmyk}{1, 1, 1, 1}
\definecolor{editorOrange}{cmyk}{0, 0.65, 1, 0}
\definecolor{editorBlue}{cmyk}{1, 0.5, 0, 0}
\definecolor{editorPink}{cmyk}{0, 1, 0, 0}

% ----------------------------------------------------------------------
%  Defining parameters/keywords for PHP
% ----------------------------------------------------------------------

\lstdefinelanguage{HTML5}{
  language=html,
  sensitive=true,
  tagstyle=\color{editorBlue},
  markfirstintag=true,
  morecomment=[s]{<!-}{-->},
  alsoletter={!},
  keywords={},
  %alsoletter={<>=-+},
  %tag=[s],
}

\lstdefinelanguage{PHP}{
        morestring=[s]{'}{'},
        morestring=[b]",
        morecomment=[l]{//},
        sensitive=true,
        alsoletter={::},
        keywords=[1]{require_once, try, new, catch, die, echo, sprintf},
        keywords=[2]{setAttribute, getMessage, PDO, PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION},
        keywords=[3]{PDOException, php},
        keywords=[4]{athletics}
}

% ----------------------------------------------------------------------
%  Design of listings for PHP
% ----------------------------------------------------------------------

\lstset{%
    % Basic design
    backgroundcolor=\color{editorWhite},
    basicstyle={\footnotesize\ttfamily},   
    frame=l,
    % Line numbers
    xleftmargin={20pt},
    numbers=left,
    stepnumber=1,
    firstnumber=1,
    numberfirstline=true,
    % Code design
    identifierstyle=\color{editorOrange},
    keywordstyle=[1]\color{editorPink}\bfseries,
    keywordstyle=[2]\color{editorBlue}\bfseries,
    keywordstyle=[3]\color{editorBlack}\bfseries,
    keywordstyle=[4]\color{editorBlue}\bfseries,
    commentstyle=\color{editorGray}\ttfamily,
    stringstyle=\color{editorPurple},
    % Code
    language=php,
    alsodigit={.:},
    tabsize=2,
    showtabs=false,
    showspaces=false,
    showstringspaces=false,
    extendedchars=true,
    breaklines=false,
    escapeinside=`',
    escapebegin={\color{editorGray}}
}

% ----------------------------------------------------------------------
%  Formatted code in document
% ----------------------------------------------------------------------

\begin{document}
\begin{lstlisting}[language=HTML5,numbers=left,firstnumber=1,belowskip=0pt]
<!DOCTYPE html>
<html>
  <head>
    <title>Utskrift av datum</title>
    <meta charset="UTF-8">
  </head>
  <body>
    <!-- `Skriver ut årtal, månad och dag enligt YYYY-MM-DD' -->
    <p>`\color{editorOrange}Dagens datum är'
\end{lstlisting}
\begin{lstlisting}[language=PHP,numbers=left,firstnumber=last,belowskip=0pt,aboveskip=0pt]
<?php echo date('Y-m-d'); ?>
\end{lstlisting}%
\begin{lstlisting}[language=HTML5,numbers=left,firstnumber=last,aboveskip=0pt]
  </p>
  </body>
</html>
\end{lstlisting}
\end{document}
Related Question