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?

9
  • 2
    Have a look at minted package Sep 6, 2022 at 19:04
  • I tried one of that codes. The compiler says: "you must invoke latex with the shell escape flag..." Still issues Sep 6, 2022 at 19:11
  • 1
    yes, you should use pdflatex -shell-escape FILENAME Sep 6, 2022 at 19:12
  • I'm sorry, I still do not understand. Why and how should I do that? Sep 6, 2022 at 19:24
  • @StefanoFedele it depends on how you are compiling your document. I use arara like this: arara: pdflatex: {options: -shell-escape} when I need to shell escape. Minted and other packages/programmes (like makeindex I believe) require -shell-escape to run. Look at arara or latexmk depending upon what you like, the documentation explains it better then I.
    – JamesT
    Sep 6, 2022 at 19:29

1 Answer 1

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}
2
  • 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? Sep 7, 2022 at 9:32
  • @StefanoFedele If you set showstringspaces=true that should indicate spaces, but perhaps not what you want? Sep 8, 2022 at 10:09

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.