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.

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}
share|improve this question
    
Looks like you are trying the same as my unfinished tikzpagenodes package (uses tikzpagelayers, 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 9 '12 at 11:18
    
Please state at the beginning what you are looking for. You are referencing another question and then show a lot of code. This makes it difficult to see what's exactly you are trying to achieve. For example are you want to use the syntax highlighting feature of listings or 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 9 '12 at 11:24
add comment

3 Answers

up vote 4 down vote accepted

If you simply want some color background and/or frame around \lstinline you can do so with \fcolorbox from xcolor (or \colorbox if you don't need a frame). No need for TikZ and atbegshi tricks. One issue here is to put the verbatim material inside the macro, which is normally not allowed.

You can use \Collectverb from the newverbs package (not to be confused with \collectverb) to collect the verbatim material in a form suitable to be passed to an internal \lstinline macro. This still won't allow \lstinline to alter any catcodes, but most characters should already have the ones required.

\documentclass{article}

\usepackage{xcolor}
\usepackage{newverbs}
\usepackage{listings}

\makeatletter
\newcommand\myverb[1][green]{%
    \@testopt{\@myverb{#1}}{-#1}% Handle second optional argument
}
\def\@myverb#1[#2]{%
    \Collectverb{\@@myverb{#1}{#2}}%
}
\def\@@myverb#1#2#3{%
    %\begingroup
    %\fboxrule=0.9\baselineskip
    %\fboxsep=...
    \fcolorbox{#2}{#1}{\lstinline|#3|}%
    %\endgroup
}
\makeatother

% Usage:
% \myverb[<background color>][<frame color>]<char><content><char>
% or:
% \myverb[<background color>][<frame color>]{<content>}
\begin{document}

\lstset{language=C}
\myverb|int a = 1;|

\lstset{language=tex}
\myverb[yellow][green]{\relax % Just do it!}

\end{document}

Result

share|improve this answer
    
Thanks for the trick to collect the argument of the macro \lstinline. –  projetmbc Apr 9 '12 at 12:06
    
The solution does not work inside footonote. Is there a way to fix this ? –  projetmbc Apr 9 '12 at 12:36
    
I've noticed another very small problem. When I use \lstset{showspaces=true, showtabs=true}, all the tabulations become spaces. That's not really a problem. –  projetmbc Apr 9 '12 at 14:15
    
@projetmbc: Again: You can't use verbatim inside other macros, this includes \footnote. You could try a \cprotect\footnote (cprotect package) which is written to allow verbatim material in arguments of other macros. –  Martin Scharrer Apr 9 '12 at 14:43
    
Also: never use tabs! Especially not in inline code. But if you want to display spaces or tabs try \Collectverb* instead of \Collectverb. As stated in the answer, \lstinline can't change the catcodes of the characters, so some option might not work fully. –  Martin Scharrer Apr 9 '12 at 14:43
show 1 more comment

Instead of redefining the internal of \verb you should define your own command.

In the following example I changed the implementation and use the benefits of the packages xparse and newverbs

EDIT: Make the example more efficient -- Thanks @MartinScharrer

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{xparse}

\usepackage[svgnames,x11names]{xcolor}
\usepackage{tikz}
\usepackage{listings}
\usepackage{newverbs}[2011/07/23]

\NewDocumentCommand \MyFrameVerb {m m m}
 {%
    \begin{tikzpicture}
      \node[rectangle,fill=#2,line width = 0.9\baselineskip,font=\ttfamily,outer sep=0pt](tempa){#3};
      \draw[#1] (tempa.south west) rectangle (tempa.north east);
    \end{tikzpicture}%
 }%
\NewDocumentCommand \myverb { O{gray} O{gray!25} }
 {%
  \collectverb{\MyFrameVerb{#1}{#2}}%
 }
\begin{document}

\myverb+NORMAL+

\myverb+NORMAL+

\myverb[blue]+BLUE+

\myverb[green][DarkViolet]+GREEN ON DARK VIOLET+ 

\myverb[white][cyan]+WHITE ON CYAN+    
\end{document}

enter image description here

share|improve this answer
    
How can I use th lstinline in your code. I would like to have the possibility to put one background color and at the same time use the highlights made by listings. –  projetmbc Apr 7 '12 at 14:04
add comment

Actually, it is not true that your code allows to get just one background color for the whole document. Indeed, with this MWE:

% 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}

\verb+NORMAL+

\verb[blue]+BLUE+

\verb[green][DarkViolet]+GREEN ON DARK VIOLET+ % on this page there is the problem

\verb[white][cyan]+WHITE ON CYAN+    
\newpage
% here the background color is correct
\verb[white][red]+WHITE ON RED+
\newpage
% here again
\verb[green][DarkViolet]+GREEN ON DARK VIOLET+

\end{document}

allows you to get 3 pages with 3 different background colors.

share|improve this answer
    
You're right, I've updated my question. –  projetmbc Apr 7 '12 at 8:53
add comment

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.