Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm looking for a possibility to find a way to colorize data. If a text starts in a line with XYZ from that point until the next blank line the text should be set in a defined colour. With the comment feature like

comment=[s]{^XYZ}{

}

it is not possible.

comment=[s]{XYZ}{</end>}

works, but it is not solution for my need. At least, XYZ can also be in the normal text, but only if a line starts with it, the text block should be coloured. Is there a solution with listings?

Here is an example:

\documentclass{report}
\usepackage{listings,xcolor}
\lstdefinelanguage{text}{}
\lstnewenvironment{mylang}{\lstset{language=text,
    comment=[s]{XYZ}{</end>},%
    commentstyle=\color{green},%
    basicstyle=\footnotesize\ttfamily,%
    showstringspaces=false,%
    showspaces=false,%
  }}{}

\begin{document}
\begin{mylang}
Only a test, this is not a comment
XYZ this should colored as a comment
and this line too

this is not a command, because only lines starting with XYZ
are comments </end>

This is not a comment, too
\end{mylang}
\end{document}

Here is an example:

\documentclass{report}
\usepackage{listings,xcolor}
\lstdefinelanguage{text}{}
\lstnewenvironment{mylang}{\lstset{language=text,
    comment=[s]{XYZ}{</end>},%
    commentstyle=\color{green},%
    basicstyle=\footnotesize\ttfamily,%
    showstringspaces=false,%
    showspaces=false,%
  }}{}

\begin{document}
\begin{mylang}
Only a test, this is not a comment
XYZ this should colored as a comment
and this line too

this is not a command, because only lines starting with XYZ
are comments </end>

This is not a comment, too
\end{mylang}
\end{document}
share|improve this question

1 Answer 1

up vote 3 down vote accepted

You have to change the \catcode of the newline character to other before defining the environment. Afterwards, you probably want to change the old meaning back:) The following code does the trick. It looks ugly, because everything is on one line, but it has to be, since newline is not newline in here! By the way, if you prefer, you can replace ^^Ms with actual newlines.

{\catcode`\^^M=12 \def\marshal{\lstnewenvironment{mylang}{\lstset{language=text,comment=[s]{^^MXYZ}{^^M^^M},commentstyle=\color{green},basicstyle=\footnotesize\ttfamily,showstringspaces=false,showspaces=false,}}{}}\expandafter}\marshal
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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