How to force line wrapping in listings package?

latex listings
latex listings color
latex source listing
latex listings xleftmargin
latex minted
lstlisting breaklines not working
assembly in latex
json in latex

I have a problem concerning the listings package in latex. I need to embed a source code of the following XML document http://www.sparxsystems.com.au/downloads/profiles/EP_Extensions.xml in my master thesis' appendix. The problem is, no matter what options I pass to the package, I don't seem to be able to fit it on the page. The lines in the listing are too long, and the listings package doesn't wrap them, which seems odd... What combination of options will do the trick?

At the moment I use the following lstset instruction:

\lstset{
breakindent=0em,
language=XML,
basicstyle=\footnotesize,
numbers=left,
numberstyle=\footnotesize,
stepnumber=2,
numbersep=5pt,
backgroundcolor=\color{white},
showspaces=false,
showstringspaces=false,
showtabs=false,
frame=single,
tabsize=2,
captionpos=b,
breaklines=true,
breakatwhitespace=true,
breakautoindent=true,
escapeinside={\%*}{*)},
linewidth=\textwidth
}

Which, with no other options set, and combined with the following command:

\lstinputlisting{EP_Extensions.xml}

Lists the given file's source to the following output:

I just had this problem...

breaklines worked for me, though you do need to set it to true - the default is false:

\lstinputlisting[style=Python,
    caption=My Class,
    label={mine.py},
    breaklines=true,
  ]{../python/mine.py}

is pretty much what I used

How to force line wrapping in listings package?, I just had this problem breaklines worked for me, though you do need to set it to true - the default is false: \lstinputlisting[style=Python  Wrapping text around figures. At time, it might be desirable to wrap text around a float (a figure, in our case) so as not to break the flow of the text. There are a number of packages in LaTeX to accomplish this task, though they requires some amount of manual tuning in most of the cases.

I had this problem and this page ranks highly on google so here was the solution for me:

I had the following inclusion: \usepackage[none]{hyphenat} which causes the issue described above. I was using it to fix the problem of latex automatically breaking lines in the middle of a word, which looks really stupid in some circumstances.

After much searching for an alternative to this solution I found nothing suitable and settled for properly broken lines but badly placed hyphenation.

latex - How to force line wrapping in listings package?, The lines in the listing are too long, and the listings package doesn't wrap them, which seems odd What combination of options will do the trick? At the moment​  How to wrap text in Excel automatically. To force a lengthy text string to appear on multiple lines, select the cell(s) that you want to format, and turn on the Excel text wrap feature by using one of the following methods. Method 1. Go to the Home tab > Alignment group, and click the Wrap Text button: Method 2.

Not a great answer, but one idea is to word wrap the source code before including it. If you're on linux, the command fold can be used for this. Clearly this works best if the text is in a fixed width font, which i would recommend in any case for code listings.

How to force line break in listings package?, breaklines=true, % sets automatic line breaking. breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace. The whitespaces at the beginning and ending of every line (after wrapping but before indenting) is dropped if the value is set to TRUE. initial_indent: It’s default value is set to’ ‘. This method prepends the given string to the first line of wrapped output. subsequent_indent: It’s default value is set to ‘ ‘. This method prepends the given string to all the lines of wrapped output except the first.

A followup to Aaron's answer. I was using basicstyle=\ttfamily for my listings. I was therefore able to permit line breaking by changing

\usepackage[none]{hyphenat}

to

\usepackage[htt]{hyphenat}
%           ^^^

The hyphenat docs describe that the htt option "enables hyphenation of ... text typeset via either \texttt or \ttfamily." In my use case, I didn't see any hyphenation in my listings, but I did get the breaking.

How to force line wrapping in listings package?, The lines in the listing are too long, and the listings package doesn't wrap them, which seems odd What combination of options will do the trick? At the moment​  to learn the parameters which control the work of the listings package. This is, however, the main task. Here are some of them. 1.3 Figure out the appearance Keywords are typeset bold, comments in italic shape, and spaces in strings appear as . You don’t like these settings? Look at this: \lstset{% general command to set parameter(s)

[TeX] Good-looking line breaks with the listings package, If you want to typeset source code in LaTeX you'll probably end up using the listings package. The output looks good and you have quite a few  How to Pack a Parcel Guidelines. Our top priority is getting your parcel delivered safe and sound. That's why we’ve asked the experts and created some packaging Do's and Don’ts to help you send your parcels.

[PDF] The Listings Package, The listings package is a source code printer for LATEX. You can typeset '​showlines' controls these empty lines and is described in section 4.2. Warning: First read Other features There are still features not mentioned so far: automatic break- ing of long Moreover, you can force the styles to be applied cumulatively​. 24  The exception is if you are composing mail in Plain text - while composing, GMail will wrap to the window. But when you send, and at the recipient end, text is split into lines of about 78 characters, the internet standard for plain text line length." improve this answer. edited Nov 1 '12 at 15:16. 69 silver badges. 95 bronze badges.

Code listing, Using the lstlisting environment from the listings package: all LaTeX commands and the text is printed keeping all the line breaks and white spaces typed. An online LaTeX editor that's easy to use. No installation, real-time collaboration, version control, hundreds of LaTeX templates, and more.

Comments
  • A full list of options is available at en.wikibooks.org/wiki/LaTeX/Packages/Listings
  • Thank you! I had the exact same problem, and couldn't figure out why listings wouldn't break my lines even with breaklines=true. You just saved me a lot of head scratching.
  • Nice find! I was able to use the htt option with monospaced listings - answer.