diff --git a/README.rst b/README.rst index 461509e..2fdb69c 100644 --- a/README.rst +++ b/README.rst @@ -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` diff --git a/sphinxcontrib/plantuml.py b/sphinxcontrib/plantuml.py index a20481a..7766494 100644 --- a/sphinxcontrib/plantuml.py +++ b/sphinxcontrib/plantuml.py @@ -555,6 +555,36 @@ def _lookup_latex_format(fmt): % (', '.join(map(repr, _KNOWN_LATEX_FORMATS)), fmt)) +def _latex_adjustbox_options(self, node): + adjustbox_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: + adjustbox_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: + adjustbox_options.append('height=%s' % h) + if 'scale' in node: + if not adjustbox_options: + adjustbox_options.append('scale=%s' + % (float(node['scale']) / 100.0)) + return adjustbox_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: @@ -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' + _latex_add_package(self, 'tikz') + base, ext = os.path.splitext(refname) + input_macro = '\\input{{%s}%s}' % (base, ext) - self.body.append('\\input{{%s}%s}' % (base, ext)) + adjustbox_options = _latex_adjustbox_options(self, node) + if adjustbox_options: + _latex_add_package(self, 'adjustbox') + options = ','.join(adjustbox_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) diff --git a/tests/test_functional.py b/tests/test_functional.py index 587091e..77cce9b 100644 --- a/tests/test_functional.py +++ b/tests/test_functional.py @@ -262,7 +262,7 @@ 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() @@ -270,6 +270,45 @@ def test_buildlatex_simple_with_tikz(): 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