1

Is there a way to change to colours from the codes in an IPython notebook when converting the nb to a LaTeX file? I want to change the green and blue that are by default

I was trying to do (in a .tplx file)

((* block definitions *))
    ((( super() )))

    % Custom prompts colors
    \definecolor{incolor}{rgb}{0.0, 0.2, 0.0}
    \definecolor{outcolor}{rgb}{0.3, 0.3, 0.0}
    \definecolor{blue}{rgb}{0.5,0.,.698} % does not seem to work
((* endblock definitions *))

But I don't know how to change other colours

Jakob
  • 19,815
  • 6
  • 75
  • 94
PerroNoob
  • 843
  • 2
  • 16
  • 36
  • do you mean changing the color of the `In` and `Out` prompt or the syntax highlighting? – Jakob Nov 05 '14 at 20:29
  • Yes. I was also trying to modify the `LatexFormatter` function (adding a style) in `/usr/local/lib/python2.7/dist-packages/IPython/nbconvert/filters/highlight.py` but with no success – PerroNoob Nov 06 '14 at 10:06

1 Answers1

2

[In] and [Out] Prompt

To change the colors of the In and Out prompt you can use a custom template, very similar to your attempt. Create a file (colors.tplx) with the following content:

((*- extends 'article.tplx' -*))

((* block definitions *))
  ((( super() )))

  \definecolor{incolor}{rgb}{0.8, 0.0, 0.0}
  \definecolor{outcolor}{rgb}{0.9, 0.3, 0.70}

((* endblock definitions *))

Of course, you have to adapt the colors of the prompts as you like. To use this template call ipython nbconvert --to pdf --template colors.tplx file.ipynb .

Note, the syntax highlighting is not affected this way.

Syntax coloring

The syntax highlighting seen in the final pdf is based on pygments. The default coloring is hardcoded in the LatexPreprocessor (see here). To get a different style a custom preprocessor has to be used. A possible preprocessor is a small modification of the current LatexPreprocessor and may look like (prelatex.py)

from __future__ import print_function, absolute_import
from IPython.nbconvert.preprocessors.base import Preprocessor

class MyLatexPreprocessor(Preprocessor):

    def preprocess(self, nb, resources):
        # Generate Pygments definitions for Latex
        from pygments.formatters import LatexFormatter

        resources["latex"]["pygments_definitions"] = LatexFormatter(style='emacs').get_style_defs()
        return nb, resources

Here, the emacs style is used. This works because the default LatexPreprocessor stores the pygments syntax color definitions in the resources dict, which is later pasted into the resulting .tex file. Our custom Preprocessor is executed after the default one and redefines the color definitions.

To enable this PreProcessor a custom config has to be created. This is as simple as creating a file (custom_cfg.py) like

c = get_config()
c.Exporter.preprocessors = ['prelatex.MyLatexPreprocessor']

With these both files in the working directory the nbconvert call looks like

ipython nbconvert --to latex --config custom_cfg.py file.ipynb

To get a list of the available styles the following snippet can be used.

from pygments.styles import get_all_styles
list(get_all_styles())

On my machine I get ['vs', 'pastie', 'borland', 'friendly', 'emacs', 'vim', 'native', 'perldoc', 'monokai', 'trac', 'tango', 'murphy', 'default', 'colorful', 'manni', 'bw', 'rrt', 'autumn', 'fruity']

Result

Combining both methods, ie. a custom template and a custom preprocessor the resulting pdf may look like:

enter image description here

You can find further information in this slightly outdated blog and this question.

Btw. I used ipython 3.0-dev (8d6041b)

Community
  • 1
  • 1
Jakob
  • 19,815
  • 6
  • 75
  • 94
  • Thanks! I couldn't find much information in Google or the iPython documentation. I just got to the point of the pre processor, but couldn't go further. Your answer is great for customizing the nbconvert functionality :) – PerroNoob Nov 07 '14 at 23:39