Skip to content

Commit

Permalink
Add adjustbox to change width, height and scale
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-burridge-cfh committed Feb 25, 2022
1 parent d6386d9 commit 2ed2502
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ plantuml_latex_output_format
:eps: generate .eps (not supported by `pdflatex`)
:pdf: generate .eps and convert it to .pdf (requires `epstopdf`)
:png: generate .png
:tikz: generate .latex in the TikZ format
:none: do not generate any images (ignore uml directive)

Because embedded png looks pretty bad, it is recommended to choose `pdf`
Expand Down
44 changes: 40 additions & 4 deletions sphinxcontrib/plantuml.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,36 @@ def _lookup_latex_format(fmt):
% (', '.join(map(repr, _KNOWN_LATEX_FORMATS)), fmt))


def _latex_adjust_box_options(self, node):
adjust_box_options = []
if 'width' in node:
if 'scale' in node:
w = self.latex_image_length(node['width'], node['scale'])
else:
w = self.latex_image_length(node['width'])
if w:
adjust_box_options.append('width=%s' % w)
if 'height' in node:
if 'scale' in node:
h = self.latex_image_length(node['height'], node['scale'])
else:
h = self.latex_image_length(node['height'])
if h:
adjust_box_options.append('height=%s' % h)
if 'scale' in node:
if not adjust_box_options:
adjust_box_options.append('scale=%s'
% (float(node['scale']) / 100.0))
return adjust_box_options


def _latex_add_package(self, package):
# TODO: Currently modifying the preamble to add a package, there may be a cleaner solution
package = '\\usepackage{%s}' % (package,)
if package not in self.elements['preamble']:
self.elements['preamble'] += package + '\n'


def latex_visit_plantuml(self, node):
_render_batches_on_vist(self)
if 'latex_format' in node:
Expand All @@ -572,12 +602,18 @@ def latex_visit_plantuml(self, node):
raise nodes.SkipNode

if fmt == 'tikz':
package = '\\usepackage{tikz}'
if package not in self.elements['preamble']:
self.elements['preamble'] += package + '\n'
adjust_box_options = _latex_adjust_box_options(self, node)
_latex_add_package(self, 'tikz')

base, ext = os.path.splitext(refname)

self.body.append('\\input{{%s}%s}' % (base, ext))
input_macro = '\\input{{%s}%s}' % (base, ext)
if adjust_box_options:
_latex_add_package(self, 'adjustbox')
options = ','.join(adjust_box_options)
self.body.append('\\adjustbox{%s}{%s}' % (options, input_macro))
else:
self.body.append(input_macro)
else:
# put node representing rendered image
img_node = nodes.image(uri=refname, **node.attributes)
Expand Down
41 changes: 40 additions & 1 deletion tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,53 @@ def test_buildlatex_simple_with_tikz():
"""
files = glob.glob(os.path.join(_outdir, 'plantuml-*.latex'))
assert len(files) == 1
assert re.search(br'\\(sphinx)?input\{+plantuml-',
assert re.search(br'\\input\{+plantuml-',
readfile('plantuml_fixture.tex'))

content = readfile(files[0]).splitlines()
assert b'-tlatex:nopreamble' in content[0]
assert content[1][2:] == b'Hello'


@with_runsphinx('latex', plantuml_latex_output_format='tikz')
def test_buildlatex_simple_scale_with_tikz():
"""Generate simple LaTeX with TikZ
.. uml::
:scale: 20%
Hello
"""
assert re.search(br'\\adjustbox\{scale=0.2\}\{\\input\{+plantuml-',
readfile('plantuml_fixture.tex'))


@with_runsphinx('latex', plantuml_latex_output_format='tikz')
def test_buildlatex_simple_width_with_tikz():
"""Generate simple LaTeX with TikZ
.. uml::
:width: 50mm
Hello
"""
assert re.search(br'\\adjustbox\{width=50mm\}\{\\input\{+plantuml-',
readfile('plantuml_fixture.tex'))


@with_runsphinx('latex', plantuml_latex_output_format='tikz')
def test_buildlatex_simple_height_with_tikz():
"""Generate simple LaTeX with TikZ
.. uml::
:height: 50mm
Hello
"""
assert re.search(br'\\adjustbox\{height=50mm\}\{\\input\{+plantuml-',
readfile('plantuml_fixture.tex'))


@with_runsphinx('latex', plantuml_latex_output_format='pdf')
def test_buildlatex_simple_with_pdf():
"""Generate simple LaTeX with PDF
Expand Down

0 comments on commit 2ed2502

Please sign in to comment.