├── LICENSE.rst ├── README.md └── pythonhighlight.sty /LICENSE.rst: -------------------------------------------------------------------------------- 1 | Copyright (c) 2009--2017, Olivier Verdier 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 8 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 9 | * Neither the name of "pythonhighlighting" nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 10 | 11 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Python highlighting in LaTeX 2 | ============================ 3 | 4 | A simple Python highlighting style to be used with LaTeX. 5 | 6 | The package is loaded by the following line: 7 | 8 | ```tex 9 | \usepackage{pythonhighlight} 10 | ``` 11 | 12 | It is then possible to include a Python snippet directly in the code using: 13 | 14 | ```tex 15 | \begin{python} 16 | def f(x): 17 | return x 18 | \end{python} 19 | ``` 20 | 21 | It is also possible to include inline Python code in LaTeX with ``\pyth``: 22 | 23 | ```tex 24 | The special method \pyth{__init__}... 25 | ``` 26 | 27 | This also works with other delimiters, for instance: 28 | ```tex 29 | We use the dictionary \pyth|d = {"a": 1, "b": 2}|. 30 | ``` 31 | 32 | Last but not least, you can load an external Python file with: 33 | ```tex 34 | \inputpythonfile{python_file.py}[23][50] 35 | ``` 36 | to display the contents of the file ``python_file`` from line 23 to line 50, 37 | or with 38 | ```tex 39 | \inputpythonfile{python_file.py} 40 | ``` 41 | to input the whole Python file. 42 | -------------------------------------------------------------------------------- /pythonhighlight.sty: -------------------------------------------------------------------------------- 1 | % Copyright 2009--2024 by Olivier Verdier 2 | % License: see the file LICENSE.rst 3 | \NeedsTeXFormat{LaTeX2e} 4 | \ProvidesPackage{pythonhighlight}[2024-11-14 Python code highlighting; provided by Olivier Verdier ] 5 | 6 | 7 | \RequirePackage{listings} 8 | \RequirePackage{xcolor} 9 | \RequirePackage{xparse} 10 | 11 | \renewcommand*{\lstlistlistingname}{Code Listings} 12 | \renewcommand*{\lstlistingname}{Code Listing} 13 | \definecolor{gray}{gray}{0.5} 14 | \colorlet{commentcolour}{green!50!black} 15 | 16 | \colorlet{stringcolour}{red!60!black} 17 | \colorlet{keywordcolour}{magenta!90!black} 18 | \colorlet{exceptioncolour}{yellow!50!red} 19 | \colorlet{commandcolour}{blue!60!black} 20 | \colorlet{numpycolour}{blue!60!green} 21 | \colorlet{literatecolour}{magenta!90!black} 22 | \colorlet{promptcolour}{green!50!black} 23 | \colorlet{specmethodcolour}{violet} 24 | 25 | \newcommand*{\framemargin}{3ex} 26 | 27 | \newcommand*{\literatecolour}{\textcolor{literatecolour}} 28 | 29 | \newcommand*{\pythonprompt}{\textcolor{promptcolour}{{>}{>}{>}}} 30 | 31 | \lstdefinestyle{pythonhighlight-style}{ 32 | %\lstset{ 33 | %keepspaces=true, 34 | language=python, 35 | inputencoding=utf8, 36 | showtabs=true, 37 | tab=, 38 | tabsize=2, 39 | basicstyle=\ttfamily\footnotesize,%\setstretch{.5}, 40 | stringstyle=\color{stringcolour}, 41 | showstringspaces=false, 42 | alsoletter={1234567890}, 43 | otherkeywords={\%, \}, \{, \&, \|}, 44 | keywordstyle=\color{keywordcolour}\bfseries, 45 | morekeywords={with,as,and,async,await,assert,break,class,continue,def,del,elif,else,except,finally,for,from,global,if,import,in,lambda,nonlocal,not,or,pass,raise,return,try,while,yield}, 46 | % emphstyle={[1001]\color{blue}\bfseries}, 47 | emph={[1002]True, False, None}, 48 | emphstyle={[1002]\color{keywordcolour}}, 49 | emph={[1003]object,type,isinstance,copy,deepcopy,zip,enumerate,reversed,list,set,len,dict,tuple,xrange,append,execfile,real,imag,reduce,str,repr}, 50 | emphstyle={[1003]\color{commandcolour}}, 51 | emph={[1001]Exception,NameError,IndexError,SyntaxError,TypeError,ValueError,OverflowError,ZeroDivisionError}, 52 | emphstyle={[1001]\color{exceptioncolour}\bfseries}, 53 | %upquote=true, 54 | morecomment=[s]{"""}{"""}, 55 | commentstyle=\color{commentcolour}\slshape, 56 | %emph={[4]1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, 57 | emph={[1004]ode, fsolve, sqrt, exp, sin, cos,arctan, arctan2, arccos, pi, array, norm, solve, dot, arange, isscalar, max, sum, flatten, shape, reshape, find, any, all, abs, plot, linspace, legend, quad, polyval,polyfit, hstack, concatenate,vstack,column_stack,empty,zeros,ones,rand,vander,grid,pcolor,eig,eigs,eigvals,svd,qr,tan,det,logspace,roll,min,mean,cumsum,cumprod,diff,vectorize,lstsq,cla,eye,xlabel,ylabel,squeeze,isscalar}, 58 | emphstyle={[1004]\color{numpycolour}}, 59 | emph={[1005]__init__,__add__,__mul__,__div__,__sub__,__call__,__getitem__,__setitem__,__eq__,__ne__,__nonzero__,__rmul__,__radd__,__repr__,__str__,__get__,__truediv__,__pow__,__name__,__future__,__all__,__main__,__doc__,__module__,__dict__,self}, 60 | emphstyle=[1005]\color{specmethodcolour}, 61 | emph={[1006]assert,yield}, 62 | emphstyle=[1006]\color{keywordcolour}\bfseries, 63 | emph={[1007]range}, 64 | emphstyle={[1007]\color{keywordcolour}\bfseries}, 65 | % emph={[8]self}, 66 | % emphstyle=[8]\bfseries, 67 | literate=*% 68 | {\%}{{\literatecolour:}}{1}% 69 | {:}{{\literatecolour:}}{1}% 70 | {=}{{\literatecolour=}}{1}% 71 | {-}{{\literatecolour-}}{1}% 72 | {+}{{\literatecolour+}}{1}% 73 | {*}{{\literatecolour*}}{1}% 74 | {**}{{\literatecolour{**}}}2% 75 | {/}{{\literatecolour/}}{1}% 76 | {//}{{\literatecolour{//}}}2% 77 | {!}{{\literatecolour!}}{1}% 78 | %{(}{{\literatecolour(}}{1}% 79 | %{)}{{\literatecolour)}}{1}% 80 | {[}{{\literatecolour[}}{1}% 81 | {]}{{\literatecolour]}}{1}% 82 | {<}{{\literatecolour<}}{1}% 83 | {>}{{\literatecolour>}}{1}% 84 | {>>>}{\pythonprompt}{3}% 85 | ,% 86 | %aboveskip=.5ex, 87 | frame=trbl, 88 | %frameround=tttt, 89 | %framesep=.3ex, 90 | rulecolor=\color{black!40}, 91 | %framexleftmargin=\framemargin, 92 | %framextopmargin=.1ex, 93 | %framexbottommargin=.1ex, 94 | %framexrightmargin=\framemargin, 95 | %framexleftmargin=1mm, framextopmargin=1mm, frame=shadowbox, rulesepcolor=\color{blue},#1 96 | %frame=tb, 97 | backgroundcolor=\color{white}, 98 | breakindent=.5\textwidth,frame=single,breaklines=true% 99 | %} 100 | } 101 | 102 | \newcommand*{\inputpython}[3]{\lstinputlisting[firstline=#2,lastline=#3,firstnumber=#2,frame=single,breakindent=.5\textwidth,frame=single,breaklines=true,style=pythonhighlight-style]{#1}} 103 | \NewDocumentCommand\inputpythonfile{moo}{\lstinputlisting[ 104 | firstline=\IfValueTF{#2}{#2}{0}, 105 | % firstnumber=\IfValueTF{#2}{#2}{0}, 106 | lastline=\IfValueTF{#3}{#3}{9999999}, 107 | frame=single, 108 | breakindent=.5\textwidth, 109 | frame=single, 110 | breaklines=true, 111 | style=pythonhighlight-style 112 | ]{#1}} 113 | 114 | \lstnewenvironment{python}[2][]{% 115 | \lst@TestEOLChar{#2}% 116 | \lstset{style=pythonhighlight-style}% 117 | \lstset{#1}% % has to be in an extra \lstset{} command so that labels work correctly 118 | \csname\@lst @SetFirstNumber\endcsname% 119 | }{% 120 | \let\if@nobreak\iffalse% 121 | \csname\@lst @SaveFirstNumber\endcsname% 122 | } 123 | 124 | \lstdefinestyle{pythonhighlight-inline-style}{ 125 | style=pythonhighlight-style,% 126 | basicstyle=\ttfamily,% 127 | keywordstyle=\color{keywordcolour},% 128 | emphstyle={[7]\color{keywordcolour}},% 129 | emphstyle=\color{exceptioncolour},% 130 | literate=*% 131 | {:}{{\literatecolour:}}{2}% 132 | {=}{{\literatecolour=}}{2}% 133 | {-}{{\literatecolour-}}{2}% 134 | {+}{{\literatecolour+}}{2}% 135 | {*}{{\literatecolour*}}2% 136 | {**}{{\literatecolour{**}}}3% 137 | {/}{{\literatecolour/}}{2}% 138 | {//}{{\literatecolour{//}}}{2}% 139 | {!}{{\literatecolour!}}{2}% 140 | %{(}{{\literatecolour(}}{2}% 141 | %{)}{{\literatecolour)}}{2}% 142 | {[}{{\literatecolour[}}{2}% 143 | {]}{{\literatecolour]}}{2}% 144 | {<}{{\literatecolour<}}{2}% 145 | {<=}{{\literatecolour{<=}}}3% 146 | {>}{{\literatecolour>}}{2}% 147 | {>=}{{\literatecolour{>=}}}3% 148 | {==}{{\literatecolour{==}}}3% 149 | {!=}{{\literatecolour{!=}}}3% 150 | {+=}{{\literatecolour{+=}}}3% 151 | {-=}{{\literatecolour{-=}}}3% 152 | {*=}{{\literatecolour{*=}}}3% 153 | {/=}{{\literatecolour{/=}}}3% 154 | %% emphstyle=\color{blue},% 155 | } 156 | 157 | \newcommand*{\pyth}{\lstinline[style=pythonhighlight-inline-style,keepspaces=true]} 158 | 159 | --------------------------------------------------------------------------------