Repository: ipython/ipython

Issue:
nbconvert latex: code highlighting causes error
The latex generated from this notebook fails to compile due to the code highlighing:
http://nbviewer.ipython.org/2f85ddd8d967795dcb0e

and the tex: https://gist.github.com/ihnorton/a86c521d4aa223f9dba9

```
! LaTeX Error: Environment Shaded undefined.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.237         \begin{Shaded}

? 
! Emergency stop.
 ...                                              

l.237         \begin{Shaded}
```

Ubuntu 13.04, IPython 1.1, pandoc 1.10.1

edit: Confirmed in pandoc 1.12.01 on Windows 7



Discussion:
Same issue here with IPython 1.1.0 and pandoc 1.9.4.2.

Affects any / all versions of nbconvert and pandoc at this point.  The pandoc preamble that sets up the highlighting environments is not properly included in the final .tex document.


Resolving patch:
diff --git a/IPython/nbconvert/filters/ansi.py b/IPython/nbconvert/filters/ansi.py
--- a/IPython/nbconvert/filters/ansi.py
+++ b/IPython/nbconvert/filters/ansi.py
@@ -122,12 +122,13 @@ def single_ansi2latex(code):
         color_code = style_code.split(';')[1]
         is_normal = style_code.split(';')[0] == '0'
 
-        # regular fonts
+        # regular weight
         if (code == style_code) or (is_normal and code == color_code):
-            return '\\'+color[0].lower()+'{', 1
-        # bold fonts
+            
+            return r'{\color{'+color[0].lower()+'}', 1
+        # bold
         if code == style_code[:3]+str(1)+style_code[3:]:
-            return '\\textbf{\\textcolor{'+color[0].lower()+'}{', 2
+            return r'\textbf{\color{'+color[0].lower()+'}', 1
     return '', 0
 
 def ansi2latex(text):
@@ -149,8 +150,8 @@ def ansi2latex(text):
             texform, openbrack = single_ansi2latex(match.group())
             outstring += texform
         last_end = match.end()
-
-    #Add the remainer of the string and THEN close any remaining color brackets.
+    
+    # Add the remainer of the string and THEN close any remaining color brackets.
     outstring += text[last_end:]
     if openbrack: 
         outstring += '}'*openbrack


Test patch:
diff --git a/IPython/nbconvert/filters/tests/test_ansi.py b/IPython/nbconvert/filters/tests/test_ansi.py
--- a/IPython/nbconvert/filters/tests/test_ansi.py
+++ b/IPython/nbconvert/filters/tests/test_ansi.py
@@ -68,12 +68,12 @@ def _try_ansi2html(self, inval, outval):
     def test_ansi2latex(self):
         """ansi2latex test"""
         correct_outputs = {
-            '%s' % (TermColors.Red)  : r'\red{}',
-            'hello%s' % TermColors.Blue: r'hello\blue{}',
-            'he%s%sllo' % (TermColors.Green, TermColors.Cyan) : r'he\green{}\cyan{llo}',
-            '%shello' % TermColors.Yellow : r'\yellow{hello}',
-            '{0}h{0}e{0}l{0}l{0}o{0}'.format(TermColors.White) : r'\white{h}\white{e}\white{l}\white{l}\white{o}\white{}',
-            'hel%slo' % TermColors.Green : r'hel\green{lo}',
+            '%s' % (TermColors.Red)  : r'{\color{red}}',
+            'hello%s' % TermColors.Blue: r'hello{\color{blue}}',
+            'he%s%sllo' % (TermColors.Green, TermColors.Cyan) : r'he{\color{green}}{\color{cyan}llo}',
+            '%shello' % TermColors.Yellow : r'{\color{yellow}hello}',
+            '{0}h{0}e{0}l{0}l{0}o{0}'.format(TermColors.White) : r'{\color{white}h}{\color{white}e}{\color{white}l}{\color{white}l}{\color{white}o}{\color{white}}',
+            'hel%slo' % TermColors.Green : r'hel{\color{green}lo}',
             'hello' : 'hello'}
 
         for inval, outval in correct_outputs.items():
diff --git a/IPython/nbconvert/tests/files/notebook2.ipynb b/IPython/nbconvert/tests/files/notebook2.ipynb
--- a/IPython/nbconvert/tests/files/notebook2.ipynb
+++ b/IPython/nbconvert/tests/files/notebook2.ipynb
@@ -1,6 +1,6 @@
 {
  "metadata": {
-  "name": "notebook2"
+  "name": ""
  },
  "nbformat": 3,
  "nbformat_minor": 0,
@@ -171,12 +171,15 @@
      "prompt_number": 14
     },
     {
-     "cell_type": "code",
-     "collapsed": false,
-     "input": [],
-     "language": "python",
+     "cell_type": "markdown",
      "metadata": {},
-     "outputs": []
+     "source": [
+      "```python\n",
+      "def foo(bar=1):\n",
+      "    \"\"\"docstring\"\"\"\n",
+      "    raise Exception(\"message\")\n",
+      "```"
+     ]
     }
    ],
    "metadata": {}