Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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:

What it looks like

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} 
share|improve this question
    
I've realised this is probably something to do with listings not reading the text as a string literal and so not applying string spaces. I'm trying to find a way to use morestring to match all text –  Pez Cuckow Apr 26 at 10:48

2 Answers 2

up vote 2 down vote accepted

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 use showspaces instead.

A couple of tips

(too long to leave in a TeX.SX comment):

  1. style and language are semantically different. In most cases, a listings language should be used only to define what a language is (keywords, string delimiters, comments, etc.). In contrast, a listings style should only be used to define what a given language should look like. You should first define the language, using keys such as morekeywords, morestring, morecomment, etc. Then, you should define the style by loading the newly defined language and customising the appearance, using keys such as basicstyle, 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 the listings hierarchy than a style is.
  2. Don't use the name showspaces for a new style. You shouldn't define a style called showspaces because that is the name of a listings key. Such a name choice is probably safe (it shouldn't be associated with any side effects) but it has potential for confusion.
  3. Load the language first, customise the look later. When you define a style, load the associated language (or the base style) first, and customise the look only afterwards. Loading a language or a style "too late" has the potential to wipe out most of the current settings.
  4. Use a one-character shorthand for inline code. If you use inline code throughout your document, you can save yourself some keystrokes by defining a one-character shorthand for inline code in the style of your choice, using 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 your tex file, you run the risk of utterly confusing TeX.

Here is how I think your code could be improved:

screenshot of output

\documentclass{article}  

% Color
\usepackage{xcolor}
\usepackage{textcomp}

% Code
\usepackage{listings}

\lstdefinelanguage{vizspaces-lang}{
  alsoletter={0,1,2,3,4,5,6,7,8,9,.,/,:}, % Is there a good reason for that?
}

\lstdefinestyle{vizspaces-sty}{
  language         = vizspaces-lang,
  basicstyle       = \footnotesize\ttfamily,
  breaklines       = true,
  showspaces       = true,
% showstringspaces = true, % (not needed if showspaces is set)
  upquote          = true,
}

\lstMakeShortInline[style=vizspaces-sty]"

\begin{document}  

\begin{table}[h]
\begin{tabular}{ll}
Raw Transactor Name & Occurrences  \\
"TESCO STORES 5128"   & 87         \\
"TESCO STORES 2977"   & 68         \\
"TESCO_STORES"        & 14         \\
"SACAT MARKS AND"     & 33         \\
"SACAT MARKS  AND"    & 16         \\
"WILKINSON "          & 22         \\
"WILKINSON"           & 8         
\end{tabular}
\label{table:cleaningstrings}
\end{table}

\end{document}
share|improve this answer

I discovered my mistake, what I hinted at in my comment was right, listings was not treating the text as a string. However I found the option showspaces=true which shows spaces found anywhere in the listing and does exactly what I wanted.

Text with spaces

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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