Altered PDF generation.

Added code to inject \appendix to the first appendix notebook
so that they are properly labelled. Started g-h chapter as chapter
one. There is probably a lot more I want to do now that I have
the basic idea of how to inject latex into the files before creating
the PDF, but this is a good start.
This commit is contained in:
Roger Labbe 2015-01-17 09:39:26 -08:00
parent dbcb166150
commit afe0b5626e
11 changed files with 278 additions and 115 deletions

3
.gitignore vendored
View File

@ -1,3 +1,6 @@
# test document not for distribution
short.pdf
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]

File diff suppressed because one or more lines are too long

View File

@ -20,7 +20,7 @@
"level": 1,
"metadata": {},
"source": [
"Appendix: Installation, Python, Numpy, and filterpy"
"Installation, Python, Numpy, and filterpy"
]
},
{

View File

@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:96b8d49070c26012f68e9c586fe5de841f9bf752ab9ecd8bd8c787afa0086ad3"
"signature": "sha256:53bdd4d227576a358de986c4a5ec6cf238e76c88031dc1b6d55f06ce9fc5c11b"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -262,7 +262,7 @@
"level": 1,
"metadata": {},
"source": [
"Appendix I : Symbology"
"Symbology"
]
},
{

View File

@ -8,6 +8,8 @@ rm --f ./*_files/*.png
rm --f Kalman_and_Bayesian_Filters_in_Python.ipynb
rm --f Kalman_and_Bayesian_Filters_in_Python.toc
rm --f Kalman_and_Bayesian_Filters_in_Python.tex
rm --f short.ipynb
rmdir ./*_files/ 2> /dev/null
if (( $# == 1)); then

View File

@ -1,17 +0,0 @@
#! /bin/bash
rm --f *.tex
rm --f *.toc
rm --f *.aux
rm --f *.log
rm --f *.out
rm --f ./*_files/*.png
rm --f Kalman_and_Bayesian_Filters_in_Python.ipynb
rm --f Kalman_and_Bayesian_Filters_in_Python.toc
rm --f Kalman_and_Bayesian_Filters_in_Python.tex
rmdir ./*_files/ 2> /dev/null
if (( $# == 1)); then
if [ "@1" == all ]; then
rm Kalman_and_Bayesian_Filters_in_Python.pdf;
fi
fi

View File

@ -3,6 +3,7 @@ import io
from IPython.nbformat import current
import sys
def remove_formatting(nb):
w = nb['worksheets']
node = w[0]
@ -13,6 +14,7 @@ def remove_formatting(nb):
del c[i]
return
def remove_links(nb):
w = nb['worksheets']
node = w[0]
@ -23,13 +25,30 @@ def remove_links(nb):
del c[i]
return
def remove_links_add_appendix(nb):
w = nb['worksheets']
node = w[0]
c = node['cells']
for i in range (len(c)):
if 'source' in c[i].keys():
if c[i]['source'][0:19] == '[Table of Contents]':
c[i]['source'] = '\\appendix'
return
def merge_notebooks(filenames):
merged = None
added_appendix = False
for fname in filenames:
with io.open(fname, 'r', encoding='utf-8') as f:
nb = current.read(f, u'json')
remove_formatting(nb)
remove_links(nb)
if not added_appendix and fname[0:8] == 'Appendix':
remove_links_add_appendix(nb)
added_appendix = True
else:
remove_links(nb)
if merged is None:
merged = nb
else:

View File

@ -19,4 +19,5 @@
((* block docclass *))
\documentclass[4pt]{book}
\setcounter{chapter}{-1}
((* endblock docclass *))

11
short_build_book Executable file
View File

@ -0,0 +1,11 @@
#! /bin/bash
echo "merging book..."
python short_merge_book.py > short.ipynb
echo "creating pdf..."
ipython nbconvert --to latex --template book --post PDF short.ipynb
echo "done."

65
short_merge_book.py Normal file
View File

@ -0,0 +1,65 @@
from __future__ import print_function
import io
from IPython.nbformat import current
import sys
def remove_formatting(nb):
w = nb['worksheets']
node = w[0]
c = node['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]
return
def remove_links(nb):
w = nb['worksheets']
node = w[0]
c = node['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']
for i in range (len(c)):
if 'source' in c[i].keys():
if c[i]['source'][0:19] == '[Table of Contents]':
c[i]['source'] = '\\appendix'
return
def merge_notebooks(filenames):
merged = None
added_appendix = False
for fname in filenames:
with io.open(fname, 'r', encoding='utf-8') as f:
nb = current.read(f, u'json')
remove_formatting(nb)
if not added_appendix and fname[0:8] == 'Appendix':
remove_links_add_appendix(nb)
added_appendix = True
else:
remove_links(nb)
if merged is None:
merged = nb
else:
merged.worksheets[0].cells.extend(nb.worksheets[0].cells)
merged.metadata.name += "_merged"
print(current.writes(merged, u'json'))
if __name__ == '__main__':
#merge_notebooks(sys.argv[1:])
merge_notebooks(
['Preface.ipynb',
'01_gh_filter/g-h_filter.ipynb',
'02_Discrete_Bayes/discrete_bayes.ipynb',
'Appendix_A_Installation/Appendix_Installation.ipynb',
'Appendix_B_Symbols_and_Notations/Appendix_Symbols_and_Notations.ipynb'])