2

I am trying to add some code to my document, but whenever there is a space inside a string in the code, it shows up with a bucket(?) thing. I have provided the code and the output below:

\documentclass[a4paper]{article}

\usepackage{listings}
\begin{document}

\section*{Code}
\begin{lstlisting}[language=python]
# comment
a = 24
print (a)
string = "some string"
\end{lstlisting}

\end{document}

space inside string inside lstlisting replaced by this symbol

1
  • You may be looking for the term "visible space character". Here's a discussion
    – Chris H
    Commented Mar 14, 2023 at 16:06

1 Answer 1

7

This is not a bug, it is a feature! With your current settings, spaces inside a string will be visualized using this "bucket", as you call it. This can be especially helpful if you have more than one space.

If you do not want them you can disable them by setting showstringspaces=false, either only for your current listing (option 1) or for the whole document (option 2).

\documentclass[a4paper]{article}

\usepackage{listings}
%\lstset{showstringspaces=false} % option 1

\begin{document}
\section*{Code}
\begin{lstlisting}[
    language=python,
    showstringspaces=false, % option 2
]
# comment
a = 24
print (a)
string = "some string"
\end{lstlisting}
\end{document}

Result

You could even enable this for "normal" spaces, i.e. outside of strings, by setting showspaces=true, if you want to.

3
  • thanks for the clear answer! I have since learned that it is called an underbracket. But if I were to name it, it would be a hottub.
    – mudders
    Commented Mar 22, 2023 at 18:48
  • showstringspaces=false fixes visual side, but then resulting PDF does not contain indentation spaces at all. (can not be used to copy paste..) Is there a middle ground: to not visual string, but include it into PDF output? Commented Jun 13 at 12:53
  • I guess this should be a new question. Feel free to link it here. Some thoughts/ideas: (1) Space characters can not reliably be copied: tex.stackexchange.com/a/195495. (2) Consider the accsupp package, see tex.stackexchange.com/q/714781 for an example. (3) Instead of making the text copyable, you could add a text file to your document: tex.stackexchange.com/a/445795/237192. (4) See stackoverflow.com/q/530121. Commented Jun 14 at 17:00

You must log in to answer this question.

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