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:
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
, removeinputenc
and addfontspec
. With those modifications it just works.Note that in the code below I set
belowskip
andaboveskip
to0pt
where necessary to remove the gap between the differentlistings
, rather than using\vspace{-12pt}
.Using
pdflatex
While the following works with
pdflatex
it is limited. This is taken more or less directly from section 9 of thelistings
manual. Theliterate
stuff is again removed, and two lines are added to\lstset
: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. Theescapebegin
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.)