From 15702d63fe6f2d63f445f235f788e26f2f43d46d 2013-06-08 20:58:30 From: jakobgager Date: 2013-06-08 20:58:30 Subject: [PATCH] Small latex mods: Escapes, Headings, Equations * remove console escapes * use different heading levels * allow explicit definition of latex environments --- diff --git a/nbconvert/exporters/exporter.py b/nbconvert/exporters/exporter.py index 66e5cd5..35b0492 100755 --- a/nbconvert/exporters/exporter.py +++ b/nbconvert/exporters/exporter.py @@ -66,6 +66,7 @@ default_filters = { 'rm_ansi': nbconvert.filters.ansi.remove_ansi, 'rm_dollars': nbconvert.filters.strings.strip_dollars, 'rm_fake': nbconvert.filters.strings.rm_fake, + 'rm_consoleesc': nbconvert.filters.strings.rm_consoleesc, 'rm_math_space': nbconvert.filters.latex.rm_math_space, 'wrap': nbconvert.filters.strings.wrap } diff --git a/nbconvert/filters/latex.py b/nbconvert/filters/latex.py index 06abd4c..82cbad6 100755 --- a/nbconvert/filters/latex.py +++ b/nbconvert/filters/latex.py @@ -21,6 +21,7 @@ import re #Latex substitutions for escaping latex. LATEX_SUBS = ( + (re.compile('\033\[[0-9;]+m'),''), # handle console escapes (re.compile(r'\\'), r'\\textbackslash'), (re.compile(r'([{}_#%&$])'), r'\\\1'), (re.compile(r'~'), r'\~{}'), diff --git a/nbconvert/filters/strings.py b/nbconvert/filters/strings.py index 6e5c289..7bad530 100755 --- a/nbconvert/filters/strings.py +++ b/nbconvert/filters/strings.py @@ -14,6 +14,7 @@ templates. #----------------------------------------------------------------------------- # Imports #----------------------------------------------------------------------------- +import re # Our own imports import textwrap @@ -54,6 +55,18 @@ def strip_dollars(text): return text.strip('$') +def rm_consoleesc(text): + """ + Remove console escapes from text + + Parameters + ---------- + text : str + Text to remove '/files/' from + """ + r= re.compile("\033\[[0-9;]+m") + return r.sub('', text) + def rm_fake(text): """ Remove all occurrences of '/files/' from text diff --git a/nbconvert/templates/latex/base.tplx b/nbconvert/templates/latex/base.tplx index 1a1e3aa..25cd15d 100644 --- a/nbconvert/templates/latex/base.tplx +++ b/nbconvert/templates/latex/base.tplx @@ -71,22 +71,38 @@ it introduces a new line ((* endblock *)) ((* block data_latex -*)) -\begin{equation*} -((( output.latex | rm_dollars))) -\end{equation*} +((*- if output.latex.startswith('$'): -*)) \begin{equation*} + ((( output.latex | rm_dollars))) + \end{equation*} +((*- else -*)) ((( output.latex ))) ((*- endif *)) ((* endblock *)) ((* block stream *)) \begin{verbatim} -((( output.text))) +((( output.text | rm_consoleesc))) \end{verbatim} ((* endblock stream *)) ((* block markdowncell scoped *))((( cell.source | markdown2latex ))) ((* endblock markdowncell *)) -((* block headingcell scoped *)) -\section{((( cell.source | markdown2latex)))} +((* block headingcell scoped -*)) + \ + ((*- if cell.level == 1 -*)) + ((* block h1 -*))section((* endblock h1 -*)) + ((*- elif cell.level == 2 -*)) + ((* block h2 -*))subsection((* endblock h2 -*)) + ((*- elif cell.level == 3 -*)) + ((* block h3 -*))subsubsection((* endblock h3 -*)) + ((*- elif cell.level == 4 -*)) + ((* block h4 -*))paragraph((* endblock h4 -*)) + ((*- elif cell.level == 5 -*)) + ((* block h5 -*))subparagraph((* endblock h5 -*)) + ((*- elif cell.level == 6 -*)) + ((* block h6 -*))subparagraph((* endblock h6 -*)) + ((= 6th level not available in standard latex =)) + + ((*- endif -*)){((( cell.source | markdown2latex )))} ((* endblock headingcell *)) ((* block rawcell scoped *))