1

I have a latex document like this, where I define my own lstlsting language:

\documentclass{article}

\usepackage{listings}
\usepackage{xcolor}

\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
\definecolor{keywords1}{RGB}{193,89,45}
\definecolor{keywords2}{RGB}{55,80,146}
\definecolor{string}{RGB}{72,94,70}
 
\lstdefinelanguage{Python}{
    backgroundcolor=\color{backcolour},   
    commentstyle=\color{codegreen},
    keywords=[1]{class, import},
    keywordstyle=[1]\color{keywords1},
    keywords=[2]{print},
    keywordstyle=[2]\color{keywords2},
    numberstyle=\tiny\color{codegray},
    stringstyle=\color{string}\ttfamily,
    basicstyle=\footnotesize,
    breakatwhitespace=false,         
    breaklines=true,                 
    captionpos=b,                    
    keepspaces=true,                 
    numbers=none,                    
    numbersep=5pt,                  
    showspaces=false,                
    showstringspaces=false,
    showtabs=false,                  
    tabsize=2
}


\begin{document}

\begin{lstlisting}[language=Python]
import pandas as pd
import qnorm

df = pd.DataFrame({'C1': {'A': 5, 'B': 2, 'C': 3, 'D': 4},
                   'C2': {'A': 4, 'B': 1, 'C': 4, 'D': 2},
                   'C3': {'A': 3, 'B': 4, 'C': 6, 'D': 8}})

print(qnorm.quantile_normalize(df, axis=1))

         C1        C2        C3
A  5.666667  5.166667  2.000000
B  2.000000  2.000000  3.000000
C  3.000000  5.166667  4.666667
D  4.666667  3.000000  5.666667
\end{lstlisting}
\end{document}

this produces: enter image description here

How do I make sure the text in between the apostrophe's (') is colored by my defined string color? I have tried adding a stringstyle, but it seems like the apostrophe isn't recognized as string. Neither are other symbols, e.g. "

What am I doing wrong...?

4
  • 1
    Welcome to TeX.SE!
    – Mensch
    Jul 6 at 20:15
  • 1
    You need to add the string character to your Python language definition, so add string=[b]{'}.
    – Werner
    Jul 6 at 20:43
  • @Werner indeed, that works!! If you put it as an answer I'll accept it. Thanks! Jul 6 at 20:44
  • @Maarten-vd-Sande: Done.
    – Werner
    Jul 6 at 21:10

1 Answer 1

1

You need to specific the string character used in your newly-defined language. Do this by adding

string = [b]{'}

to your list of options. From the listings documentation (section 4.7 Language definitions):

Strings

string=[⟨b|d|m|bd|s⟩]{⟨delimiter (character)⟩}
morestring=[⟨b|d|m|bd|s⟩]{⟨delimiter⟩}
deletestring=[⟨b|d|m|bd|s⟩]{⟨delimiter⟩}

define, add to or delete the delimiter from the list of string delimiters. Starting and ending delimiters are the same, i.e. in the source code the delimiters must match each other.

The optional argument is the type and controls the how the delimiter itself is represented in a string or character literal: it is escaped by a backslash, doubled (or both is allowed via bd). Alternately, the type can refer to an unusual form of delimiter: string delimiters (akin to the s comment type) or matlab-style delimiters. The latter is a special type for Ada and Matlab and possibly other languages where the string delimiters are also used for other purposes. It is equivalent to d, except that a string does not start after a letter, a right parenthesis, a right bracket, or some other characters.

enter image description here

\documentclass{article}

\usepackage{listings}
\usepackage{xcolor}

\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
\definecolor{keywords1}{RGB}{193,89,45}
\definecolor{keywords2}{RGB}{55,80,146}
\definecolor{string}{RGB}{72,94,70}
 
\lstdefinelanguage{Python}{
    backgroundcolor=\color{backcolour},   
    commentstyle=\color{codegreen},
    keywords=[1]{class, import},
    keywordstyle=[1]\color{keywords1},
    keywords=[2]{print},
    keywordstyle=[2]\color{keywords2},
    numberstyle=\tiny\color{codegray},
    string=[b]{'},
    stringstyle=\color{string}\ttfamily,
    basicstyle=\footnotesize,
    breakatwhitespace=false,         
    breaklines=true,                 
    captionpos=b,                    
    keepspaces=true,                 
    numbers=none,                    
    numbersep=5pt,                  
    showspaces=false,                
    showstringspaces=true,
    showtabs=false,                  
    tabsize=2
}


\begin{document}

\begin{lstlisting}[language=Python]
import pandas as pd
import qnorm

df = pd.DataFrame({'C1': {'A': 5, 'B': 2, 'C': 3, 'D': 4},
                   'C2': {'A': 4, 'B': 1, 'C': 4, 'D': 2},
                   'C3': {'A': 3, 'B': 4, 'C': 6, 'D': 8}})

print(qnorm.quantile_normalize(df, axis=1))

         C1        C2        C3
A  5.666667  5.166667  2.000000
B  2.000000  2.000000  3.000000
C  3.000000  5.166667  4.666667
D  4.666667  3.000000  5.666667
\end{lstlisting}
\end{document}

You must log in to answer this question.

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