Skip to content

Commit

Permalink
fix(latex-renderer): rendering of package options
Browse files Browse the repository at this point in the history
Rendering package options with string delimiters leads to LaTeX errors,
e.g., when `render_strikethrough` activates the 'ulem' package with
option 'normalem', the rendered LaTeX should read
`\usepackage[normalem]{ulem}` rather than
`\usepackage['normalem']{ulem}` as it previously did. This patch fixes
that.

NB: Too keep the implementation simple (and as it has not shown adverse
side-effects when using pdflatex), the generated LaTeX for an added
package listings without options is now \usepackage[]{listings} rather
than \usepackage{listings}.
  • Loading branch information
choeppler committed Oct 1, 2024
1 parent a40cd2f commit 2769252
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
4 changes: 2 additions & 2 deletions mistletoe/latex_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ def render_line_break(token):
return '\n' if token.soft else '\\newline\n'

def render_packages(self):
pattern = '\\usepackage{options}{{{package}}}\n'
return ''.join(pattern.format(options=options or '', package=package)
pattern = '\\usepackage[{options}]{{{package}}}\n'
return ''.join(pattern.format(options=', '.join(options) or '', package=package)
for package, options in self.packages.items())

def render_document(self, token):
Expand Down
13 changes: 10 additions & 3 deletions test/test_latex_renderer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from unittest import TestCase, mock
from parameterized import parameterized
import mistletoe.latex_renderer
from mistletoe.latex_renderer import LaTeXRenderer
from mistletoe import markdown

Expand Down Expand Up @@ -132,6 +131,14 @@ def test_document(self):
'\\end{document}\n')
self._test_token('Document', expected, footnotes={})

def test_packages(self):
self.renderer.packages = {
'ulem': ['normalem'],
'hyperref': [],
}
self.assertEqual(self.renderer.render_packages(),
'\\usepackage[normalem]{ulem}\n' +
'\\usepackage[]{hyperref}\n')

class TestHtmlEntity(TestCase):
def test_html_entity(self):
Expand All @@ -151,7 +158,7 @@ def test_footnote_image(self):
from mistletoe import Document
raw = ['![alt][foo]\n', '\n', '[foo]: bar "title"\n']
expected = ('\\documentclass{article}\n'
'\\usepackage{graphicx}\n'
'\\usepackage[]{graphicx}\n'
'\\begin{document}\n'
'\n'
'\n\\includegraphics{bar}\n'
Expand All @@ -163,7 +170,7 @@ def test_footnote_link(self):
from mistletoe import Document
raw = ['[name][key]\n', '\n', '[key]: target\n']
expected = ('\\documentclass{article}\n'
'\\usepackage{hyperref}\n'
'\\usepackage[]{hyperref}\n'
'\\begin{document}\n'
'\n'
'\\href{target}{name}'
Expand Down

0 comments on commit 2769252

Please sign in to comment.