2

I would like to render the comment character differently, specifically I would like to render Ada's double dash as a long dash. Using literate=*{--}{{---}}1 achieves the mapping, but unfortunately this interferes with listings' ability to render the comment. It is no longer formated as comment. How can I achieve my goal without this side effect?

Here is some sample code demonstrating the problem:

 \documentclass[10pt,a4paper]{article}
 \usepackage[utf8]{inputenc}
 \usepackage[T1]{fontenc}
 \usepackage{listings}

 \usepackage{mathptmx}
 \usepackage[scaled]{helvet}
 \usepackage{txfonts}

 \lstset{basicstyle=\sffamily}
 \lstset{texcl}
 \lstset{literate=%
 *{:=}{{$\coloneqq$}}1
 {+}{{$+$}}1
 {:}{{$\colon$}}1
 {<=}{{$\leq$}}1
 {>=}{{$\geq$}}1
 {/=}{{$\neq$}}1
 {=}{{$=$}}1
 {=>}{{$\Rightarrow$}}1
 {--}{{---} }1
 }
 \lstset{language=Ada,tabsize=3,columns=fullflexible,showstringspaces=false,escapechar=`,keepspaces}


 \title{Test}

 \begin{document}
 \maketitle

 \section{Code Listings}

 \begin{lstlisting}
 function Perform_Operation(X: Integer);
 -- This is a comment, keywords such as begin end
 -- should not be highligted and should be in
 -- commentstyle

 \end{lstlisting}
  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – DG' Feb 11 '19 at 10:19
  • Welcome. Could you provide a small sample file for testing? – Andrew Swann Feb 11 '19 at 10:21
  • {--}{{$---$}}1 is sufficient? – ferahfeza Feb 11 '19 at 10:51
  • No @ferahfeza, that still interferes with comment styling – Akuri Feb 11 '19 at 13:17
2

You can use the escape mechanism, e.g. via

escapeinside={--}{\^^M},escapebegin={--},escapeend={}

which captures the line from -- to the end, typesets an endash and the comment text as ordinary LaTeX.

Sample output

\documentclass[10pt,a4paper]{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{listings}

\usepackage{mathptmx}
\usepackage[scaled]{helvet}
\usepackage{txfonts}

\lstset{language=Ada,basicstyle=\sffamily,texcl,
literate=%
*{:=}{{$\coloneqq$}}1
{+}{{$+$}}1
{:}{{$\colon$}}1
{<=}{{$\leq$}}1
{>=}{{$\geq$}}1
{/=}{{$\neq$}}1
{=}{{$=$}}1
{=>}{{$\Rightarrow$}}1,
tabsize=3,
columns=fullflexible,
showstringspaces=false,
escapechar=`,
keepspaces,
escapeinside={--}{\^^M},escapebegin={--},escapeend={}}

\title{Test}

\begin{document}
\maketitle

\section{Code Listings}

\begin{lstlisting}
function Perform_Operation(X: Integer);
-- This is a comment, keywords such as begin end
-- should not be highligted and should be in
-- commentstyle

\end{lstlisting}

\end{document}

You can add further styling to the output if needed via escapebegin. For example, to italic text after the dash, you can use

escapebegin={--\itshape}

italic sample

| improve this answer | |
  • This works quite well, a minor problem I noticed is that if there is a double dash inside a string ("something -- something"), the escape mechanism is confused and the rest of the program is rendered in latex mode. – Akuri Feb 11 '19 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.