diff --git a/00_Preface.ipynb b/00_Preface.ipynb index 58c9ac2..8be43e0 100644 --- a/00_Preface.ipynb +++ b/00_Preface.ipynb @@ -14,6 +14,13 @@ "# Preface" ] }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "\\addcontentsline{toc}{chapter}{Preface}" + ] + }, { "cell_type": "code", "execution_count": 1, diff --git a/Appendix_A_Installation.ipynb b/Appendix_A_Installation.ipynb index 3bd7845..0e5daf5 100644 --- a/Appendix_A_Installation.ipynb +++ b/Appendix_A_Installation.ipynb @@ -8,27 +8,11 @@ ] }, { - "cell_type": "code", - "execution_count": 1, + "cell_type": "raw", "metadata": { "collapsed": false }, - "outputs": [ - { - "data": { - "text/latex": [ - "\\appendix" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], "source": [ - "%%latex\n", "\\appendix" ] }, diff --git a/pdf/book.tplx b/pdf/book.tplx index 0e955cd..63c83f3 100644 --- a/pdf/book.tplx +++ b/pdf/book.tplx @@ -4,7 +4,7 @@ ((* block docclass *)) \documentclass{book} -\setcounter{chapter}{-1} +\setcounter{chapter}{0} ((* endblock docclass *)) ((* block preamble *)) @@ -13,7 +13,6 @@ ((* block title *)) \title{Kalman and Bayesian Filters in Python} \author{Roger R Labbe Jr} -\date{} ((* endblock title *)) ((* block markdowncell scoped *)) diff --git a/pdf/build_book b/pdf/build_book index 7e1efe0..3607e85 100755 --- a/pdf/build_book +++ b/pdf/build_book @@ -5,7 +5,8 @@ echo "merging book..." python merge_book.py > Kalman_and_Bayesian_Filters_in_Python.ipynb echo "creating pdf..." -ipython nbconvert --to PDF --template book Kalman_and_Bayesian_Filters_in_Python.ipynb +ipython nbconvert --to latex --template book Kalman_and_Bayesian_Filters_in_Python.ipynb +ipython to_pdf.py mv Kalman_and_Bayesian_Filters_in_Python.pdf .. echo "done." diff --git a/pdf/merge_book.py b/pdf/merge_book.py index 24b25f7..71fca65 100644 --- a/pdf/merge_book.py +++ b/pdf/merge_book.py @@ -5,11 +5,11 @@ import sys def remove_formatting(nb): - c = nb['cells'] - for i in range (len(c)): - if 'input' in c[i].keys(): - if c[i]['input'][0:16] == '#format the book': - del c[i] + cells = nb['cells'] + for i in range (len(cells)): + if 'source' in cells[i].keys(): + if cells[i]['source'][0:16] == '#format the book': + del cells[i] return @@ -53,6 +53,11 @@ def merge_notebooks(filenames): if __name__ == '__main__': + '''merge_notebooks( + ['../00_Preface.ipynb', + '../01_g-h_filter.ipynb', + '../Appendix_A_Installation.ipynb'])''' + merge_notebooks( ['../00_Preface.ipynb', '../01_g-h_filter.ipynb', diff --git a/pdf/short_merge_book.py b/pdf/short_merge_book.py index d9c39d3..54bb3c7 100644 --- a/pdf/short_merge_book.py +++ b/pdf/short_merge_book.py @@ -1,32 +1,29 @@ from __future__ import print_function import io -from IPython.nbformat import current +import IPython.nbformat as nbformat import sys + def remove_formatting(nb): - w = nb['worksheets'] - node = w[0] - c = node['cells'] + c = nb['cells'] for i in range (len(c)): - if 'input' in c[i].keys(): - if c[i]['input'][0:16] == '#format the book': + if 'source' in c[i].keys(): + if c[i]['source'][0:16] == '#format the book': del c[i] return + def remove_links(nb): - w = nb['worksheets'] - node = w[0] - c = node['cells'] + c = nb['cells'] for i in range (len(c)): if 'source' in c[i].keys(): if c[i]['source'][0:19] == '[Table of Contents]': del c[i] return + def remove_links_add_appendix(nb): - w = nb['worksheets'] - node = w[0] - c = node['cells'] + c = nb['cells'] for i in range (len(c)): if 'source' in c[i].keys(): if c[i]['source'][0:19] == '[Table of Contents]': @@ -39,7 +36,7 @@ def merge_notebooks(filenames): added_appendix = False for fname in filenames: with io.open(fname, 'r', encoding='utf-8') as f: - nb = current.read(f, u'json') + nb = nbformat.read(f, nbformat.NO_CONVERT) remove_formatting(nb) if not added_appendix and fname[0:8] == 'Appendix': remove_links_add_appendix(nb) @@ -49,14 +46,15 @@ def merge_notebooks(filenames): if merged is None: merged = nb else: - merged.worksheets[0].cells.extend(nb.worksheets[0].cells) - merged.metadata.name += "_merged" + merged.cells.extend(nb.cells) + #merged.metadata.name += "_merged" - print(current.writes(merged, u'json')) + print(nbformat.writes(merged, nbformat.NO_CONVERT)) if __name__ == '__main__': #merge_notebooks(sys.argv[1:]) merge_notebooks( - ['../01_g-h_filter.ipynb', - '../02_Discrete_Bayes.ipynb']) + ['../00_Preface.ipynb', + '../01_g-h_filter.ipynb', + '../Appendix_A_Installation.ipynb']) diff --git a/pdf/to_pdf.py b/pdf/to_pdf.py new file mode 100644 index 0000000..90795d8 --- /dev/null +++ b/pdf/to_pdf.py @@ -0,0 +1,13 @@ +from __future__ import print_function +import io + +import IPython.nbconvert.exporters.pdf as pdf +import fileinput + +for line in fileinput.input('Kalman_and_Bayesian_Filters_in_Python.tex', inplace=True): + print(line.replace('\chapter{Preface}', '\chapter*{Preface}'), end='') + + +p = pdf.PDFExporter() +p.run_latex('Kalman_and_Bayesian_Filters_in_Python.tex') +