From 1d3685fa1c8839494b791dddfdce659a2168dc98 2013-09-23 17:57:19 From: Jonathan Frederic Date: 2013-09-23 17:57:19 Subject: [PATCH] Add strip_verbatim to latex highlight --- diff --git a/IPython/nbconvert/filters/highlight.py b/IPython/nbconvert/filters/highlight.py index 78cb9ea..b390e6c 100644 --- a/IPython/nbconvert/filters/highlight.py +++ b/IPython/nbconvert/filters/highlight.py @@ -37,6 +37,7 @@ __all__ = [ 'highlight2latex' ] + def highlight2html(source, language='ipython', metadata=None): """ Return a syntax-highlighted version of the input source as html output. @@ -44,30 +45,37 @@ def highlight2html(source, language='ipython', metadata=None): Parameters ---------- source : str - source of the cell to highlight. + source of the cell to highlight language : str - Language to highlight the syntax of. + language to highlight the syntax of metadata : NotebookNode cell metadata - metadata of the cell to highlight. + metadata of the cell to highlight """ return _pygment_highlight(source, HtmlFormatter(), language, metadata) -def highlight2latex(source, language='ipython', metadata=None): +def highlight2latex(source, language='ipython', metadata=None, strip_verbatim=False): """ Return a syntax-highlighted version of the input source as latex output. Parameters ---------- source : str - source of the cell to highlight. + source of the cell to highlight language : str - Language to highlight the syntax of. + language to highlight the syntax of metadata : NotebookNode cell metadata - metadata of the cell to highlight. + metadata of the cell to highlight + strip_verbatim : bool + remove the Verbatim environment that pygments provides by default """ - return _pygment_highlight(source, LatexFormatter(), language, metadata) + latex = _pygment_highlight(source, LatexFormatter(), language, metadata) + if strip_verbatim: + latex = latex.replace(r'\begin{Verbatim}[commandchars=\\\{\}]' + '\n', '') + return latex.replace('\n\\end{Verbatim}\n', '') + else: + return latex @@ -78,12 +86,12 @@ def _pygment_highlight(source, output_formatter, language='ipython', metadata=No Parameters ---------- source : str - source of the cell to highlight. + source of the cell to highlight output_formatter : Pygments formatter language : str - Language to highlight the syntax of. + language to highlight the syntax of metadata : NotebookNode cell metadata - metadata of the cell to highlight. + metadata of the cell to highlight """ # If the cell uses a magic extension language, diff --git a/IPython/nbconvert/templates/latex/python_style.tplx b/IPython/nbconvert/templates/latex/python_style.tplx index d718c11..397fb96 100644 --- a/IPython/nbconvert/templates/latex/python_style.tplx +++ b/IPython/nbconvert/templates/latex/python_style.tplx @@ -16,6 +16,6 @@ ((* block input scoped *)) \begin{Verbatim}[commandchars=\\\{\}] -((( cell.input | highlight2latex | replace('\\begin{Verbatim}[commandchars=\\\\\\{\\}]\n', '') | replace('\n\\end{Verbatim}\n', '') | add_prompts ))) +((( cell.input | highlight2latex(strip_verbatim=True) | add_prompts ))) \end{Verbatim} ((* endblock input *))