6

Problem:

When using non-English characters, the listings package breaks in terms of creating a space in the line next to code numbers.

Minimal Working Example (MWE):

\documentclass{scrreprt}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[swedish]{babel}
\usepackage{listings}
\usepackage{xcolor}

\makeatletter
% ----------------------------------------------------------------------
%  Definition of colors
% ----------------------------------------------------------------------
\definecolor{editorPurple}{cmyk}{0.5, 1, 0, 0}
\definecolor{editorOrange}{cmyk}{0, 0.8, 1, 0}
\definecolor{editorBlue}{cmyk}{1, 0.6, 0, 0}
\definecolor{editorPink}{cmyk}{0, 1, 0, 0}

% ----------------------------------------------------------------------
%  Definition of HTML5
% ----------------------------------------------------------------------

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

% ----------------------------------------------------------------------
%  Setup for listings
% ----------------------------------------------------------------------

\lstset{%
  % General design
  inputencoding=utf8,
  backgroundcolor=\color{white},
  basicstyle=\tiny\ttfamily\upshape,
  frame=l,
  comment=[l]{;},
  escapeinside={;@}{\^^M},
  escapeinside=`',
  % line-numbers
  xleftmargin={14pt},
  numbers=left,
  stepnumber=1,
  firstnumber=1,
  numberfirstline=true,
  numberstyle=\color{black},
  % Code design
  identifierstyle=\color{editorOrange},
  keywordstyle=\color{editorPink},
  commentstyle=\color{editorGray},
  stringstyle=\color{editorPurple},
  % Code
  language=html,
  alsodigit={.:},
  tabsize=2,
  showtabs=false,
  showspaces=false,
  showstringspaces=false,
  extendedchars=true,
  breaklines=false,
  % German umlauts
  literate=
  {á}{{\'a}}1 {é}{{\'e}}1 {í}{{\'i}}1 {ó}{{\'o}}1 {ú}{{\'u}}1
  {Á}{{\'A}}1 {É}{{\'E}}1 {Í}{{\'I}}1 {Ó}{{\'O}}1 {Ú}{{\'U}}1
  {à}{{\`a}}1 {è}{{\`e}}1 {ì}{{\`i}}1 {ò}{{\`o}}1 {ù}{{\`u}}1
  {À}{{\`A}}1 {È}{{\'E}}1 {Ì}{{\`I}}1 {Ò}{{\`O}}1 {Ù}{{\`U}}1
  {ä}{{\"a}}1 {ë}{{\"e}}1 {ï}{{\"i}}1 {ö}{{\"o}}1 {ü}{{\"u}}1
  {Ä}{{\"A}}1 {Ë}{{\"E}}1 {Ï}{{\"I}}1 {Ö}{{\"O}}1 {Ü}{{\"U}}1
  {â}{{\^a}}1 {ê}{{\^e}}1 {î}{{\^i}}1 {ô}{{\^o}}1 {û}{{\^u}}1
  {Â}{{\^A}}1 {Ê}{{\^E}}1 {Î}{{\^I}}1 {Ô}{{\^O}}1 {Û}{{\^U}}1
  {œ}{{\oe}}1 {Œ}{{\OE}}1 {æ}{{\ae}}1 {Æ}{{\AE}}1 {ß}{{\ss}}1
  {ç}{{\c c}}1 {Ç}{{\c C}}1 {ø}{{\o}}1 {å}{{\r a}}1 {Å}{{\r A}}1
  {€}{{\EUR}}1 {£}{{\pounds}}1
}

% ----------------------------------------------------------------------
%  Check keywords within code
% ----------------------------------------------------------------------

\gdef\lst@BeginTag{%
    \lst@DelimOpen
        \lst@ifextags\else
        {\let\lst@ifkeywords\iftrue
         \lst@ifmarkfirstintag\lst@firstintagtrue\fi\color{editorBlue}}}
\gdef\lst@EndTag{\lst@DelimClose\lst@ifextags\else\color{editorBlue}}

\makeatother

\begin{document}

\begin{lstlisting}[language=HTML5,numbers=left,firstnumber=1]
<!DOCTYPE html>
<html lang="sv">
  <head>
    <title>Kod med indentering</title>
    <meta charset="utf-8">
  </head>
  <body>
    <section>
      <h1>Artiklar om frukt</h1>
      <article>
        <h2>Äpple</h2>
        <p>Text</p>
      </article>
      <article>
        <h2>Apelsin</h2>
        <p>Text</p>
      </article>
    </section>
  </body>
</html>
\end{lstlisting}

\end{document}

Results:

enter image description here

Desired outcome:

Make sure that the break in code line does not occur.

  • You can add a bit more space between the lines with lineskip=1pt,, or use another font (lmodern seems to work) where the Ä is smaller. – Ulrike Fischer Feb 9 '17 at 17:20
  • I tried package minted instead. I did not show that problem. Although I must confess, that on the first try, it used colors, that are not identical with your example. – Jan Feb 9 '17 at 17:29
  • @UlrikeFischer Using lineskip=1pt reformats everything which is not desired. Loading lmodern seems so far as a nice solution. – kexxcream Feb 9 '17 at 17:30
5

LINESPACING

On a lark, I tried changing the literate table to {Ä}{{\smash{\"A}}}1 and it seems to work. Don't know if it is kosher.

COLOR

On the issue of color of the diacritics, I tried a strategy of first extending the change to the literate table to read {Ä}{{\smash{\color{\CLR}\"A}}}1 and then had to figure out how and where to save the color named \CLR. Those changes included

\lstdefinelanguage{HTML5}{...
  tagstyle=\color{editorBlue}\gdef\CLR{editorPink},
...}

and

\lstset{...
  identifierstyle=\color{editorOrange}\gdef\CLR{editorOrange},
  keywordstyle=\color{editorPink}\gdef\CLR{editorPink},
  commentstyle=\color{editorGray}\gdef\CLR{editorGray},
  stringstyle=\color{editorPurple}\gdef\CLR{editorPurple},
...}

and

\gdef\lst@EndTag{\lst@DelimClose\lst@ifextags\else\color{editorBlue}%
                 \gdef\CLR{editorOrange}}

With those changes, and adding a "Ä" to lines 2, 5, 11, and 12 of the listing for test purposes, it seems to work, though I am no expert on listings.

Here is the MWE.

\documentclass{scrreprt}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[swedish]{babel}
\usepackage{listings}
\usepackage{xcolor}

\makeatletter
% ----------------------------------------------------------------------
%  Definition of colors
% ----------------------------------------------------------------------
\definecolor{editorPurple}{cmyk}{0.5, 1, 0, 0}
\definecolor{editorOrange}{cmyk}{0, 0.8, 1, 0}
\definecolor{editorBlue}{cmyk}{1, 0.6, 0, 0}
\definecolor{editorPink}{cmyk}{0, 1, 0, 0}

% ----------------------------------------------------------------------
%  Definition of HTML5
% ----------------------------------------------------------------------

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

% ----------------------------------------------------------------------
%  Setup for listings
% ----------------------------------------------------------------------

\lstset{%
  % General design
  inputencoding=utf8,
  backgroundcolor=\color{white},
  basicstyle=\tiny\ttfamily\upshape,
  frame=l,
  comment=[l]{;},
  escapeinside={;@}{\^^M},
  escapeinside=`',
  % line-numbers
  xleftmargin={14pt},
  numbers=left,
  stepnumber=1,
  firstnumber=1,
  numberfirstline=true,
  numberstyle=\color{black},
  % Code design
  identifierstyle=\color{editorOrange}\gdef\CLR{editorOrange},
  keywordstyle=\color{editorPink}\gdef\CLR{editorPink},
  commentstyle=\color{editorGray}\gdef\CLR{editorGray},
  stringstyle=\color{editorPurple}\gdef\CLR{editorPurple},
  % Code
  language=html,
  alsodigit={.:},
  tabsize=2,
  showtabs=false,
  showspaces=false,
  showstringspaces=false,
  extendedchars=true,
  breaklines=false,
  % German umlauts
  literate=
  {á}{{\'a}}1 {é}{{\'e}}1 {í}{{\'i}}1 {ó}{{\'o}}1 {ú}{{\'u}}1
  {Á}{{\'A}}1 {É}{{\'E}}1 {Í}{{\'I}}1 {Ó}{{\'O}}1 {Ú}{{\'U}}1
  {à}{{\`a}}1 {è}{{\`e}}1 {ì}{{\`i}}1 {ò}{{\`o}}1 {ù}{{\`u}}1
  {À}{{\`A}}1 {È}{{\'E}}1 {Ì}{{\`I}}1 {Ò}{{\`O}}1 {Ù}{{\`U}}1
  {ä}{{\"a}}1 {ë}{{\"e}}1 {ï}{{\"i}}1 {ö}{{\"o}}1 {ü}{{\"u}}1
  {Ä}{{\smash{\color{\CLR}\"A}}}1 {Ë}{{\"E}}1 {Ï}{{\"I}}1 {Ö}{{\"O}}1 {Ü}{{\"U}}1
  {â}{{\^a}}1 {ê}{{\^e}}1 {î}{{\^i}}1 {ô}{{\^o}}1 {û}{{\^u}}1
  {Â}{{\^A}}1 {Ê}{{\^E}}1 {Î}{{\^I}}1 {Ô}{{\^O}}1 {Û}{{\^U}}1
  {œ}{{\oe}}1 {Œ}{{\OE}}1 {æ}{{\ae}}1 {Æ}{{\AE}}1 {ß}{{\ss}}1
  {ç}{{\c c}}1 {Ç}{{\c C}}1 {ø}{{\o}}1 {å}{{\r a}}1 {Å}{{\r A}}1
  {€}{{\EUR}}1 {£}{{\pounds}}1
}

% ----------------------------------------------------------------------
%  Check keywords within code
% ----------------------------------------------------------------------

\gdef\lst@BeginTag{%
    \lst@DelimOpen
        \lst@ifextags\else
        {\let\lst@ifkeywords\iftrue
         \lst@ifmarkfirstintag\lst@firstintagtrue\fi\color{editorBlue}}}
\gdef\lst@EndTag{\lst@DelimClose\lst@ifextags\else\color{editorBlue}%
                 \gdef\CLR{editorOrange}}

\makeatother

\begin{document}

\begin{lstlisting}[language=HTML5,numbers=left,firstnumber=1]
<!DOCTYPE html>
<html lang="Äsv">
  <head>
    <title>Kod med indentering</title>
    <meta charset="utf-8Ä">
  </head>
  <body>
    <section>
      <h1>Artiklar om frukt</h1>
      <article>
        <Äh2>Äpple</Äh2>
        <pÄ>TexÄt</pÄ>
      </article>
      <article>
        <h2>Apelsin</h2>
        <p>Text</p>
      </article>
    </section>
  </body>
</html>
\end{lstlisting}

\end{document}

enter image description here

  • Copy/pasted your literate code and did a run. Same results, the gap is still there. Using pdfLaTex. – kexxcream Feb 9 '17 at 17:27
  • @kexxcream I am also using pdflatex, and since I don't fully understand the underlying mechanism, I'm not sure I can add more at this time, other than to say, on my system, the change made the difference. – Steven B. Segletes Feb 9 '17 at 17:30
  • I think, the black "Ä" also seems to be part of the problem? – Jan Feb 9 '17 at 17:30
  • @StevenB.Segletes Thanks, appreciated. Yes, the Ä seems to be part of the problem. – kexxcream Feb 9 '17 at 17:32
  • 1
    @Jan Of course, {Ä}{{\smash{\color{editorOrange}\"A}}}1 will fix this case, but only if umlauted capital A's only appear as identifiers, and not as keywords, comments, or strings. – Steven B. Segletes Feb 9 '17 at 17:35

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.