Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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):

\documentclass{minimal}
\usepackage{listings}
\begin{document}
This is normal text with some \lstinline{inlined code}, 
\lstinline{more inlined code}, and \lstinline{even more inlined code}.
\end{document}

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.


edit

Finally managed to whip up a short working example:

\documentclass[11pt]{book}

\usepackage{listings}
\lstset{
basicstyle=\ttfamily\footnotesize,
breaklines=true
}

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

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
2  
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
1  
@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
add comment

1 Answer

up vote 5 down vote accepted

Add the option breakatwhitespace:

\usepackage{listings}
\lstset{
  basicstyle=\ttfamily\footnotesize,
  breaklines,
  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
add comment

Your Answer

 
discard

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.