I'm having trouble with showstringspaces
in the listings package, setting the option to true seems to be being completely ignored.
My objective is to highlight the spaces in the strings using \inlinetext
but currently the output looks like the below:
Am I missing something? Why is showstringspaces
not having any effect?
Minimum working example:
\documentclass{article}
% Color
\usepackage{xcolor}
\usepackage{textcomp}
% Code
\usepackage{listings}
\newcommand*{\inlinetext}{\lstinline[language=textwithspaces,showstringspaces=true,style=showspaces]}
\lstdefinestyle{showspaces}{
basicstyle = \footnotesize\ttfamily,
breaklines=true,
upquote=true,
showstringspaces=true
}
\lstdefinelanguage{textwithspaces}{
alsoletter={0,1,2,3,4,5,6,7,8,9,.,/,:},
showstringspaces=true,
style=showspaces
}
\begin{document}
\begin{table}[h]
\begin{tabular}{ll}
Raw Transactor Name & Occurrences \\
\inlinetext$TESCO STORES 5128$ & 87 \\
\inlinetext$TESCO STORES 2977$ & 68 \\
\inlinetext$TESCO_STORES$ & 14 \\
\inlinetext$SACAT MARKS AND$ & 33 \\
\inlinetext$SACAT MARKS AND$ & 16 \\
\inlinetext$WILKINSON $ & 22 \\
\inlinetext$WILKINSON$ & 8
\end{tabular}
\label{table:cleaningstrings}
\end{table}
\end{document}
Best Answer
The reason why no visible-space characters are shown is that
showstringspaces
key only shows visible spaces in string literals, and neither your custom style nor your custom language define what should be treated as strings in your listings. It seems you want to show visible spaces everywhere; in that case, as you correctly guessed, you should useshowspaces
instead.A couple of tips
(too long to leave in a TeX.SX comment):
style
andlanguage
are semantically different. In most cases, alistings
language should be used only to define what a language is (keywords, string delimiters, comments, etc.). In contrast, alistings
style should only be used to define what a given language should look like. You should first define the language, using keys such asmorekeywords
,morestring
,morecomment
, etc. Then, you should define the style by loading the newly defined language and customising the appearance, using keys such asbasicstyle
,keyworstyle
,stringstyle
,commentstyle
,showstringspaces
, etc. According to this rule, you are allowed to load a language within a style definition, but never the opposite, because a language is further up thelistings
hierarchy than a style is.showspaces
because that is the name of alistings
key. Such a name choice is probably safe (it shouldn't be associated with any side effects) but it has potential for confusion.listings
'\lstMakeShortInline
macro (see subsection 4.17 in the documentation). Of course, you should use a character that is rarely/never used: if it occurs in other places in yourtex
file, you run the risk of utterly confusing TeX.Here is how I think your code could be improved: