7

I want to make someting like the lstlinebgrd package to creating a zebra effect.

\documentclass{article}
\usepackage{lstlinebgrd}
\usepackage[english]{babel}%If not an error occurs
\begin{document}
\begin{lstlisting}[language=C,basicstyle=\ttfamily,linebackgroundcolor={\ifodd\value{lstnumber}\color{green}\fi}]
    First line
    Second line
    Third line
    Fourth line
\end{lstlisting}

but alternate text style for odd and even line, like this non-working code: note the basicstyle={\ifodd\value{lstnumber}\it\fi}

\documentclass{article}
\usepackage{lstlinebgrd}
\usepackage[english]{babel}%If not an error occurs
\begin{document}
\begin{lstlisting}[language=C,basicstyle={\ifodd\value{lstnumber}\it\fi},linebackgroundcolor={\ifodd\value{lstnumber}\color{green}\fi}]
    First line : normalfont
    Second line : italic
    Third line : normal font
    Fourth line : italic
\end{lstlisting}

I have tried to modify the code from : Creating a zebra effect using listings (who led to the package lstlinebgrd) without succes.

I know the listing package, nor the package lstlinebgrd are conceive for that. My latexfuu is not enough to hack a solution. Maybe you can help me, or give me a direction? An other package maybe. The solution don't have to be for code but for text.

1 Answer 1

7

Rather then use of basicstyle which will be applied on all listing code you can modify identifierstyle, commentstyle, stringstyle, keywordstyle like this

\documentclass{article}
\usepackage{lstlinebgrd}
\usepackage[english]{babel}%If not an error occurs

\def\oddtest{\ifodd\value{lstnumber}\normalfont\else\itshape\fi}

\begin{document}
\begin{lstlisting}[language=C,
    identifierstyle=\oddtest,
    commentstyle=\oddtest,
    stringstyle=\oddtest,
    keywordstyle=\oddtest,
    linebackgroundcolor={\ifodd\value{lstnumber}\color{green}\fi}]
    First line : normalfont     % comment
    Second line : italic        % comment
    Third line : normal font
    Fourth line : italic
\end{lstlisting}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .