Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Hacking sphinx for jupyterbook-latex
from sphinx.writers.latex import LaTeXTranslator
from docutils import nodes, writers
from docutils.nodes import Element, Node, Text
from import captioned_literal_block
CR = '\n'
class MyTranslator(LaTeXTranslator):
def visit_literal_block(self, node: Element) -> None:
if node.rawsource != node.astext():
# most probably a parsed-literal block -- don't highlight
self.in_parsed_literal += 1
self.body.append(r'\begin{sphinxalltt}' + CR)
labels = self.hypertarget_to(node)
if isinstance(node.parent, captioned_literal_block):
labels += self.hypertarget_to(node.parent)
if labels and not self.in_footnote:
self.body.append(CR + r'\def\sphinxLiteralBlockLabel{' + labels + '}')
lang = node.get('language', 'default')
linenos = node.get('linenos', False)
highlight_args = node.get('highlight_args', {})
highlight_args['force'] = node.get('force', False)
opts = self.config.highlight_options.get(lang, {})
hlcode = self.highlighter.highlight_block(
node.rawsource, lang, opts=opts, linenos=linenos,
location=node, **highlight_args
if self.in_footnote:
self.body.append(CR + r'\sphinxSetupCodeBlockInFootnote')
hlcode = hlcode.replace(r'\begin{Verbatim}',
# if in table raise verbatim flag to avoid "tabulary" environment
# and opt for sphinxVerbatimintable to handle caption & long lines
elif self.table:
self.table.has_problematic = True
self.table.has_verbatim = True
hlcode = hlcode.replace(r'\begin{Verbatim}',
elif "output" in node['classes']:
hlcode = hlcode.replace(r'\begin{Verbatim}',
elif "input" in node['classes']:
hlcode = hlcode.replace(r'\begin{Verbatim}',
hlcode = hlcode.replace(r'\begin{Verbatim}',
# hlcode += ''.join(node["classes"])
# get consistent trailer
hlcode = hlcode.rstrip()[:-14] # strip \end{Verbatim}
if self.table and not self.in_footnote:
hlcode += r'\end{sphinxVerbatimintable}'
elif "output" in node["classes"]:
hlcode += r'\end{sphinxVerbatimOutput}'
hlcode += r'\end{sphinxVerbatim}'
hllines = str(highlight_args.get('hl_lines', []))[1:-1]
if hllines:
self.body.append(CR + r'\fvset{hllines={, %s,}}%%' % hllines)
self.body.append(CR + hlcode + CR)
if hllines:
self.body.append(r'\sphinxresetverbatimhllines' + CR)
raise nodes.SkipNode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment