6

Why is spacing before and after vertical bar inconsistent in this listing?

\documentclass{article}

\usepackage{listings}
\lstset{
    morestring=[b]',
    basicstyle=\ttfamily
}

\begin{document}
\lstinline{'foo' | 'bar'}
\end{document}

enter image description here

I'm using pdflatex, if it's relevant.

5

Add keepspaces:

\documentclass{article}
\usepackage{listings}

\begin{document}
\lstset{
    basicstyle=\ttfamily,
}
\lstinline{'foo' | 'bar'}

\lstset{
    morestring=[b]',
    basicstyle=\ttfamily,
}
\lstinline{'foo' | 'bar'}

\lstset{
    morestring=[b]',
    basicstyle=\ttfamily,
    keepspaces,
}
\lstinline{'foo' | 'bar'}

\end{document}

enter image description here

| improve this answer | |
  • Can you explain why this is happening, and why only after delimiters? The documentation is a bit vague about what is happening if keepspaces=false. – siracusa Jul 8 '19 at 10:39
  • @siracusa I guess it's a byproduct of how listings looks for strings, so it swallows spaces as it goes. – egreg Jul 8 '19 at 10:44
  • Thanks! Not sure what the documentation means when it comes to this option... "keepspaces=true tells the package not to drop spaces to fix column alignment and always converts tabulators to spaces." – effeffe Jul 8 '19 at 12:25
  • 2
    @effeffe My approach to listings is “try some of the enchantments, one of them will work”. – egreg Jul 8 '19 at 12:26

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.