2

The following picture shows my problem:

enter image description here

How can I tell \lstinline to break lines at specific C++ operators? For example: FooFactory::instance()->createFoo() should be broken as follows:

... text   FooFactory::
instance()->createFoo()

or

FooFactory::instance()->
createFoo() text text ...

MWE:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[left=6cm,right=7cm]{geometry}
\usepackage{parskip}
\usepackage{showframe}
\usepackage{listings}

\lstset{
    language=C++,
    basicstyle=\ttfamily,
    breaklines=true,
    breakatwhitespace=true,
    inputencoding=utf8,
    extendedchars=true
}
\lstset{literate=% I dont know what I am doing...
    {::}{::}{1\discretionary{}{}{}} % line-break at ::
    {->}{->}{1\discretionary{}{}{}} % line-break at ->
}

\begin{document}

text text text text text text text text \lstinline{FooFactory::instance()->createFoo()} text text
text text text text 

text text text text text text \lstinline{FooFactory::instance()->createFoo()} text text
text text text text 

\end{document}
0

1 Answer 1

2

I don't understand why this works either. More precisely, I don't know why you need to put the \discretionary after the number instead of inside the replacement text.

\documentclass{article}
%\usepackage[utf8]{inputenc}% all it does for me is produce warnings
\usepackage[left=6cm,right=7cm]{geometry}
\usepackage{parskip}
\usepackage{showframe}
\usepackage{listings}

\lstset{
    language=C++,
    basicstyle=\ttfamily,
    breaklines=true,
    breakatwhitespace=true,
    inputencoding=utf8,
    extendedchars=true
    }
\lstset{literate={::}{}{0\discretionary{::}{}{::}}% line-break at ::
   {->}{}{0\discretionary{->}{}{->}}% line-break at ->
}
\begin{document}

text text text text text text text text  \lstinline{FooFactory::instance()->createFoo()} text text text text text text 

text text text text text text \lstinline{FooFactory::instance()->createFoo()} text text text text text text 

\begin{lstlisting}
    FooFactory::instance()->createFoo();
\end{lstlisting}

\end{document}

listings

6
  • Many thanks, that works perfectly for \lstinline, however it breaks the lstlisting environment. I have updated my question.
    – sergej
    Aug 2, 2015 at 9:26
  • With indentation, there just isn't room for the whole line. Aug 2, 2015 at 13:50
  • Many Thanks! The 0 in front of \discretionary did the trick. Right?
    – sergej
    Aug 2, 2015 at 14:04
  • It also got rid of the extra space being added. Aug 2, 2015 at 14:09
  • What is formfeed for? And why is there no comma as separator between the options?
    – sergej
    Aug 2, 2015 at 14:10

You must log in to answer this question.

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