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}.
\begin{lstlisting}[language=TeX,breaklines]
...
\end{lstlisting}
works, depending however on the language used. For instance, with TeX as language.
Another solution, similar to the one proposed by projetmbc is
\makeatletter
{\obeylines\gdef\bt@eol{^^M}}
\newenvironment{breakabletexttt}
{\ttfamily\hfuzz=0.4em
\list{}{\leftmargin=2em
\itemindent=-\leftmargin
\listparindent=-\leftmargin
\parsep=0pt}
\item\relax\obeylines\breakable@texttt}
{\endlist}
\def\breakable@texttt#1{%
\ifx#1\end
\expandafter\end
\else
\expandafter\ifx\bt@eol#1%
#1%
\else
\string#1\hskip1sp
\fi
\expandafter\breakable@texttt
\fi}
\makeatother
Then
\begin{breakabletexttt}
<long line 1>
<long line 2>
...
\end{breakabletexttt}
will print the lines breaking them when at the right margin. The \hfuzz=0.4em
allows at most one character to stick out (the line width should be made an integer multiple of the monospaced font characters, or a flexible space should be added between characters instead of \hskip1sp
).
EDIT: the following variant will respect spaces
\makeatletter
{\obeylines\gdef\bt@eol{^^M}}
\newenvironment{breakabletexttt}
{\ttfamily\hfuzz=0.4em
\list{}{\leftmargin=2em
\itemindent=-\leftmargin
\listparindent=-\leftmargin
\parsep=0pt}
\item\relax\obeylines\obeyspaces\expandafter\breakable@texttt\@gobble}
{\endlist}
\def\breakable@texttt{\futurelet\@let@token\breakable@texttti}
\def\breakable@texttti#1{%
\ifx\@let@token\end
\expandafter\end
\else
\expandafter\ifx\bt@eol\@let@token
\par
\else
\string#1\hskip1sp
\fi
\expandafter\breakable@texttt
\fi}
\makeatother
It's important to code as
\begin{breakabletexttt}
line
...
\end{breakabletexttt}
with a new line after the \begin
.
Best Answer
The following MWE provides
\lstInline[<options>]{<code snippet>}
as a complement to\lstinline
that tests whether the in-line code will fit within the remainder of the line, otherwise it issues a line break.The driver behind the line break conditioning comes from the
linegoal
package the relies onzref
'ssavepos
module.\lstinline
is first set within a box\mylisting
inside\lstInline
, to establish its width. If it exceeds\linegoal
(the remaining space available on the line), a line break in the form\hfill\\
is issued, after which\lstinline
is called again.Resetting
\lstinline
(rather than just using the boxed value\usebox{\mylisting}
) allows for the code segment to wrap around multiple lines and also properly stretch/shrink, if needed, within the regular text.Any changes in the start position of the code segment will require a recompile to properly work - a requirement when working with the
\label
-\ref
system provided bylinegoal
.Note that
\lstInline
uses\lstinline!...!
with!
as the character delimiter. As such there will be problems when actually using!
in the inline listing. If this is a problem, just use an alternate character inside the definition.