plasTeX 3.0 — A Python Framework for Processing LaTeX Documents

6.8.3 Renderer plugin

For our final example, we build a plugin which brings a new renderer. We will use as an example a renderer targetting Markdown. This is slightly silly since markdown itself is a source code format, and much cruder that LaTeX, but this is only an example.

\begin{forest} 
  for tree={
    font=\ttfamily,
    grow'=0,
    child anchor=west,
    parent anchor=south,
    anchor=west,
    calign=first,
    edge path={
      \noexpand\path [draw, \forestoption{edge}]
      (!u.south west) +(7.5pt,0) |- node[fill,inner sep=1.25pt] {} (.child anchor)\forestoption{edge label};
    },
    before typesetting nodes={
      if n=1
        {insert before={[,phantom]}}
        {}
    },
    fit=band,
    before computing xy={l=15pt},
  }
[my_renderer_plugin
[pyproject.toml]
[src
    [plastex_markdown_renderer
        [__init__.py]
        [Renderers
            [Markdown
                [__init__.py]
                [Math.jinja2s]
                [Sectioning.jinja2s]
                [Text.jinja2s]
                [Themes
                    [default
                        [default-layout.jinja2]
                    ]
                ]
            ]
        ]]]]
\end{forest}

The ‘__init__.py’ in ‘plastex_markdown_renderer’ is empty but the one in ‘Markdown’ contains

from plasTeX.Renderers.PageTemplate import Renderer as PTRenderer

class Renderer(PTRenderer):
    """Renderer targetting Markdown using templates."""
    fileExtension = '.md'

which simply declares a page template based renderer which will produce files with extension ‘.md’. The various files with ‘.jinja2s’ extension contain the following code split into three files for convenience:

name: math displaymath
{{ obj.mathjax_source }}

name: document
{{ obj }}

name: section
# {{ obj.fullTitle }}
{{ obj }}

name: subsection
## {{ obj.fullTitle }}
{{ obj }}

name: par
{{ obj }}

name: emph em
*{{ obj }}*

name: itshape textit
*{{ obj }}*

name: bfseries textbf
**{{ obj }}**

name: ttfamily texttt
`{{ obj }}`

and the file ‘default-layout.jinja2’ simply contains one line saying {{ obj }}. This will allow to run

plastex --plugins  plastex_markdown_renderer --renderer Markdown test.tex

to render a tiny subset of LaTeX to markdown.