Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Tikz support to latex formats #63

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
58 changes: 52 additions & 6 deletions sphinxcontrib/plantuml.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ def _split_cmdargs(args):
'png': [],
'svg': ['-tsvg'],
'txt': ['-ttxt'],
'latex': ['-tlatex:nopreamble'],
}


Expand Down Expand Up @@ -541,6 +542,7 @@ def _convert_eps_to_pdf(self, refname, fname):
'eps': ('eps', lambda self, refname, fname: (refname, fname)),
'pdf': ('eps', _convert_eps_to_pdf),
'png': ('png', lambda self, refname, fname: (refname, fname)),
'tikz': ('latex', lambda self, refname, fname: (refname, fname)),
}


Expand All @@ -553,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:
Expand All @@ -569,12 +601,26 @@ def latex_visit_plantuml(self, node):
logger.warning(str(err))
raise nodes.SkipNode

# put node representing rendered image
img_node = nodes.image(uri=refname, **node.attributes)
img_node.delattr('uml')
if not img_node.hasattr('alt'):
img_node['alt'] = node['uml']
node.append(img_node)
if fmt == 'tikz':
_latex_add_package(self, 'tikz')

base, ext = os.path.splitext(refname)
input_macro = '\\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)
img_node.delattr('uml')
if not img_node.hasattr('alt'):
img_node['alt'] = node['uml']
node.append(img_node)


def latex_depart_plantuml(self, node):
Expand Down
57 changes: 57 additions & 0 deletions tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,63 @@ def test_buildlatex_simple_with_eps():
assert content[1][2:] == b'Hello'


@with_runsphinx('latex', plantuml_latex_output_format='tikz')
def test_buildlatex_simple_with_tikz():
"""Generate simple LaTeX with TikZ

.. uml::

Hello
"""
files = glob.glob(os.path.join(_outdir, 'plantuml-*.latex'))
assert len(files) == 1
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