Repository: ipython/ipython

Issue:
nbconvert: Unicode error with minus sign
Running
`ipython nbconvert --format="latex" odes_clean.ipynb`
I get a strange (to my mind) unicode error, which seems to be a minus sign, apparently in an SVG?

```
/bin/sh: /Applications/Inkscape.app/Contents/Resources/bin/inkscape: No such file or directory
/bin/sh: /Applications/Inkscape.app/Contents/Resources/bin/inkscape: No such file or directory
Traceback (most recent call last):
  File "/usr/local/bin/ipython", line 6, in <module>
    start_ipython()
  File "/Users/dsanders/development/ipython/IPython/__init__.py", line 118, in start_ipython
    return launch_new_instance(argv=argv, **kwargs)
  File "/Users/dsanders/development/ipython/IPython/config/application.py", line 539, in launch_instance
    app.start()
  File "/Users/dsanders/development/ipython/IPython/terminal/ipapp.py", line 362, in start
    return self.subapp.start()
  File "/Users/dsanders/development/ipython/IPython/nbconvert/nbconvertapp.py", line 176, in start
    self.convert_notebooks()
  File "/Users/dsanders/development/ipython/IPython/nbconvert/nbconvertapp.py", line 197, in convert_notebooks
    config=self.config)
  File "/Users/dsanders/development/ipython/IPython/nbconvert/exporters/export.py", line 61, in decorator
    return f(*args, **kwargs)
  File "/Users/dsanders/development/ipython/IPython/nbconvert/exporters/export.py", line 214, in export_by_name
    return globals()[function_name](nb, **kw)
  File "/Users/dsanders/development/ipython/IPython/nbconvert/exporters/export.py", line 61, in decorator
    return f(*args, **kwargs)
  File "/Users/dsanders/development/ipython/IPython/nbconvert/exporters/export.py", line 165, in export_latex
    return export(LatexExporter, nb, **kw)
  File "/Users/dsanders/development/ipython/IPython/nbconvert/exporters/export.py", line 61, in decorator
    return f(*args, **kwargs)
  File "/Users/dsanders/development/ipython/IPython/nbconvert/exporters/export.py", line 122, in export
    output, resources = exporter_instance.from_filename(nb, resources)
  File "/Users/dsanders/development/ipython/IPython/nbconvert/exporters/exporter.py", line 221, in from_filename
    return self.from_notebook_node(nbformat.read(f, 'json'), resources=resources,**kw)
  File "/Users/dsanders/development/ipython/IPython/nbconvert/exporters/exporter.py", line 190, in from_notebook_node
    nb_copy, resources = self._transform(nb_copy, resources)
  File "/Users/dsanders/development/ipython/IPython/nbconvert/exporters/exporter.py", line 442, in _transform
    nbc, resc = transformer(nbc, resc)
  File "/Users/dsanders/development/ipython/IPython/nbconvert/transformers/base.py", line 61, in __call__
    return self.call(nb,resources)
  File "/Users/dsanders/development/ipython/IPython/nbconvert/transformers/base.py", line 85, in call
    worksheet.cells[index], resources = self.transform_cell(cell, resources, index)
  File "/Users/dsanders/development/ipython/IPython/nbconvert/transformers/convertfigures.py", line 54, in transform_cell
    self._convert_figure(cell_out, resources, data_type, data)
  File "/Users/dsanders/development/ipython/IPython/nbconvert/transformers/convertfigures.py", line 63, in _convert_figure
    data = self.convert_figure(data_type, data)
  File "/Users/dsanders/development/ipython/IPython/nbconvert/transformers/svg2pdf.py", line 77, in convert_figure
    f.write(data)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2212' in position 13282: ordinal not in range(128)
```



Discussion:

Resolving patch:
diff --git a/IPython/nbconvert/transformers/svg2pdf.py b/IPython/nbconvert/transformers/svg2pdf.py
--- a/IPython/nbconvert/transformers/svg2pdf.py
+++ b/IPython/nbconvert/transformers/svg2pdf.py
@@ -14,6 +14,7 @@
 #-----------------------------------------------------------------------------
 
 import base64
+import io
 import os
 import sys
 import subprocess
@@ -28,9 +29,7 @@
 # Constants
 #-----------------------------------------------------------------------------
 
-INKSCAPE_COMMAND = 'inkscape --without-gui --export-pdf="{to_filename}" "{from_filename}"'
-INKSCAPE_OSX_COMMAND = '/Applications/Inkscape.app/Contents/Resources/bin/inkscape --without-gui --export-pdf="{to_filename}" "{from_filename}"'
-
+INKSCAPE_APP = '/Applications/Inkscape.app/Contents/Resources/bin/inkscape'
 
 #-----------------------------------------------------------------------------
 # Classes
@@ -43,6 +42,7 @@ class SVG2PDFTransformer(ConvertFiguresTransformer):
 
     from_format = Unicode('svg', config=True, help='Format the converter accepts')
     to_format = Unicode('pdf', config=False, help='Format the converter writes')
+    
     command = Unicode(config=True,
         help="""The command to use for converting SVG to PDF
         
@@ -54,13 +54,15 @@ class SVG2PDFTransformer(ConvertFiguresTransformer):
         """)
     
     def _command_default(self):
+        return self.inkscape + \
+               ' --without-gui --export-pdf="{to_filename}" "{from_filename}"'
+    
+    inkscape = Unicode(config=True, help="The path to Inkscape, if necessary")
+    def _inkscape_default(self):
         if sys.platform == "darwin":
-            return INKSCAPE_OSX_COMMAND
-        elif sys.platform == "win32":
-            # windows not yet supported
-            return ""
-        else:
-            return INKSCAPE_COMMAND
+            if os.path.isfile(INKSCAPE_APP):
+                return INKSCAPE_APP
+        return "inkscape"
 
 
     def convert_figure(self, data_format, data):
@@ -73,7 +75,8 @@ def convert_figure(self, data_format, data):
             
             #Write fig to temp file
             input_filename = os.path.join(tmpdir, 'figure.' + data_format)
-            with open(input_filename, 'wb') as f:
+            # SVG data is unicode text
+            with io.open(input_filename, 'w', encoding='utf8') as f:
                 f.write(data)
 
             #Call conversion application
@@ -89,4 +92,4 @@ def convert_figure(self, data_format, data):
                     # PDF is a nb supported binary, data type, so base64 encode.
                     return base64.encodestring(f.read())
             else:
-                return TypeError("Inkscape svg to png conversion failed")
+                raise TypeError("Inkscape svg to png conversion failed")


Test patch: