“listings”包目前给我带来了两个问题。
生成的列表太宽。它超出了
\textwidth
。遗憾的是,我发现文档列表中没有宽度设置选项。不幸的\resizebox
是,最小示例中的注释会导致一些编译错误。我认为如果行号延伸到左边缘就没问题了,但框应该真的和一样宽,\textwidth
而不是突出在边缘。列表的角已按要求做成圆角,但(在 PDF 查看器和纸质打印件中放大时)角上会出现灰色(只要角不是圆的)。
这是我的最小示例:
\documentclass{scrreprt}
\usepackage{listings}
\usepackage{graphicx}
\usepackage{color}
\definecolor{listgray}{rgb}{0.88,0.88,0.88}
\begin{document}
%lstlisting Einstellungen
\lstset{
language=Matlab,
tabsize=2,
showspaces=false,
showstringspaces=false,
backgroundcolor=\color{listgray},
float=[htb],
captionpos=b,
basicstyle=\footnotesize,
frame=tbrl, %t: top, r, b, l
frameround=tttt,
numbers=left,
numberstyle=\tiny,
numberblanklines=false,
}
\rule{\textwidth}{1mm}
%\resizebox{\textwidth}{!}
{
\begin{lstlisting}[float,caption={Beschriftung},label=fig:matlab]
Var = A ./ B;
A = Var .* B;
\end{lstlisting}
}
\end{document}
答案1
我现在还没有读到说明,如果你可以改变这一点,因为对于你想要的,有一个简单的解决方案:
\documentclass{scrreprt}
\usepackage{listings}
\usepackage{graphicx}
\usepackage{color}
\definecolor{listgray}{rgb}{0.88,0.88,0.88}
\begin{document}
% lstlisting Einstellungen
\lstset{
language=Matlab,
tabsize=2,
showspaces=false,
showstringspaces=false,
backgroundcolor=\color{listgray},
float=[htb],
captionpos=b,
basicstyle=\footnotesize,
frame=tbrl, %t: top, r, b, l
frameround=tttt,
numbers=left,
numberstyle=\tiny,
numberblanklines=false,
}
%%%%%%%%
% Linie um die Textbreite darzustellen
\noindent\rule{\textwidth}{1mm}
\begin{figure}
\hspace*{\dimexpr\fboxsep+\fboxrule}%
\begin{minipage}{\dimexpr\textwidth-2\fboxsep-2\fboxrule}
\begin{lstlisting}
Var = A ./ B;
A = Var .* B;
\end{lstlisting}
\end{minipage}
\caption{Beschriftung}\label{fig:matlab}
\end{figure}
\end{document}
或与数字对齐
\documentclass{scrreprt}
\usepackage{listings}
\usepackage{graphicx}
\usepackage{color}
\definecolor{listgray}{rgb}{0.88,0.88,0.88}
\begin{document}
% lstlisting Einstellungen
\lstset{
language=Matlab,
tabsize=2,
showspaces=false,
showstringspaces=false,
backgroundcolor=\color{listgray},
float=[htb],
captionpos=b,
basicstyle=\footnotesize,
frame=tbrl, %t: top, r, b, l
frameround=tttt,
numbers=left,
numberstyle=\tiny,
numberblanklines=false,
}
%%%%%%%%
% Linie um die Textbreite darzustellen
\noindent\rule{\textwidth}{1mm}
\begin{figure}
\hspace*{\dimexpr\fboxsep+\fboxrule+2em}%
\begin{minipage}{\dimexpr\textwidth-2\fboxsep-2\fboxrule-2em}
\begin{lstlisting}
Var = A ./ B;
A = Var .* B;
\end{lstlisting}
\end{minipage}
\caption{Beschriftung}\label{fig:matlab}
\end{figure}
\end{document}
```
答案2
linewidth
并将xleftmargin
调整列表的宽度和位置
\documentclass{scrreprt}
\usepackage{listings}
\usepackage{graphicx}
\usepackage{color}
\definecolor{listgray}{rgb}{0.88,0.88,0.88}
\usepackage[showframe]{geometry}
\begin{document}
%lstlisting Einstellungen
\lstset{
language=Matlab,
tabsize=2,
showspaces=false,
showstringspaces=false,
backgroundcolor=\color{listgray},
float=[htb],
captionpos=b,
basicstyle=\footnotesize,
frame=tbrl, %t: top, r, b, l
frameround=tttt,
numbers=left,
numberstyle=\tiny,
numberblanklines=false,
linewidth=.99\textwidth,
xleftmargin=0.15cm
}
\noindent\rule{\textwidth}{1mm}
\begin{lstlisting}[float,caption={Beschriftung},label=fig:matlab]
Var = A ./ B;
A = Var .* B;
\end{lstlisting}
\end{document}