Skip to content

Commit

Permalink
pass max width to latex adjustbox
Browse files Browse the repository at this point in the history
Problem:
When creating large diagrams using plantuml, they can quickly get too wide for a
pdf page.  For images in the png, pdf and svg format, this problem is handled by
the sphinx-provided "sphinxincludegraphics" latex command, which resizes the
included graphic if needed.  As tikz images are embedded using the "\input"
command, this resize logic is not applicable for them.

Solution:
I would suggest to use the "max-width" option (which is already somehow
implemented but not documented) to allow the user to specify a maximum width.
The "adjustbox" macro, which is already being used for giving an absolute size
for an image, also supports this type of maximum size.  This way, the image is
not resized if it fits, but only if it would exceed the given size.
  • Loading branch information
Valentin Heinisch authored and yuja committed Mar 6, 2024
1 parent 913c6ef commit 7ef52e4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
9 changes: 8 additions & 1 deletion sphinxcontrib/plantuml.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ def _lookup_latex_format(fmt):
)


def _latex_adjustbox_options(self, node):
def _latex_adjustbox_options(self, node): # noqa: C901
adjustbox_options = []
if 'width' in node:
if 'scale' in node:
Expand All @@ -651,6 +651,13 @@ def _latex_adjustbox_options(self, node):
if 'scale' in node:
if not adjustbox_options:
adjustbox_options.append('scale=%s' % (float(node['scale']) / 100.0))
if 'max-width' in node:
if 'scale' in node:
w = self.latex_image_length(node['max-width'], node['scale'])
else:
w = self.latex_image_length(node['max-width'])
if w:
adjustbox_options.append('max width=%s' % w)
return adjustbox_options


Expand Down
13 changes: 13 additions & 0 deletions tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,19 @@ def test_buildlatex_simple_height_with_tikz():
readfile('plantuml_fixture.tex'))


@with_runsphinx('latex', plantuml_latex_output_format='tikz')
def test_buildlatex_simple_max_width_with_tikz():
"""Generate simple LaTeX with TikZ
.. uml::
:max-width: 50mm
Hello
"""
assert re.search(br'\\adjustbox\{max width=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 7ef52e4

Please sign in to comment.