如何在列表中插入带重音符号(扩展字符)的关键字

如何在列表中插入带重音符号(扩展字符)的关键字

这就是我想要做的:

morekeywords={SENÃO, ENTÃO, FAÇA, ATÉ, ENQUANTO, PARA, SE, DECLARA, DECLARE, E, OU, FIM_ENQUANTO, FIM_PARA}

我正在定义一种新语言,并且我想使用自然语言词汇作为关键词。

问题出在关键字上:SENÃO, ENTÃO, FAÇA, ATÉ

我已经解决了第一个问题,即显示特殊字符: 如何在列表中插入带有重音符号的代码?

这里alsoletter对我不起作用,使用它时出现错误:alsoletter={Ç,É,Ã}

测试.tex

\documentclass{article}

\usepackage[brazilian]{babel}
\usepackage[T1]{fontenc} 
\usepackage[utf8]{inputenc}
\usepackage{listingsutf8}

\usepackage{listings}

\lstset{%
        inputencoding=utf8,
        extendedchars=true,
        literate=%
        {é}{{\'{e}}}1
        {è}{{\`{e}}}1
        {ê}{{\^{e}}}1
        {ë}{{\¨{e}}}1
        {É}{{\'{E}}}1
        {Ê}{{\^{E}}}1
        {û}{{\^{u}}}1
        {ù}{{\`{u}}}1
        {â}{{\^{a}}}1
        {à}{{\`{a}}}1
        {á}{{\'{a}}}1
        {ã}{{\~{a}}}1
        {Á}{{\'{A}}}1
        {Â}{{\^{A}}}1
        {Ã}{{\~{A}}}1
        {ç}{{\c{c}}}1
        {Ç}{{\c{C}}}1
        {õ}{{\~{o}}}1
        {ó}{{\'{o}}}1
        {ô}{{\^{o}}}1
        {Õ}{{\~{O}}}1
        {Ó}{{\'{O}}}1
        {Ô}{{\^{O}}}1
        {î}{{\^{i}}}1
        {Î}{{\^{I}}}1
        {í}{{\'{i}}}1
        {Í}{{\~{Í}}}1
}

\lstdefinelanguage{pseudo}
{
extendedchars=true,
alsoletter={_},
morekeywords={ENQUANTO,PARA,SE,SEN\~{A}O,ENTÃO,ATÉ,FA\c{C}A,DECLARA,DECLARE,E,OU,FIM_ENQUANTO,FIM_PARA,FAÇA},
ndkeywords={NUMERO,TEXTO,LEIA,ESCREVA},
sensitive=true,
morecomment=[l]{//},
morecomment=[s]{/*}{*/},
morestring=[b]"
}

\begin{document}
\lstinputlisting[language=pseudo]{foo.pseudo}
\end{document}

foo.伪

ENQUANTO nota != -1 FAÇA
// Comment example
 LEIA nota
 SE nota >= 7 ENTÃO
   ESCREVA "Aprovado"
 SENÃO
   ESCREVA "Reprovado"
   TEST "Algo mais"
/*
mais 
comentário
*/

FIM_ENQUANTO 

我见过:

答案1

我设法通过复制来“解决”这个问题关键词样式在有文化的集合内。可能有更好的方法,但这种方法对一些关键词很有效。

\documentclass{article}

\usepackage[brazilian]{babel}
\usepackage[utf8]{inputenc}
\usepackage{color}
\usepackage{listings}

\newcommand\keywordstyle[1]{{\color{red}\bfseries{#1}}}%

\lstset{%
literate=
        {Ç}{{\c{C}}}1
        {Ã}{{\~{A}}}1
        {á}{{\'{a}}}1
        {ENTÃO}{{\keywordstyle{ENT\~{A}O}}}5
        {SENÃO}{{\keywordstyle{SEN\~{A}O}}}5
        {FAÇA}{{\keywordstyle{FA\c{C}A}}}4
}

\lstdefinelanguage{pseudo}
{
extendedchars=true,
alsoletter={_},
morekeywords={ENQUANTO,PARA,SE,DECLARA,DECLARE,E,OU,FIM_ENQUANTO,FIM_PARA},
ndkeywords={NUMERO,TEXTO,LEIA,ESCREVA},
sensitive=true,
morecomment=[l]{//},
morecomment=[s]{/*}{*/},
morestring=[b]"
}

\begin{document}
\lstinputlisting[language=pseudo]{foo.pseudo}
\end{document}

答案2

对于匈牙利语文档,以下设置适用于 TeX Live 2013 和 PDFLaTeX。使用 XeLaTeX,您应该使用该选项转义ő/Őű/Ű字符escapeinside

\documentclass{report}

\usepackage[magyar]{babel}
\usepackage{ifxetex}
\ifxetex
  \usepackage{fontspec}
\else
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
  \usepackage{lmodern}
\fi
\usepackage{listings}

\lstset{
    basicstyle=\ttfamily,
    keepspaces=true,
    escapeinside={(*@}{@*)}, % only required for XeLaTeX
    columns=flexible, % only use with keepspaces=true       
    literate=*
        {á}{{\'a}}1
        {é}{{\'e}}1
        {í}{{\'i}}1
        {ó}{{\'o}}1
        {ö}{{\"o}}1
        {ő}{{\H{o}}}1
        {ú}{{\'u}}1
        {ü}{{\"u}}1
        {ű}{{\H{u}}}1
        {Á}{{\'A}}1
        {É}{{\'E}}1
        {Í}{{\'I}}1
        {Ó}{{\'O}}1
        {Ö}{{\"O}}1
        {Ő}{{\H{O}}}1
        {Ú}{{\'U}}1
        {Ü}{{\"U}}1
        {Ű}{{\H{U}}}1
}

\begin{document}

% for PDFLaTeX
\begin{lstlisting}
árvíztűrő tükörfúrógép á é í ó ö ő ú ü ű
ÁRVÍZTŰRŐ TÜKÖRFÚRÓGÉP Á É Í Ó Ö Ő Ú Ü Ű
\end{lstlisting}

% for XeLaTeX
\begin{lstlisting}
árvízt(*@ű@*)r(*@ő@*) tükörfúrógép á é í ó ö (*@ő@*) ú ü (*@ű@*)
ÁRVÍZT(*@Ű@*)R(*@Ő@*) TÜKÖRFÚRÓGÉP Á É Í Ó Ö (*@Ő@*) Ú Ü (*@Ű@*)
\end{lstlisting}

\end{document}

请注意,如果没有keepspaces=true,该columns=flexible选项将导致重音字符前的(一些)空格字符消失。

相关内容