9

I have a Jupyter notebook that I wish to convert to pdf for publication, however when I save the notebook as a pdf many of the cells go over the edge.

Is there are way to wrap lines (to the standard 80 characters) so that as I type the cells are never wider than a standard A4 page?

Alternatively, is there something I can do when I convert to pdf instead? Thanks.

The Ref
  • 684
  • 2
  • 7
  • 20
  • See http://stackoverflow.com/questions/36419342/how-to-wrap-code-text-in-jupyter-notebooks – Roelant Apr 03 '17 at 11:15
  • Take a look at https://github.com/jupyter/nbconvert/issues/392 – dot.Py Apr 03 '17 at 11:15
  • @Roelant thanks for the link, however these line wraps do not persist when the notebook is converted to pdf. Do you know any way of making them remain? – The Ref Apr 03 '17 at 12:11
  • See https://github.com/jupyter/nbconvert/issues/323 tl;dr, either ensure your lines are short enough in your notebook, or use a custom post-processor to break lines. – Louise Davies Apr 03 '17 at 15:21

2 Answers2

1

Here is a solution which will always wrap long lines (not just on export to psd):

https://stackoverflow.com/a/39398949/5411817

Essentially, there is a flag in Jupyter's config file which turns on line wrapping.

Simply add the following to your config:

{
  "MarkdownCell": {
    "cm_config": {
      "lineWrapping": true
    }
  },
  "CodeCell": {
    "cm_config": {
      "lineWrapping": true
    }
  }
}

You'll need to restart Jupyter to see the change.

You can find (or create) your config file in your user directory: ~/.ipython/profile_nbserver/ipython_notebook_config.py,

-

My Bad: I did not realize that line wrapping breaks on export to PDF !! Comment under question by @Louie links to a discussion and sample code for writing a custom exporter. He also poses a workaround of manually wrapping long lines (in a pinch).

I'll leave my answer here, as it answers the question posted as the Title ("How do you wrap lines in a Jupiter Notebook?"), and highlights that the usual solution breaks on pdf export. Others looking for that answer can easily find it in this thread.

SherylHohman
  • 16,580
  • 17
  • 88
  • 94
1

The problem has been solved in nbconvert 5.5 Just update and run

jupyter nbconvert --to pdf your-notebook.ipynb 
moe
  • 1,716
  • 1
  • 14
  • 30