13

I am using the listings package to represent verbatim font with breaklines set to true so that line breaks are produced automatically. I have set \emergencystretch to an abnormally large value since it seems the best way to produce (relatively) good looking text without manually editing the formatting all over the place.

However, when the line is broken immediately after a \lstinline (shorthand form of = used in the example below), a space is placed at the start of the following line. How to I remove this space automatically?

\documentclass[10pt,a5paper]{book}
\usepackage{listings}
\lstset{breaklines=true,basicstyle=\ttfamily}
\lstMakeShortInline=
\setlength{\emergencystretch}{35pt}

\begin{document}

Blah blah blah, standard text to highlight that usually the line breaks are normal and no space follows them. The actual issue is highlighted in the next paragraph.

OK. My name is Eddie. This is my first question. =How quick= will it be answered? Only time will tell. Seriously though, it would be nice to get an answer within about 3 and a half minutes.

\end{document}

Produces: (check the space between "quick" and "will" - end of first line, second paragraph.

enter image description here

My initial thought was to specify \lstset{...postbreak=\ignorespaces} but this doesn't work since the linebreak occurs after the \lstinline.

Perhaps it is possible to use a \ifthenelse statement but it would require me to have a switch which lets me know if a linebreak is produced following a \lstinline command (could be hassle since this will only be determined after typesetting).

Any ideas would be massively appreciated.

  • 1
    Sorry David, I have done what you asked (I did have to copy and paste each line individually though which seemed unnecessary). The book you had input in by way (The Latex Companion) is incredibly useful. so thanks. – Eddie Apr 16 '18 at 20:36
  • 1
    you can highlight the whole text block then use {} or ctrl-k key and it will show the block as code. Actually it's a very good question, nice example and answer isn't exactly obvious:-) – David Carlisle Apr 16 '18 at 20:43
9

listings has problems if the end of a breakable inline listing is at the end of the line. See e.g. also listings package introduces unnecessary linebreaks.

You can try breakatwhitespace:

\documentclass[10pt,a5paper]{book}
\usepackage{listings}
\lstset{breaklines=true,breakatwhitespace,
        basicstyle=\ttfamily,alsoletter=\\\{\}\*\[\]\-}
\lstMakeShortInline=
%\setlength{\emergencystretch}{35pt}

\begin{document}

Blah blah blah, standard text to highlight that usually the line breaks are normal and no space follows them. The actual issue is highlighted in the next paragraph.

OK. My name is Eddie. This is my first question. =How quick= will it be answered? Only time will tell. Seriously though, it would be nice to get an answer within about 3 and a half minutes.

\end{document}

Or load microtype

\documentclass[10pt,a5paper]{book}
\usepackage{listings,microtype}
\lstset{breaklines=true,
        basicstyle=\ttfamily,alsoletter=\\\{\}\*\[\]\-}
\lstMakeShortInline=
%\setlength{\emergencystretch}{35pt}

\begin{document}

Blah blah blah, standard text to highlight that usually the line breaks are normal and no space follows them. The actual issue is highlighted in the next paragraph.

OK. My name is Eddie. This is my first question. =How quick= will it be answered? Only time will tell. Seriously though, it would be nice to get an answer within about 3 and a half minutes.

\end{document}

But none will imho handle every case, so you will have to check visually if everything is okay.

  • Amazingly, breakatwhitespace solves the problem for all cases in my document. I'm not sure why it works but it does. Thanks and thanks to anyone else who commented. – Eddie Apr 16 '18 at 21:35
7

As a workaround if you have a document you just need to get out then you can do

enter image description here

\documentclass[10pt,a5paper]{book}
\usepackage{listings}
\lstset{breaklines=true,basicstyle=\ttfamily,alsoletter=\\\{\}\*\[\]\-}
\lstMakeShortInline=
\setlength{\emergencystretch}{35pt}

%\showoutput
\begin{document}

Blah blah blah, standard text to highlight that usually the line breaks are normal and no space follows them. The actual issue is highlighted in the next paragraph.

OK. My name is Eddie. This is my first question. =How quick=\linebreak[2] will it be answered? Only time will tell. Seriously though, it would be nice to get an answer within about 3 and a half minutes.

\end{document}

but I'm missing something at present I don't see why the space isn't dropped if for example you use \linebreak[0] I may add to this answer later. It's not related to the short = processing, you get the same from \lstinline|How quick|

Ah listings is adding a break point at the end of the text (which explains the behaviour at least) if you know that you don't want that inline listing ti break you could use \leavevmode\hbox{=How quick#} rather than using the \linebreak as above but if you do want to allow it to break but not at the end, it seems some re-arrangement of the listings internals would be required.

  • Thanks for all the help David, your solutions are useful but would require an awful lot of user input from me (think about the amount of verbatim environments you used in The Latex Companion and maybe you will understand). It appears that the simplest solution for my case is to use breakatwhitespace. I am unsure why it works so I am going to look into the listings.sty file to find out. Thanks a lot for all of your help. P.s. in all seriousness I have a copy of the book on the desk as I am writing this. It is a work of art and I hope it will help me to produce similar levels of quality. – Eddie Apr 16 '18 at 21:43
  • 1
    @Eddie breakatwhitespace works because it only allows breaking at space, in particular it doesn't allow a break at the end of the listing, the fact that listings does allow a break there is a bug really but it's tricky code – David Carlisle Apr 16 '18 at 21:49
6

I suggest using minted instead (requires to run LaTeX with the -shell-escape option and to install pygmentize):

\documentclass[10pt,a5paper]{book}
\usepackage{minted}
\usemintedstyle{bw}

\begin{document}

Blah blah blah, standard text to highlight that usually
the line breaks are normal and no space follows them.
The actual issue is highlighted in the next paragraph.

OK. My name is Eddie. This is my first
question. \mintinline{tex}=How quick= will it be answered?
Only time will tell. Seriously though,
it would be nice to get an answer within
about 3 and a half minutes.

\end{document}

enter image description here

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.