TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm writing a computer science paper with LaTeX and have a lot of inline code. Now, when this code is followed by a comma and is at the end of line, the line break is inserted before the comma, which is wrong and looks weird.

Here's an example:

This is normal text with some inlined code, more inlined code
, and even more inlined code.

Basically, the corresponding LaTeX code is (although I'm having problems reproducing the issue in this MWE):

This is normal text with some \lstinline{inlined code}, 
\lstinline{more inlined code}, and \lstinline{even more inlined code}.

How can I avoid the line break between the code and the comma? I found that putting \mbox around the comma and \lstinline macro works, but this feels not right. Are there better solutions?

If the problem is due to settings in my universities .sty or .cls files, is there a way I could quickly find the offending option? … That is, without commenting most of it and then re-introduce every option one by one.


Finally managed to whip up a short working example:



Bacon ipsum dolor sit amet ball tip shankle i Nrtdy uses \lstinline{System.LongerCode}, whereas Uiae uses \lstinline{System.OtherCode}.

Removing breaklines=true seems like to avoid the problem. This in unfortunate, since I need automatic line breaks in listings in other places. Is there a sensible fix?

share|improve this question
Can you cook up a small example such that people can demonstrate solutions on that example that would suit your needs? – percusse Jul 26 '12 at 9:06
@percusse: It's difficult to get the line breaks at the correct spot. There's the thesis template I have to use, and I'm having problems getting the line break to show up in an MWE :( So maybe it is some weird setting in the template … – knittl Jul 26 '12 at 9:42
If I typeset your example with a narrow measure, the comma is never detached from the \lstinline code. I'm afraid it depends on the .cls or .sty files you're using. – egreg Jul 26 '12 at 10:38
@egreg: Is there a simple way to find out what part of the cls/sty files is causing this? – knittl Jul 26 '12 at 12:49
Look for something containing \lst – egreg Jul 26 '12 at 12:53
up vote 5 down vote accepted

Add the option breakatwhitespace:


Since text typeset in \ttfamily won't be hyphenated, this will do as you want and it should have no other consequence.

share|improve this answer
I often have long identifiers (without whitespace, but with periods), that need breaking, e.g. System.Collections.Generic.SortedDictionary – knittl Jul 26 '12 at 14:50
It's possible to use \lstset inside curly braces and set a property only inside it. Using {\lstset{breakatwhitespace} ... paragraph with text and code ...} works without messing up the rest of the document. Thanks! – knittl Jul 29 '12 at 13:29

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.