1

I am trying to change color of text in the listing. I use delimiters, but it seems that they remove indentation.

Here is my setup:

\usepackage{listings}
\usepackage{color}

\definecolor{javared}{rgb}{0.6,0,0} % for strings
\definecolor{javagreen}{rgb}{0.25,0.5,0.35} % comments
\definecolor{javapurple}{rgb}{0.5,0,0.35} % keywords
\definecolor{javadocblue}{rgb}{0.25,0.35,0.75} % javadoc

\lstset{language=Java,
    basicstyle=\small\ttfamily,
    frame=single,
    breaklines=true,
    columns=fullflexible,
    keywordstyle=\color{javapurple}\bfseries,
    stringstyle=\color{javared},
    commentstyle=\color{javagreen},
    morecomment=[s][\color{javadocblue}]{/**}{*/},
    tabsize=4,
    showspaces=false,
    showstringspaces=false,
    escapeinside={(*@}{@*)},
    moredelim=[il][\textcolor{pyellow}]{$$},
    moredelim=[is][\textcolor{pgrey}]{\%\%}{\%\%},
    moredelim=[is][\color{red}]{|}{|},
    moredelim=[is][\color{green}]{#}{#}
}

\begin{lstlisting}
|+      THIS SHOULD BE GREEN|
#-      THIS SHOULD BE RED#
\end{lstlisting}

This is output with missing indentation:

Missing identation

Are my settings correct? Is there any way to keep indentation or insert it?

2

Beside the spacing problem, note that the # character in LaTeX has a special meaning and so needs to be escaped when used as a delimiter for listings. Your last delimiter definition should thus read

moredelim=[is][\color{green}]{\#}{\#}

The missing spaces occur because you are using the option columns=fullflexible. For column alignment, listings has four options, namely fixed, flexible, spaceflexible and fullflexible. The differences between them are described in section 2.10 of the listings manual.

For longer, monospaced listings you probably want to use the fixed or flexible format. The effects of the four options on your example can be seen in the following image:

enter image description here

  • Thank you very much, now it works as expected. – BugHunter Sep 18 at 23:05

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.