列出背景颜色溢出虚线上的边距

列出背景颜色溢出虚线上的边距

我正在使用代码列表,斑马线效果和断线作为基础。但是,当行通过空格或制表符缩进时,列表的边距将不受尊重,并且背景颜色会溢出。

我尝试了多种修改方法,linebreakbgrd例如使用浮动框、更改postbreakprebreak,甚至尝试linegoal使用包而不是使用,\linewidth但似乎都不起作用。这是该问题的一个最小工作示例:

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

这是一个屏幕截图,如您所见,line 3由于缩进,确实溢出了列表边距:

背景溢出列表边距

答案1

供参考。如果您想要排版 Python 代码,则包piton及其环境{Piton}具有解决该问题的功能。

\documentclass[10pt,a4paper]{article}
\usepackage{xcolor}
\usepackage{piton}

\begin{document}

\PitonOptions
  { 
    break-lines-in-Piton,
    indent-broken-lines,
    background-color = {gray!10,white},
    line-numbers,
    continuation-symbol = {}
  }


\begin{Piton}
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{Piton}

\end{document}

上述代码的输出

答案2

这是临时解决方案。添加breakautoindent=false,确实会阻止行自动缩进,但\linewidth仍然有效。

相关内容