4

I use the listings package. If a line break inside a multi-word \lstinline{} occurs, I get a nasty spaces at the beginning of the new line:

page left                  page right
    |                          |   
    |normal text normal text IF|
    | FORALL normal text       | 

IF FORALL is inside a \lstinline{}.

How to avoid the space before FORALL?

Thanks!


EDIT: I don't mind to elaborate. I am typing a document that includes some inline code snippets. I use \lstinline{} to typeset them (since I also use lstlisting in my document).

The illustration above depicts the result from latex code:

normal text normal text \lstinline{IF FORALL} normal text

Now if IF FORALL does fit into a single line, I get

    |normal text normal text IF FORALL normal|
    |text                                    | 

on my page, which is fine. The | mark the start of the page margin.

If IF FORALL is broken down into multiple lines, I get the result shown above. I would much rather like to have:

    |normal text normal text IF|
    |FORALL normal text        | 

Hope that this makes it more clear. Thanks.

4

3 Answers 3

3

The space is the one between the IF and the FORALL. I don't know if it is possible to eat it somehow, but you can at least move a bit back:

\documentclass{book}
\usepackage{listings}
\lstset{breaklines,postbreak=\kern-1ex}
\textwidth=4cm
\parindent=0cm
\begin{document}
normal text normal text \lstinline{IF FORALL} normal text
\end{document}

Addition: I just realized, that the space disappears too if you use \lstset{breaklines,breakatwhitespace}

Addition 2: An obvious solution which avoids all this hassle with the white space is to use two \lstinline commands: \lstinline{IF} \lstinline{FORALL}.

3
  • @Ulrike: Your first solution doesn't work: -1ex isn't quite correct, but that's not the main problem. If you increase the text width to 4.3cm, then "IF " fits on the first line, and the second line starts with "FORALL", moved to the left by -1ex. That's definitely not desired. Your second solutions seems to work perfectly, at least with a recent TeX installation! So could you please replace your first solution with the second one? Oct 22, 2010 at 14:13
  • Well no: the second doesn't work perfectly either, as then (with textwidth=4.3cm) the first line is a bit to short (the space behind the IF is there). The perfect solution is postbreak=\kern-1ex when the break is before the space and prebreak=\kern-1ex if the break is after the space. Oct 22, 2010 at 15:18
  • Oh my, you're right, the breakatwhitespace option doesn't solve the problem either. To me this seems like a bug in the package. Anyway, this breakatwhitespace isn't well documented. Still your \kern-1ex isn't perfect, but it's not clear how to make it better: It seems that the width of the white space varies depending on the letters around it. So at the moment, your "Addition 2" seems like the best solution. Oct 22, 2010 at 17:26
1

This doesn't answer your question per se, but what I do is either rewrite the text so that the listing fits on a single line, or if that is not possible for whatever reason, then make it a nonfloating listing. This takes a little extra space, but I think it is more readable than having it break.

0

This is apparently not implemented at this time.

You must log in to answer this question.

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