11

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?

5
  • 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, 2012 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, 2012 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, 2012 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, 2012 at 12:49
  • Look for something containing \lst
    – egreg
    Jul 26, 2012 at 12:53

1 Answer 1

11

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.

3
  • 1
    I often have long identifiers (without whitespace, but with periods), that need breaking, e.g. System.Collections.Generic.SortedDictionary
    – knittl
    Jul 26, 2012 at 14:50
  • 1
    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, 2012 at 13:29
  • I found with this solution, that longer variable names without spaces (for me underscores), would push out into the right margin. For example, use \lstinline{System.LongerCode.LongerStill} in the minimal example in the question.
    – user4417
    Mar 29, 2018 at 10:17

You must log in to answer this question.

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