In the following code, I've decided to use the code from Colored background in inline listings so as to redefine \verb to have the possibility to use colors for the text and the background of the verbatim text.
The problem I meet is that only one color for the background can be used by page and I would like to have different background colors in the same page.
This is surely the consequence of the use of the style:
\tikzstyle{highlighter} = [%
\bck@color, %
line width = 0.9\baselineskip%
]
What are the changes to do in the following code?
Another question: is it possible to have a border rule with also a customizable color?
% Two compilations must be done !
\documentclass{article}
\usepackage[utf8]{inputenc}
% == EXTRA == %
\setlength{\parindent}{0cm}
% == PACKAGES USED == %
\usepackage{xargs}
\usepackage{etoolbox}
\usepackage{atbegshi}
\usepackage{ifthen}
\usepackage[svgnames,x11names]{xcolor}
\usepackage{tikz}
\usepackage{listings}
% == DEF : Verbatim with color option == %
\definecolor{colorCodeNormal}{named}{black}
\definecolor{colorCodeBack}{named}{Snow2}
\lstset{backgroundcolor=\color{colorCodeBack}}%
\makeatletter
\newcounter{highlight}[page]
\newcommand{\tikzhighlightanchor}[1]{%
\ensuremath{%
\vcenter{%
\hbox{%
\tikz[remember picture, overlay]{
\coordinate (#1 highlight \arabic{highlight});
}%
}%
}
}%
}
\newcommand{\bh}[0]{\stepcounter{highlight}\tikzhighlightanchor{begin}}
\newcommand{\eh}[0]{\tikzhighlightanchor{end}}
\AtBeginShipout{%
\AtBeginShipoutUpperLeft{
\ifthenelse{\value{highlight} > 0}{\tikz[remember picture, overlay]{\foreach \stroke in {1,...,\arabic{highlight}} \draw[highlighter] (begin highlight \stroke) -- (end highlight \stroke);}}{}
}
}
\newtoggle{@InInlineListing}%
\togglefalse{@InInlineListing}%
\renewcommand\lstinline[1][]{%
\leavevmode\bgroup\toggletrue{@InInlineListing}\bh % \hbox\bgroup --> \bgroup
\def\lst@boxpos{b}%
\lsthk@PreSet\lstset{flexiblecolumns,#1}%
\lsthk@TextStyle
\@ifnextchar\bgroup{\afterassignment\lst@InlineG \let\@let@token}%
\lstinline@}%
\def\lst@LeaveAllModes{%
\ifnum\lst@mode=\lst@nomode
\expandafter\lsthk@EndGroup\iftoggle{@InInlineListing}{\eh{}}{}%
\else
\expandafter\egroup\expandafter\lst@LeaveAllModes
\fi%
}
\renewcommand\verb{%
\@ifstar{%
\lstset{%
showspaces=true,%
showtabs=true%
}%
\@verbItInColor%
}{%
\lstset{%
showspaces=false,%
showtabs=false%%
}%
\@verbItInColor%
}%
}
\newcommandx\@verbItInColor[2][%
1=colorCodeNormal,%
2=colorCodeBack
]{%
\ifx\relax#1\relax
\edef\txt@color{colorCodeNormal}%
\else
\edef\txt@color{#1}%
\fi
\ifx\relax#2\relax
\edef\bck@color{colorCodeBack}%
\else
\edef\bck@color{#2}%
\fi
\lstset{basicstyle=\ttfamily\color{\txt@color}}%
\tikzstyle{highlighter} = [%
\bck@color, %
line width = 0.9\baselineskip%
]
\lstinline%
}
\makeatother
\begin{document}
% On this first page there is a problem.
\verb+NORMAL+
\verb[blue]+BLUE+
\verb[green][DarkViolet]+GREEN ON DARK VIOLET+
\verb[white][cyan]+WHITE ON CYAN+
\newpage
% Here the background color is correct.
\verb[white][red]+WHITE ON RED+
\newpage
% Here again the backgorund color is the good one.
\verb[green][DarkViolet]+GREEN ON DARK VIOLET+
\end{document}


tikzpagenodespackage (usestikzpagelayers, unfinished as well). While not complete finished, it should be usable for what you want to do. It stores settings internally for every page, so it would allow multiple colors per page. However, using this to add a background color for inline verbatim is overkill! There are better ways to do this. – Martin Scharrer Apr 09 '12 at 11:18listingsor having a plain verbatim text like with the normal\verb? In the first case add some example line which actually uses some verbatim text which can be highlighted. – Martin Scharrer Apr 09 '12 at 11:24