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.– jub0bsAug 7, 2014 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?– jub0bsAug 7, 2014 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. PooreAug 8, 2014 at 3:20
-
@Jubobs I would say the default spyder style ? it doesn't seem to have another name in the spyder editor.– guillaumeAug 8, 2014 at 5:19
-
@G.Poore sorry but I didn't find the style I'm looking for in that demo page.– guillaumeAug 8, 2014 at 5:21
1 Answer
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}