4

the package "listings" is currently causing me two problems.

  1. The generated listing is too wide. That it stands out beyond \textwidth. I've found in the documentary listings unfortunately no setting options for the width. The \resizebox commented out in the minimal example unfortunately causes some compile errors. I think it would be fine if the line numbering extends into the left edge, but the box should really be as wide as \textwidth and not stick out in the edge.

  2. The listing has rounded corners as desired, but (when zooming in the PDF viewer and on the paper printout) the gray appears at the corners (as far as the corners are not rounded).

Here is my minimal example:

\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
  • But he need the frame ? listings does not set the listing within the frame, but sets the frame around the listing. In other words, the listing has the current allowable width and the frame sticks out beyond that.
    – protter
    Commented Jul 9, 2019 at 12:07

2 Answers 2

2

I have not read now in the instructions, if you can change that, because there is a simple solution for what you want:

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

or with the numbers aligned

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


4

linewidth and xleftmargin will adjust the width and position of the listing

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

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .