我正在写一本关于 Awk 编程语言的书,并且我已通过 启用了单个框架\lstset{frame=single}
。但是,我希望代码片段框内的代码离框架边框更远。有什么想法可以更改代码片段框内代码的填充吗?在 HTML 中我只需执行padding: 5px
,但我不知道如何在 LaTeX 中执行此操作。
答案1
有一些选项可以直接基于环境lstlisting
,或者您可以定义自己的列表样式。
\documentclass{book}
\usepackage{listings}
\lstset{frame=single}
% Define your custom style
\lstdefinestyle{mystyle}{
framexleftmargin=5mm, % Adjust left margin
framextopmargin=3pt, % Adjust top margin
}
\begin{document}
Below is a lstlisting box with the default padding.
\begin{lstlisting}
A box using the default padding
\end{lstlisting}
Below is a lstlisting box that uses the style with additional padding as defined above.
\begin{lstlisting}[style=mystyle]
A box using a new padding
\end{lstlisting}
And here's an example when the additional spacing is added not defining your own style. And the box has been pushed back to its original position, keeping the left-margin inside the box the same as above.
\begin{lstlisting}[framexleftmargin=5mm, framextopmargin=3pt, xleftmargin=5mm]
A box using a new padding with a lot of code so we can see how the box handles line breaks
\end{lstlisting}
\end{document}