1

I am looking for a latex preamble in order to have a good python script. Here I found this

\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}

% Default fixed font does not support bold face \DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{12} % for bold \DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{12} % for normal

% Custom colors \usepackage{color} \definecolor{deepblue}{rgb}{0,0,0.5} \definecolor{deepred}{rgb}{0.6,0,0} \definecolor{deepgreen}{rgb}{0,0.5,0}

\usepackage{listings}

% Python style for highlighting \newcommand\pythonstyle{\lstset{ language=Python, basicstyle=\ttm, morekeywords={self}, % Add keywords here keywordstyle=\ttb\color{deepblue}, emph={MyClass,init}, % Custom highlighting emphstyle=\ttb\color{deepred}, % Custom highlighting style stringstyle=\color{deepgreen}, frame=tb, % Any extra options here showstringspaces=false }}

% Python environment

which looks great but I find noway to apply it into my latex code, phereaps I'm lost into the complexity of it and what I am looking for is just a sample code such as

\begin{document}

here some python codes

\begin{listings} \pythonstyle import numpy as np class init(self, a, b): self.a = a self.b = b \end{listings} \end{document}

or the like, in order to have a proper python ambient. Could you help me, please?

Torbjørn T.
  • 206,688

1 Answers1

3

You're doing two things wrong:

  1. the code environment of the listings package is not called listing, but lstlisting
  2. the \pythonstyle macro should go before the code environment

So if you do

\pythonstyle 
\begin{lstlisting}
import numpy as np
class __init__(self, a, b):
    self.a = a
    self.b = b
\end{lstlisting}

it will work.

I do think it's a bit odd to define a macro like that, instead of for example defining a new listings style with \lstdefinestyle. See example below.

enter image description here

\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}

% Default fixed font does not support bold face \DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{12} % for bold \DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{12} % for normal

% Custom colors \usepackage{color} \definecolor{deepblue}{rgb}{0,0,0.5} \definecolor{deepred}{rgb}{0.6,0,0} \definecolor{deepgreen}{rgb}{0,0.5,0}

\usepackage{listings}

% Python style for highlighting \newcommand\pythonstyle{\lstset{ language=Python, literate={-}{-}1 {}{}1,% {xxx}{\textrm{ }}1, basicstyle=\ttm, morekeywords={self}, % Add keywords here keywordstyle=\ttb\color{deepblue}, emph={MyClass,init}, % Custom highlighting emphstyle=\ttb\color{deepred}, % Custom highlighting style stringstyle=\color{deepgreen}, frame=tb, % Any extra options here showstringspaces=false }}

\lstdefinestyle{pythonstyling}{ language=Python, basicstyle=\ttm, morekeywords={self}, % Add keywords here keywordstyle=\ttb\color{deepblue}, emph={MyClass,init}, % Custom highlighting emphstyle=\ttb\color{deepred}, % Custom highlighting style stringstyle=\color{deepgreen}, frame=tb, % Any extra options here showstringspaces=false }

% Python \begin{document} here some python codes

\begin{lstlisting}[style=pythonstyling] import numpy as np class init(self, a, b): self.a = a self.b = b \end{lstlisting}

\pythonstyle \begin{lstlisting} import numpy as np class init(self, a, b): self.a = a self.b = b \end{lstlisting} \end{document}

Torbjørn T.
  • 206,688
  • That looks good, but if you want a pdf with a python code ready to be copied and pasted, than you need to make him understand that a python indentation is present. I tried with that "{xxx}{\textrm{ }}1", but it does not work. Would you suggest anything? – Stefano Fedele Sep 07 '22 at 09:32
  • @StefanoFedele If you set showstringspaces=true that should indicate spaces, but perhaps not what you want? – Torbjørn T. Sep 08 '22 at 10:09