1

In my work with Python 3.4, I use spyder (anaconda3 2.0.1) and I like the highlighting of the Spyder editor (version 2.3). Is it possible to have the same highlighting scheme in latex documents using the pythontex package ? I didn't find such an information in the Pygments documentation...

Alan Munn
  • 218,180

1 Answers1

2

The most up-voted answer at how-to-highlight-python-syntax-in-latex-listings-lstinputlistings-command does not answer this question, as it does not address the default Spyder highlighting. The example below (based on the one linked) shows how to list code and output, both in block and inline, with the default Spyder highlighting.

This example is Python v3.x friendly, that is print is a function not a statement. It is clearly commented on how to customise.

\documentclass[a4paper,12pt]{article}

% This is the default font in Spyder
\newcommand*{\pyfontfamily}{\fontfamily{DejaVuSansMono-TLF}\selectfont}

% These are close to the default font colors in Spyder
\usepackage{color}
\definecolor{pycommentcol}{rgb}{0.3,0.3,0.3}     % gray
\definecolor{pystatecol}{rgb}{0,0,0.7}           % blue
\definecolor{pystringcol}{rgb}{0,0.6,0}          % green
\definecolor{pyinbuiltscol}{rgb}{0.55,0.15,0.55} % plum
\definecolor{pyspecialcol}{rgb}{0.8,0.45,0.12}   % orange

% Python style for highlighting
% for help with listings
% see docs http://texdoc.net/texmf-dist/doc/latex/listings/listings.pdf
\usepackage{listings}
\newcommand\pythonstyle{\lstset{
    language=Python,
    basicstyle=\pyfontfamily,
    commentstyle=\color{pycommentcol}\itshape,
    emph={self,cls,@classmethod,@property}, % Custom highlighting
    emphstyle=\color{pyspecialcol}\itshape, % Custom highlighting style
    morestring=[b]{"""},
    stringstyle=\color{pystringcol},
    keywordstyle=\color{pystatecol},        % statements
    % remove any inbuilt functions from keywords
    deletekeywords={print},
    % Switch to predefined class that contain many, but not all,
    % inbuilt functions and classes
    classoffset=1,
    % add any inbuilts, not statements
    morekeywords={print,None,TypeError},
    keywordstyle=\color{pyinbuiltscol},
    frame=tb,                        
    showstringspaces=false            
}}

% Python environment
\lstnewenvironment{python}[1][]
{
    \pythonstyle
    \scriptsize
    \lstset{#1}
}
{}
% Python for inline
\newcommand\pythoninline[1]{{\pythonstyle\lstinline!#1!}}

% Python output style for highlighting
\newcommand\pythonoutstyle{\lstset{
    basicstyle=\pyfontfamily,                     
    showstringspaces=false            
}}
% Python output environment
\lstnewenvironment{pythonout}[1][]
{
    \pythonoutstyle
    \scriptsize
    \lstset{#1}
}
{}
% Python output for inline
\newcommand\pythonoutinline[1]{{\pythonoutstyle\lstinline!#1!}}

\begin{document}

\section{``In-text'' listing highlighting}

A docstring and some comments:
\begin{python}
"""
Created on Thu Feb 20 18:33:21 2020

@author: Alexander Pitchford

Some examples for testing listings in Latex aiming to match Spyder colours

This should be green!
"""

# This should be grey
# PEP8 says that lines should be no longer than 79 characters
# This is a completely full line of nonsense just to take up some space and see
# if it fits
\end{python}

\vspace{5mm}
\noindent A simple program:
\begin{python}
# Python program to add two numbers
a = 1
b = 3

mysum = a + b
print("The sum of ", a, " and ", b, " is ", mysum)
\end{python}
\noindent That outputs:
\begin{pythonout}
The sum of  1  and  3  is  4
\end{pythonout}

\vspace{5mm}
\noindent A function:
\begin{python}
def my_func(list_of_numbers):
    """sum the list items"""
    try:
        return float(sum(l))
    except TypeError:
        return None

l = [1, 'boo', 2]
print("The sum is", my_func(l))
\end{python}

\vspace{5mm}
\noindent A class:
\begin{python}
class MyClass(object):
    def __init__(self, my, yours):
        self.my = my
        self.yours = yours

    @property
    def mine(self):
        return self.my
\end{python}

\section{Inline highlighting}

Definition \pythoninline{class MyClass} means \dots

\end{document}

This example outputs: enter image description here