在使用列表创建斑马效果,我找到了一种解决方案,可以让列表产生斑马线效果。不幸的是,这只有在线条没有断线的情况下才有效。有办法解决这个问题吗?
下面是一个带有示例的代码来说明该问题:
\documentclass[10pt,a4paper]{article}
\usepackage[utf8x]{inputenc}
\usepackage{ucs}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[frenchb]{babel}
\usepackage[x11names, svgnames]{xcolor}
\usepackage{graphicx}
\usepackage{listings}
\usepackage{amsfonts}
\usepackage{amssymb}
\makeatletter
% Patch line number key to call line background macro
\lst@Key{numbers}{none}{%
\def\lst@PlaceNumber{\lst@linebgrd}%
\lstKV@SwitchCases{#1}%
{none&\\%
left&\def\lst@PlaceNumber{\llap{\normalfont
\lst@numberstyle{\thelstnumber}\kern\lst@numbersep}\lst@linebgrd}\\%
right&\def\lst@PlaceNumber{\rlap{\normalfont
\kern\linewidth \kern\lst@numbersep
\lst@numberstyle{\thelstnumber}}\lst@linebgrd}%
}{\PackageError{Listings}{Numbers #1 unknown}\@ehc}}
% New keys
\lst@Key{linebackgroundcolor}{}{%
\def\lst@linebgrdcolor{#1}%
}
\lst@Key{linebackgroundsep}{0pt}{%
\def\lst@linebgrdsep{#1}%
}
\lst@Key{linebackgroundwidth}{\linewidth}{%
\def\lst@linebgrdwidth{#1}%
}
\lst@Key{linebackgroundheight}{\ht\strutbox}{%
\def\lst@linebgrdheight{#1}%
}
\lst@Key{linebackgrounddepth}{\dp\strutbox}{%
\def\lst@linebgrddepth{#1}%
}
\lst@Key{linebackgroundcmd}{\color@block}{%
\def\lst@linebgrdcmd{#1}%
}
% Line Background macro
\newcommand{\lst@linebgrd}{%
\ifx\lst@linebgrdcolor\empty\else
\rlap{%
\lst@basicstyle
\color{-.}% By default use the opposite (`-`) of the current color (`.`) as background
\lst@linebgrdcolor{%
\kern-\dimexpr\lst@linebgrdsep\relax%
\lst@linebgrdcmd{\lst@linebgrdwidth}{\lst@linebgrdheight}{\lst@linebgrddepth}%
}%
}%
\fi
}
\makeatother
\lstset{
% * Number for lines
numbers=left, % Where to put the line-numbers
numberstyle=\footnotesize, % Size of the fonts used for the line-numbers
stepnumber=1, % Step between two numbers
numbersep=5pt, % How far the line-numbers are from the code
%
% * Back returns for long lines
breaklines=true, % Sets automatic line breaking
breakatwhitespace=false, % Automatic breaks only happen at whitespace ?
breakindent=0pt, % Space before the text of a break
postbreak=\mbox{ % Character used at the begining of a break
\rotatebox[y=0.9ex]{180}{\color{Red} $\Lsh$}%
},
}
\begin{document}
\begin{lstlisting}[language=Python,linebackgroundcolor={\ifodd\value{lstnumber}\color{gray}\fi}]
for i in range(5): for i in range(5): for i in range(5): for i in range(5): for i in range(5): for i in range(5):
sum += i**3
print sum
print('Fin de la boucle...')
\end{lstlisting}
\end{document}
答案1
您可以添加第二个彩色条作为postbreak
代码的一部分,如下所示。但是,我无法让\color{-.}
命令在那里工作,所以我使用了\color{white}
在这个特定示例中工作正常的命令,但在一般情况下应该用变量替换。
\documentclass[10pt,a4paper]{article}
\usepackage[utf8x]{inputenc}
\usepackage{ucs}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[frenchb]{babel}
\usepackage[x11names, svgnames]{xcolor}
\usepackage{graphicx}
\usepackage{listings}
\usepackage{amsfonts}
\usepackage{amssymb}
\makeatletter
% Patch line number key to call line background macro
\lst@Key{numbers}{none}{%
\def\lst@PlaceNumber{\lst@linebgrd}%
\lstKV@SwitchCases{#1}%
{none&\\%
left&\def\lst@PlaceNumber{\llap{\normalfont
\lst@numberstyle{\thelstnumber}\kern\lst@numbersep}\lst@linebgrd}\\%
right&\def\lst@PlaceNumber{\rlap{\normalfont
\kern\linewidth \kern\lst@numbersep
\lst@numberstyle{\thelstnumber}}\lst@linebgrd}%
}{\PackageError{Listings}{Numbers #1 unknown}\@ehc}}
% New keys
\lst@Key{linebackgroundcolor}{}{%
\def\lst@linebgrdcolor{#1}%
}
\lst@Key{linebackgroundsep}{0pt}{%
\def\lst@linebgrdsep{#1}%
}
\lst@Key{linebackgroundwidth}{\linewidth}{%
\def\lst@linebgrdwidth{#1}%
}
\lst@Key{linebackgroundheight}{\ht\strutbox}{%
\def\lst@linebgrdheight{#1}%
}
\lst@Key{linebackgrounddepth}{\dp\strutbox}{%
\def\lst@linebgrddepth{#1}%
}
\lst@Key{linebackgroundcmd}{\color@block}{%
\def\lst@linebgrdcmd{#1}%
}
% Line Background macro
\newcommand{\lst@linebgrd}{%
\ifx\lst@linebgrdcolor\empty\else
\rlap{%
\lst@basicstyle
\color{-.}% By default use the opposite (`-`) of the current color (`.`) as background
\lst@linebgrdcolor{%
\kern-\dimexpr\lst@linebgrdsep\relax%
\lst@linebgrdcmd{\lst@linebgrdwidth}{\lst@linebgrdheight}{\lst@linebgrddepth}%
}%
}%
\fi
}
\newcommand{\lst@linebreakbgrd}{%
\ifx\lst@linebgrdcolor\empty\else
\rlap{{%
\lst@basicstyle
\color{white}% By default use the opposite (`-`) of the current color (`.`) as background
\lst@linebgrdcolor{%
\lst@linebgrdcmd{\lst@linebgrdwidth}{\lst@linebgrdheight}{\lst@linebgrddepth}%
}%
}}%
\fi
}
\makeatother
\lstset{
% * Number for lines
numbers=left, % Where to put the line-numbers
numberstyle=\footnotesize, % Size of the fonts used for the line-numbers
stepnumber=1, % Step between two numbers
numbersep=5pt, % How far the line-numbers are from the code
%
% * Back returns for long lines
breaklines=true, % Sets automatic line breaking
breakatwhitespace=false, % Automatic breaks only happen at whitespace ?
breakindent=0pt, % Space before the text of a break
postbreak={%
\mbox{% % Character used at the begining of a break
\lst@linebreakbgrd
\rotatebox[y=0.9ex]{180}{\color{Red} $\Lsh\,$}%
}},
}
\begin{document}
\begin{lstlisting}[language=Python,linebackgroundcolor={\ifodd\value{lstnumber}\color{gray}\fi}]
for i in range(5): for i in range(5): for i in range(5): for i in range(5): for i in range(5): for i in range(5): for i in range(5): for i in range(5): for i in range(5): for i in range(5): for i in range(5): for i in range(5):
for i in range(5): for i in range(5): for i in range(5): for i in range(5): for i in range(5): for i in range(5):
for i in range(5): for i in range(5): for i in range(5): for i in range(5): for i in range(5): for i in range(5):
for i in range(5): for i in range(5): for i in range(5): for i in range(5): for i in range(5): for i in range(5):
sum += i**3
print sum
print('Fin de la boucle...')
\end{lstlisting}
\end{document}