使用非英文字符会破坏列表

使用非英文字符会破坏列表

问题:

当使用非英文字符时,列表包会在代码数字旁边的行中创建空格。

最小工作示例(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}

结果:

在此处输入图片描述

期望的结果:

确保代码行中不会出现中断。

答案1

行间距

出于好玩,我尝试将literate表格改为{Ä}{{\smash{\"A}}}1,似乎有效。不知道这是否符合犹太教规。

颜色

关于变音符号的颜色问题,我尝试了一种策略,首先将更改扩展到识字表以进行阅读{Ä}{{\smash{\color{\CLR}\"A}}}1,然后必须弄清楚如何以及在何处保存名为的颜色\CLR。这些更改包括

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

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

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

经过这些更改,并为了测试目的在清单的第 2、5、11 和 12 行添加了“Ä”,它似乎可以工作,尽管我不是这方面的专家listings

这是 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}

在此处输入图片描述

相关内容