4

I'm trying to use the listings package to format a primitive, line-oriented, DSL. The language has three types of line: comment, command, and data.

Here's an MWE that 'works but not quite':

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\lstset{%
  basicstyle=\small\ttfamily,
  frame=single,
  morecomment=[l][\color{green}]{*},
  morecomment=[l][\color{red}]{\#},
  }
\begin{document}
\begin{lstlisting}
* A comment
* A # comment
# A command
# A * command
  Data
  Some # data
  Some * data
\end{lstlisting}
\end{document}

resulting in this:

Partially correct output

Now, I need to stop the interpretation of # and * in data as starting comments, so I tried to use the morecomment=[f] option to replace my [l] options:

  morecomment=[f][0][\color{green}]{*},
  morecomment=[f][0][\color{red}]{\#},

but this now stops on the first line of the listing, with

! Missing number, treated as zero. <to be read again>  \protect  l.13
*
     A comment ?

Can anyone tell me what I'm doing wrong?

4

You're almost there, you just need

  morecomment=[f][\color{green}][0]{*},
  morecomment=[f][\color{red}][0]{\#},

output of code

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\lstset{%
  basicstyle=\small\ttfamily,
  frame=single,
  morecomment=[f][\color{green}][0]{*},
  morecomment=[f][\color{red}][0]{\#},
  }
\begin{document}
\begin{lstlisting}
* A comment
* A # comment
# A command
# A * command
  Data
  Some # data
  Some * data
\end{lstlisting}
\end{document}
  • 1
    "You're almost there" -- that's too kind, thank you! – Brent.Longborough Oct 11 '17 at 19:08

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.