使用支持 Unicode 的引擎

使用支持 Unicode 的引擎

问题:

  • 问题 1(已解决):listings与包装一起使用时,瑞典字母 ÅÄÖ/åäö 无法正确打印。
  • 问题 2:如何解决这个问题,让瑞典语字母也像其他字母一样变成橙色?
  • 问题 3:如何解决才能让 PHP 行(第 10 行)与第 9 行位于同一行?

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

这些错误表明瑞典语字母存在问题。我使用literatelistings软件包解决了这个问题,但没有成功。

目前结果:

在此处输入图片描述

期望的结果:

字母ä颜色与其他字母相同(即橙色),且 PHP 代码与元素位于同一行<p>

答案1

使用支持 Unicode 的引擎

如果可以选择将引擎更改为 XeTeX 或 LuaTeX,那么您可以literate从中删除内容\lstset,然后删除inputenc并添加fontspec。 经过这些修改后,它就可以正常工作了。

请注意,在下面的代码中,我在需要的地方设置了和 来belowskip消除不同之间的差距,而不是使用。aboveskip0ptlistings\vspace{-12pt}

在此处输入图片描述

% !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}

使用pdflatex

虽然以下内容对其的作用pdflatex有限。这或多或少直接取自手册的第 9 节listingsliterate内容再次被删除,并添加了两行\lstset

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

带有斯堪的纳维亚字符的注释是在`和之间输入的'escapeinside表示在指定的两个字符之间,listings跳转回“正常”LaTeX 模式。该escapebegin行表示\color规范将添加到每个转义部分的开头,并且由于 LaTeX 照常呈现这一点,因此文本是彩色的。

您会看到限制在哪里——正如这里所用,所有此类文本块都将具有相同的颜色。如果您只在注释中使用此类字符,那么没有问题。如果您在其他地方有它们,您将不得不\color{..}在每个转义序列的开头添加一个额外的字符,该字符应该具有不同的颜色,就像\color{editorOrange}在本例中一样。因此,这意味着很多额外的操作。

(请注意,我并不是这方面的专家listings,因此可能有更好的方法来做到这一点。)

% !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}

答案2

这是我针对问题第一部分想出的解决方案。在标题中输入以下行:

\usepackage{inconsolata} % Swedish encoding in lstlisting

然后将下面的代码放在您想要的代码列表的位置。

\lstset{
  language=bash, % Switch code language ... bash is the default
  basicstyle=\ttfamily\footnotesize,
  numberstyle=\tiny,
  numbers=left,
  backgroundcolor=\color{gray!10},
  frame=single,
  tabsize=2,
  rulecolor=\color{black!30},
  title=\lstname,
  escapeinside={\%*}{*)},
  breaklines=true,
  breakatwhitespace=true,
  framextopmargin=2pt,
  framexbottommargin=2pt,
  inputencoding=utf8,
  extendedchars=true,
  % Support for Swedish, German and Portuguese umlauts
  literate=%
  {Ö}{{\"O}}1
  {Ä}{{\"A}}1
  {Å}{{\AA{}}}1
  {Ü}{{\"U}}1
  {ß}{{\ss}}1
  {ü}{{\"u}}1
  {ö}{{\"o}}1
  {ä}{{\"a}}1
  {å}{{\aa{}}}1
  {á}{{\'a}}1
  {ã}{{\~a}}1
  {é}{{\'e}}1,
}
\lstinputlisting[language=bash]{your_code_file.txt}

相关内容