10

This is what I would like to do:

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

I'm defining a new language, and I would like to use natural language words as keywords.

The problem is with keywords: SENÃO, ENTÃO, FAÇA, ATÉ.

I have solved the first problem, in showing special chars with: How to insert code with accents with listings?

The alsoletter here, doesn't work for me, I got an error when using it: alsoletter={Ç,É,Ã}.

test.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.pseudo

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 

I have seen:

  • Welcome to TeX.SX! Your post was migrated here from another Stack Exchange site. Please register on this site, too, and make sure that both accounts are associated with each other (by using the same OpenID), otherwise you won't be able to comment on or accept answers or edit your question. – Joseph Wright Jul 07 '13 at 05:49
  • Remark - you're supposed to not include the comma in alsoletter specification -- but the reaon why it error out is lst@SelectStdCharTable does not include the definitions for those characters, basically listings just have poor utf8 support -- I guess it's possible to somehow hack all the characters in 128-255 to be "letter", but using the literate hack seems to be the best option here. – user202729 Aug 01 '22 at 14:37

2 Answers2

7

I managed to "solve" this by replicating the keywordstyle within the literate set. There is probably a better way to do it, but this works well for a few keywords.

\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}
gnramos
  • 71
2

For Hungarian documents, the following settings work with TeX Live 2013 and PDFLaTeX. With XeLaTeX, you should escape the ő/Ő and ű/Ű characters with the escapeinside option.

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

Note that without keepspaces=true, the columns=flexible option will cause (some) space characters before the accented characters to disappear.