如何在列表环境中将星号(*)仅应用于少数识字者?

如何在列表环境中将星号(*)仅应用于少数识字者?

我对“识字”的定义有疑问:

\lstset{
  numbers=left, numberstyle=\footnotesize, stepnumber=1, numbersep=12pt,
  frame=none, framesep=7pt,
  breaklines=true,
  basicstyle=\footnotesize,
  showstringspaces=false,
  upquote=true,
  columns=fullflexible,
  showstringspaces=false,
  commentstyle=\color{gray}\upshape,
  stringstyle=\color{black}\ttfamily,
  keywordstyle=\color{darkblue},
  captionpos=b,
  extendedchars=true,
  literate=
  *{0}{{{\color{DarkGreen}0}}}1
  {1}{{{\color{DarkGreen}1}}}1
  {2}{{{\color{DarkGreen}2}}}1
  {3}{{{\color{DarkGreen}3}}}1
  {4}{{{\color{DarkGreen}4}}}1
  {5}{{{\color{DarkGreen}5}}}1
  {6}{{{\color{DarkGreen}6}}}1
  {7}{{{\color{DarkGreen}7}}}1
  {8}{{{\color{DarkGreen}8}}}1
  {9}{{{\color{DarkGreen}9}}}1
  {á}{{\'a}}1
  {é}{{\'e}}1
  {í}{{\'i}}1
  {ó}{{\'o}}1
  {ú}{{\'u}}1
}

注释和字符串中的数字颜色必须被忽略(因此开头为 *),但重音符号的规则则不能。这是因为带有不同语言字符串的源代码稍后会包含在选项中\lstinputlisting,如果不更改 unicode 字符,它就会崩溃。

那么,有人知道将 * 仅应用于部分文字定义的方法吗?我尝试在包含文件的特定列表中定义它们,但第二个文字消除了先前的定义。

附言:我也尝试过用 来改变文件的编码\inputencoding=utf8/latin1,但是这会导致编译速度非常慢并且返回错误Missing $ inserted.

编辑:我正在添加一个完全可编译的 MWE,我认为这代表了问题所在。输出根据为 Javascript 定义的规则突出显示颜色。但是,正如我之前所说,在第 22 行(参见输出)中,数字是绿色的,即使是注释。如果在数字前添加 *,编译会崩溃,因为字符串内的 unicode 字符不会被替换。

(注意:javascript 没有任何意义)

\documentclass[spanish, 11pt]{book}

\usepackage[utf8]{inputenc}
\usepackage[margin=2.5cm]{geometry}
\usepackage[spanish]{babel}
\usepackage{fancyhdr}
\usepackage{graphicx}
\usepackage[colorlinks=true, linkcolor=black, citecolor=black, urlcolor=blue]{hyperref}
\usepackage[format=hang]{caption}
\usepackage{appendix}
\usepackage{color}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{listings}
\usepackage{listingsutf8}
\usepackage{float}
\usepackage{textcomp}
\usepackage{underscore}


\lstdefinelanguage{JavaScript}{
  keywords={typeof, new, true, false, catch, function, return, null, catch, switch, var, if, in, while, do, else, case, break, ajax, each, get, post},
  keywordstyle=\color{darkblue}\bfseries,
  keywords=[2]{class, export, boolean, throw, implements, import, this},
  keywordstyle=[2]\color{darkgray}\bfseries,
  identifierstyle=\color{black},
  sensitive=false,
  comment=[l]{//},
  morecomment=[s]{/*}{*/},
  commentstyle=\color{FireBrick}\ttfamily,
  stringstyle=\color{DarkOrange}\ttfamily,
  morestring=[b]',
  morestring=[b]"
}

\lstdefinestyle{Javascript}{
   language=JavaScript,
   extendedchars=true,
   basicstyle=\footnotesize,
   showstringspaces=false,
   showspaces=false,
   tabsize=2,
   breaklines=true,
   showtabs=false,
   captionpos=b,
}

\lstset{
language=Javascript,
style=Javascript,
  numbers=left, numberstyle=\footnotesize, stepnumber=1, numbersep=12pt,
  frame=none, framesep=7pt,
  breaklines=true,
  basicstyle=\footnotesize,
  showstringspaces=false,
  upquote=true,
  columns=fullflexible,
  showstringspaces=false,
  keywordstyle=\color{DarkBlue},
  captionpos=b,
  extendedchars=true,
  literate=
  {0}{{{\color{DarkGreen}0}}}1
  {1}{{{\color{DarkGreen}1}}}1
  {2}{{{\color{DarkGreen}2}}}1
  {3}{{{\color{DarkGreen}3}}}1
  {4}{{{\color{DarkGreen}4}}}1
  {5}{{{\color{DarkGreen}5}}}1
  {6}{{{\color{DarkGreen}6}}}1
  {7}{{{\color{DarkGreen}7}}}1
  {8}{{{\color{DarkGreen}8}}}1
  {9}{{{\color{DarkGreen}9}}}1
  {á}{{\'a}}1 
  {é}{{\'e}}1
  {í}{{\'i}}1
  {ó}{{\'o}}1
  {ú}{{\'u}}1
  {Á}{{\'A}}1
  {É}{{\'E}}1
  {Í}{{\'I}}1
  {Ó}{{\'O}}1
  {Ú}{{\'U}}1
  {¡}{{!`}}1
  {¿}{{?`}}1
  {·}{{$\cdot$}}1
}

\begin{document}
\begin{lstlisting}
function manageLanguage(text)
{ // Language options in interface and errors
    switch (language)
    {
case "Welcome":
        return '¡Bienvenido a '

        case "Browse":
        return '<b>Navega</b> por tu disco duro y selecciona lo(s) fichero(s) a visualizar o simplemente <b>arrástralos</b> al área del botón'

        case "Default":
        return 'Ejemplo por Defecto'
    }

    case "Catalan":
        switch (text)
        {

        case "AlreadySession":
        return 'Ja tens una sessió oberta. Sel·lecciona Nou al menú Fitxer per tancarla i apujar nous fitxers.'

        //var start = (Math.round(this.x*100)/100);
                if (start % intervals == 0)         
                return "<b>" + manageLanguage("Interval") +":</b> [" + Math.round(this.x*10)/10 + " - " + Math.round((this.x+intervals)*10)/10+"]<br/><b>" + manageLanguage(this.series.name) + ":</b> " + this.y + " ";
                else return false; //"<b>" + manageLanguage("Interval") +":</b> [" + this.key + " - " + (this.key+intervals)+"]<br/><b>" + manageLanguage(this.series.name) + ":</b> " + this.y;
\end{lstlisting}

\end{document}

答案1

下面的例子使用了Jubobs 的评论,通过 进行上下文检查\lst@mode

\documentclass[spanish, 11pt]{book}

\usepackage[utf8]{inputenc}
\usepackage[spanish]{babel}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{listings}
\usepackage{listingsutf8}
\usepackage{textcomp}

\makeatletter
\newcommand*{\lstnumcolor}{%
  \ifnum\lst@mode=\lst@Pmode
    \color{DarkGreen}%
  \fi
}
\makeatother

\lstdefinelanguage{JavaScript}{
  keywords={typeof, new, true, false, catch, function, return, null, catch, switch, var, if, in, while, do, else, case, break, ajax, each, get, post},
  keywordstyle=\color{darkblue}\bfseries,
  keywords=[2]{class, export, boolean, throw, implements, import, this},
  keywordstyle=[2]\color{darkgray}\bfseries,
  identifierstyle=\color{black},
  sensitive=false,
  comment=[l]{//},
  morecomment=[s]{/*}{*/},
  commentstyle=\color{FireBrick}\ttfamily,
  stringstyle=\color{DarkOrange}\ttfamily,
  morestring=[b]',
  morestring=[b]"
}

\lstdefinestyle{Javascript}{
   language=JavaScript,
   extendedchars=true,
   basicstyle=\footnotesize,
   showstringspaces=false,
   showspaces=false,
   tabsize=2,
   breaklines=true,
   showtabs=false,
   captionpos=b,
   keepspaces,
}

\lstset{
language=Javascript,
style=Javascript,
  numbers=left, numberstyle=\footnotesize, stepnumber=1, numbersep=12pt,
  frame=none, framesep=7pt,
  breaklines=true,
  basicstyle=\footnotesize,
  showstringspaces=false,
  upquote=true,
  columns=fullflexible,
  showstringspaces=false,
  keywordstyle=\color{DarkBlue},
  captionpos=b,
  extendedchars=true,
  literate=
  {0}{{{\lstnumcolor0}}}1
  {1}{{{\lstnumcolor1}}}1
  {2}{{{\lstnumcolor2}}}1
  {3}{{{\lstnumcolor3}}}1
  {4}{{{\lstnumcolor4}}}1
  {5}{{{\lstnumcolor5}}}1
  {6}{{{\lstnumcolor6}}}1
  {7}{{{\lstnumcolor7}}}1
  {8}{{{\lstnumcolor8}}}1
  {9}{{{\lstnumcolor9}}}1
  {á}{{\'a}}1 
  {é}{{\'e}}1
  {í}{{\'i}}1
  {ó}{{\'o}}1
  {ú}{{\'u}}1
  {Á}{{\'A}}1
  {É}{{\'E}}1
  {Í}{{\'I}}1
  {Ó}{{\'O}}1
  {Ú}{{\'U}}1
  {¡}{{!`}}1
  {¿}{{?`}}1
  {·}{{$\cdot$}}1
}

\begin{document}
\begin{lstlisting}
function manageLanguage(text)
{ // Language options in interface and errors
    String str4 = "Numbers 123 and 4 in string"; // Numbers 123 and 4 in comment
    switch (language)
    {
        case "Welcome":
        return '¡Bienvenido a '

        case "Browse":
        return '<b>Navega</b> por tu disco duro y selecciona lo(s) fichero(s) a visualizar o simplemente <b>arrástralos</b> al área del botón'

        case "Default":
        return 'Ejemplo por Defecto'
    }

    case "Catalan":
        switch (text)
        {

        case "AlreadySession":
        return 'Ja tens una sessió oberta. Sel·lecciona Nou al menú Fitxer per tancarla i apujar nous fitxers.'

        //var start = (Math.round(this.x*100)/100);
                if (start % intervals == 0)         
                return "<b>" + manageLanguage("Interval") +":</b> [" + Math.round(this.x*10)/10 + " - " + Math.round((this.x+intervals)*10)/10+"]<br/><b>" + manageLanguage(this.series.name) + ":</b> " + this.y + " ";
                else return false; //"<b>" + manageLanguage("Interval") +":</b> [" + this.key + " - " + (this.key+intervals)+"]<br/><b>" + manageLanguage(this.series.name) + ":</b> " + this.y;
\end{lstlisting}

\end{document}

结果

评论:

局限性:

  • 标识符内的数字也是彩色的。

包裹listingsutf8

通过包将 UTF-8 转换为 8 位编码的技巧listingsutf8仅适用于\lstinputlisting。因此,需要将代码放入单独的文件中。但是,然后utf8/latin1(或utf8/x-iso-8859-1) 可以与 一起使用,literate=*...以处理除注释之外的数字。

相关内容