在 pythontex 中获得类似 spyder 的颜色突出显示

在 pythontex 中获得类似 spyder 的颜色突出显示

在使用 Python 3.4 时,我使用 spyder(anaconda3 2.0.1),并且我喜欢 Spyder 编辑器(版本 2.3)的突出显示功能。使用该pythontex包是否可以在 latex 文档中使用相同的突出显示方案?我在 Pygments 文档中没有找到这样的信息...

答案1

得票最多的答案是如何在 Latex 列表 lstinputlistings 命令中突出显示 Python 语法无法回答这个问题,因为它没有解决默认的 Spyder 突出显示问题。下面的示例(基于链接的示例)显示了如何使用默认的 Spyder 突出显示列出代码和输出(块和内联)。

此示例适用于 Python v3.x,即它是print一个函数而不是语句。它清楚地注释了如何自定义。

\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}

此示例输出: 在此处输入图片描述

相关内容