# Mathematica export notebook into pdf files with table of contents and bookmarks

# Mathematica

I have been using Mathemtica to write notes. It’s really convenient to use notebook files because we can write program, formular, figures and text in one file. However, not everyone uses Mathematica, If we want to share our notes with others, exporting nb files into pdf format is necessary.

In default, the generated pdf by Mathematica is relatively simple, and there is no TOC or bookmarks in the generated pdf files. It will be very inconvenient to read if the notes are very long. I have searched on stack exchange, but it seems that there is no existing good solution. According to the question, may be we can save the nb files into `tex`

format and then compile it into pdf, but (according to my experience) problem exists when the nb file is long and the formular is complicated.

Finally, I myself try to add bookmarks mannually. I use python to process the generated pdf files, what I want to realize can be summarized as follows:

- Using Mathematica: For a long notebook file, we directly generate the pdf files (
`pdf file 1`

) - Using Mathematica: Then I will extract the chapter/section information and generate a pdf file (
`pdf file 2`

) containing the table of contents (with suitable pdf page numbers) - Using Python：Finally, I will use python to merge the two pdfs into
`pdf file 3`

and add bookmarks into this merged pdf (`pdf file 3`

).

It seems that the above process is cumbersome, but it can be automated by the program, and the operation is very simple. I will show an example:

Assuming that the notebook file to be processed is `test.nb`

, then in Mathematica

(1) we just need run the following program to generate the helping files

1 | (*The notebook directory*) |

**NOTES:**

By runing above Mathematica program, two pdf files will be generated (

`pdf file 1`

,`pdf file 2`

). Extra txt file names`test.txt`

is also generated. In this txt file, information about the TOC are saved.

(2)then we use the corresponding python program (see appendix II) to generate the final pdf files.

If you don’t want to install python, I have also written a program with simple GUI and compiled it into Exe format. This can be directly run on windows. Original code can be found on this

the final pdf files will have toc and bookmarks:

However, by using python, the original links in PDF will be lost, so you can’t jump into certain page by clicking the links, this is the problem of the python package `PyPDF2`

.

## Appendix

### the Mathematica function

modefied from the question

1 | *helper file for creating cell*) |

### Python program to generate TOC

1 | # we set the file name to be processed |