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...

  • Usually, we don't put a greeting or a “thank you” in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Accepting and upvoting answers is the preferred way here to say “thank you” to users who helped you. – jub0bs Aug 7 '14 at 21:43
  • As far as I know, arbitrary colour schemes can be used inside the Spyder IDE. Can you be more specific as to what sort of syntax highlighting you want? – jub0bs Aug 7 '14 at 21:46
  • Pygments has lots of styles you can try here; if one of those isn't close enough to what you want, then you would have to create a custom style. – G. Poore Aug 8 '14 at 3:20
  • @Jubobs I would say the default spyder style ? it doesn't seem to have another name in the spyder editor. – guillaume Aug 8 '14 at 5:19
  • @G.Poore sorry but I didn't find the style I'm looking for in that demo page. – guillaume Aug 8 '14 at 5:21

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.


% This is the default font in Spyder

% These are close to the default font colors in Spyder
\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
    emph={self,cls,@classmethod,@property}, % Custom highlighting
    emphstyle=\color{pyspecialcol}\itshape, % Custom highlighting style
    keywordstyle=\color{pystatecol},        % statements
    % remove any inbuilt functions from keywords
    % Switch to predefined class that contain many, but not all,
    % inbuilt functions and classes
    % add any inbuilts, not statements

% Python environment
% Python for inline

% Python output style for highlighting
% Python output environment
% Python output for inline


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

A docstring and some comments:
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

\noindent A simple program:
# Python program to add two numbers
a = 1
b = 3

mysum = a + b
print("The sum of ", a, " and ", b, " is ", mysum)
\noindent That outputs:
The sum of  1  and  3  is  4

\noindent A function:
def my_func(list_of_numbers):
    """sum the list items"""
        return float(sum(l))
    except TypeError:
        return None

l = [1, 'boo', 2]
print("The sum is", my_func(l))

\noindent A class:
class MyClass(object):
    def __init__(self, my, yours):
        self.my = my
        self.yours = yours

    def mine(self):
        return self.my

\section{Inline highlighting}

Definition \pythoninline{class MyClass} means \dots


This example outputs: enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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