0

I want to include some Python code to my Latex document. I have read this Tex Stackexchange question and added the following code to get syntax highlights:

\usepackage{color}
\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{12} % for bold
\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{12}  % for normal
\definecolor{deepblue}{rgb}{0,0,0.5}
\definecolor{deepred}{rgb}{0.6,0,0}
\definecolor{deepgreen}{rgb}{0,0.5,0}

\newcommand\pythonstyle{\lstset{
    language=Python,
    basicstyle=\ttm,
    otherkeywords={self},             
    keywordstyle=\ttb\color{deepblue},
    emph={__init__},          
    emphstyle=\ttb\color{deepred},    
    stringstyle=\color{deepgreen},
    frame=tb,                         
    showstringspaces=false  
}}

\newcommand\pythonexternal[2][]{{
    \pythonstyle
    \lstinputlisting[#1]{#2}}
 }

Using the above code, the Python code in the Latex document is extremely large and it also goes far outside the right margin.

I only have a basic knowledge of Latex and don't really know how I can fix these issues.

1
  • you can tell listings to allow linebreaking (there is a key, I forget the name search for linebreaking in the manual) but python is rather sensitive to white space so you may not want that, or you could use \small or some other size to make it smaller, or you can adjust the python code by hand breaking the lines in a language aware manner to keep less than 80 (or whatever) characters per line. All are possible but you need to choose what happens... May 6, 2015 at 17:07

1 Answer 1

1

I improved upon my own question by taking some bits from this SO question.

My code for Python syntax coloring and layout is now like this:

\usepackage{color}
\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{12} % for bold
\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{12}  % for normal
\definecolor{deepblue}{rgb}{0,0,0.5}
\definecolor{deepred}{rgb}{0.6,0,0}
\definecolor{deepgreen}{rgb}{0,0.5,0}

% Python style for highlighting
\newcommand\pythonstyle{\lstset{
    language=Python,
    basicstyle=\footnotesize,
    otherkeywords={self},             
    keywordstyle=\footnotesize\color{deepblue},
    emph={__init__},          
    emphstyle=\footnotesize\color{deepred},    
    stringstyle=\color{deepgreen},
    frame=single,                         
    showstringspaces=false  ,
    breaklines=true,
    numbers=left,
    numberstyle=\footnotesize,
    tabsize=3,
    breakatwhitespace=false
}}

% Python environment
\lstnewenvironment{python}[1][]
{
    \pythonstyle
    \lstset{#1}
}{}

% Python external
\newcommand\pythonexternal[2][]{{
    \pythonstyle
    \lstinputlisting[#1]{#2}}
}

%Python inline
\newcommand\pythoninline[1]{{\pythonstyle\lstinline!#1!}}

All this code results in the following look:

example

So now, the Python code is smaller and stays inside the margin, solving my original problem.

1
  • If you've answered your question, don't forget to mark the answer as "accepted" by clicking on the tickmark below the answer's score.
    – jub0bs
    May 9, 2015 at 16:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .