lstinline 的不同背景颜色

lstinline 的不同背景颜色

在下面的代码中,我决定使用内联列表中的彩色背景从而重新定义\verb对文本和逐字文本的背景使用颜色的可能性。

我遇到的问题是页面只能使用一种背景颜色,而我想在同一页面上使用不同的背景颜色。

这肯定是使用该风格的结果:

\tikzstyle{highlighter} = [%
    \bck@color, %
    line width = 0.9\baselineskip%
]

以下代码需要做哪些更改?

另一个问题:是否可以制定具有可自定义颜色的边框规则?

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

答案1

如果您只是想要一些彩色背景和/或框架,您可以使用from 来\lstinline实现(或者如果您不需要框架)。无需 TikZ 和技巧。这里的一个问题是将逐字材料放在宏内,这通常是不允许的。\fcolorboxxcolor\colorboxatbegshi

您可以使用包\Collectverb中的newverbs(不要与混淆\collectverb)以适合传递给内部宏的形式收集逐字材料\lstinline。这仍然不允许\lstinline更改任何 catcode,但大多数字符应该已经具有所需的 catcode。

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

结果

答案2

您不应该重新定义内部,而\verb应该定义自己的命令。

在下面的例子中,我改变了实现方式,并利用了包的优点xparsenewverbs

编辑:使示例更高效——感谢@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}

在此处输入图片描述

答案3

实际上,您的代码不允许为整个文档获取一种背景颜色。事实上,使用此 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}

允许您获得具有 3 种不同背景颜色的 3 个页面。

相关内容