alternating color for source code listings

325 views
Skip to first unread message

pipi...@googlemail.com

unread,
Apr 24, 2009, 8:04:46 AM4/24/09
to LaTeX Users Group
Hi @all,

I see that source code listings can be done by
\begin{lstlisting}
[xleftmargin=20pt,numbers=left,caption=Title,language=perl]{name}

#!/usr/bin/perl -w
print "Hello World";
\end{lstlisting}

with following in the praemble.tex

% Define Code Listenings
\lstset{
basicstyle=\ttfamily,
basicstyle=\scriptsize,
keywordstyle=\color{red},
commentstyle=\color{blue},
stringstyle=\color{green},
backgroundcolor=\color{white},
morecomment=[s][\color{blue}]{/*}{*/}
}

so all of this works fine. My problem is that I want alternating color
for my source code.
Like:
#!/usr/bin/perl -w grey
print "Hello World"; white
print "Hello World2"; grey

and so on ....

Is there any other way than setting up a block for every code line?

Many Thanks

Bjoern

Krishnan

unread,
Apr 25, 2009, 10:42:09 PM4/25/09
to LaTeX Users Group


On Apr 24, 5:04 pm, "pipio...@googlemail.com"
<pipio...@googlemail.com> wrote:

> I see that source code listings can be done by
> \begin{lstlisting}
> [xleftmargin=20pt,numbers=left,caption=Title,language=perl]{name}
>
> #!/usr/bin/perl -w
> print "Hello World";
> \end{lstlisting}

......................

> My problem is that I want alternating color
> for my source code.
> Like:
> #!/usr/bin/perl -w grey
> print "Hello World"; white
> print "Hello World2"; grey
>
> and so on ....


If this is the only requirement, why not try this alternative?

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{xcolor}

\begin{document}

\renewcommand{\FancyVerbFormatLine}[1]{%
\ifodd\value{FancyVerbLine}%
\textcolor{gray}{#1}%
\else%
#1%
\fi}

\begin{Verbatim}%
[fontsize=\scriptsize,numbers=left,xleftmargin=20pt]
#!/usr/bin/perl -w
print "Hello World";
print "Hello World2";
\end{Verbatim}

\end{document}












ryan

unread,
Apr 27, 2009, 4:14:16 AM4/27/09
to LaTeX Users Group
Hey,

is it also possible to keep syntax highlighting from listings and make
alternating background colors per line?


greetz
ryan

pipi...@googlemail.com

unread,
Apr 27, 2009, 3:23:25 AM4/27/09
to LaTeX Users Group
Hi Krishnan,

thank you for your answer. But with this way I can't set colors for
the language specific keywords, comments or strings. Is there a way to
combine this both things.

Thank you for your help

Bjoern
Message has been deleted

Martin

unread,
Apr 28, 2009, 5:37:24 AM4/28/09
to LaTeX Users Group
On Apr 27, 8:23 am, "pipio...@googlemail.com"
<pipio...@googlemail.com> wrote:
> Hi Krishnan,
>
> thank you for your answer. But with this way I can't set colors for
> the language specific keywords, comments or strings. Is there a way to
> combine this both things.
>
> Thank you for your help
>
> Bjoern
One other way to do this is to use an external tool to convert your
Perl script into marked up LaTeX code, i.e. it does the syntax
highlighting and verbatim conversion. Then you can use a simple Perl
script (I assume here you can program Perl) to put a \textcolor{this|
that}{ ... } in every line with alternating colors.
One tool which does this is 'texifyperl' from the 'texify' program
bundle. I have it on my Ubuntu box and don't now if is exists for
Windows or Mac, but I guess so. I'm not sure how to configure it to
get the colors you want, but there are other programs, too.

This method would also speed up subsequent LaTeX runs, because the
syntax highlighting does not has to be repeated.

Best,
Martin

Reply all
Reply to author
Forward
0 new messages