# adjusting listing width

I have to include a piece of code in a report and I have an overflow problem because of two things: the comments and the fact that the instruction lines are too wide (which is my main problem).

I can always delete the comments so I don't exactly mind but I cannot reduce the width of my instructions (especially considering there are various indentation levels). So I was wondering if it is possible to adjust the width of the listing.

MWE with overflow:

\documentclass[a4paper,11pt]{article}
\usepackage{listings}

\begin{document}
\begin{lstlisting}
This is a long instruction that I use as example and I hope it is long enough
\end{lstlisting}
\end{document}

• How do you want to adjust the width? Ignore all margins? Why don't you simply use line breaking (listings can do it for you). – TeXnician Sep 18 '18 at 12:23
• I wanted to do a kind of "zoom out" (like changing the scale of tikzpictures). Line breaking indeed gives me a good result, it didn't occur to me. Thank for telling me. – Parker Sep 18 '18 at 12:36

You can use

\lstset{
breaklines=true,                    % sets automatic line breaking
%   breakatwhitespace=false,            % sets if automatic breaks should only happen at whitespace
prebreak = \raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}} % just as an example
}


The prebreak option puts an arrow at the end of the line to indicate the automatic line break.

Full MWE:

\documentclass[a4paper,11pt]{article}
\usepackage{listings}
\lstset{
breaklines=true,                    % sets automatic line breaking
%   breakatwhitespace=false,            % sets if automatic breaks should only happen at whitespace
prebreak = \raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}} % just as an example
}

\begin{document}
\begin{lstlisting}
This is a long instruction that I use as example and I hope it is long enough
\end{lstlisting}
\end{document}


There are many more options for other things to adjust, check the documentation. Also, you can use, as mentioned by TeXnician, manual line breaks.

edit: If you want smaller font sizes, you could use, e.g.,

basicstyle=\scriptsize\ttfamily


as option for \lstset, then the result would look like