1

I'm using an editor that breaks lines after a line length of 80 characters. Whenever I use a long lstinline at the end of a line and the editor continues in a new line I get the error message Package Listings: lstinline ended by EOL.

Consider following example:

\documentclass{article}
\usepackage{listings}

\lstset{
    breaklines=true,
    breakatwhitespace=true,
    language=c,
    basicstyle=\small\ttfamily,
    numbers=left,
    numberstyle=\tiny,
    frame=tb,
    keepspaces=true,
    columns=fullflexible,
    showstringspaces=false,
}
\begin{document}
This is a text with a long function in \lstinline|void some_func(int
some_argument)|, where the editor automatically adds a second line.
\end{document}

The editor automatically adds a new line after void some_func(int which results in the error message. Is there a way to span the lstlisting command over multiple lines?

  • Why don't you just put the \lstinline into a line by itself? With certain exceptions (like verbatim and \lstinline) LaTeX converts newlines into spaces (and ignores extra spaces). – John Kormylo Feb 5 at 15:37
  • @JohnKormylo It's possible that a function name has more than 80 characters, so the editor would break lines anyways – Mike van Dyke Feb 6 at 9:20
1

I couldn't find a key-value interface for this in listings.sty, so here's here's a hard coded method. You have to change the macro \lst@InlineM:

\def\lst@InlineM#1{\gdef\lst@inlinechars{%
    \lst@Def{`#1}{\lst@DeInit\egroup\global\let\lst@inlinechars\@empty}%
    \lst@Def{13}{\lst@DeInit\egroup \global\let\lst@inlinechars\@empty
        \PackageError{Listings}{lstinline ended by EOL}\@ehc}}% <--- Error message
    \lst@inlinechars}

to something that doesn't produce an error. The default behaviour is to end the \lstinline (with \lst@DeInit\egroup) then throw an error. To make the new line behave as a space you can do:

\def\lst@InlineM#1{\gdef\lst@inlinechars{%
    \lst@Def{`#1}{\lst@DeInit\egroup\global\let\lst@inlinechars\@empty}%
    \lst@Def{13}{\lst@ProcessSpace}}% <---
    \lst@inlinechars}

enter image description here

MWE:

\documentclass{article}
\usepackage{listings}

\lstset{
    breaklines=true,
    breakatwhitespace=true,
    language=c,
    basicstyle=\small\ttfamily,
    numbers=left,
    numberstyle=\tiny,
    frame=tb,
    keepspaces=true,
    columns=fullflexible,
    showstringspaces=false,
}

\makeatletter
\def\lst@InlineM#1{\gdef\lst@inlinechars{%
    \lst@Def{`#1}{\lst@DeInit\egroup\global\let\lst@inlinechars\@empty}%
    \lst@Def{13}{\lst@ProcessSpace}}%
    \lst@inlinechars}
\makeatother

\begin{document}
This is a text with a long function in \lstinline|void some_func(int
some_argument)|, where the editor automatically adds a second line.
\end{document}
  • 1
    Imho it is rather dangerous: If you forget the closing delimiter in the \lstinline it will eat up your document and you can get rather bewildering errors. I would change the input or the editor. – Ulrike Fischer Feb 5 at 12:04
  • @UlrikeFischer I completely agree. However many editors have a reasonable syntax highlighting which will make the mistake obvious. But if someone writes \makeatletter they should be willing to pay the price ;) – Phelype Oleinik Feb 5 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.