LaTeX writer: Integerate width_to_latex_length() and latex_image_length() (ref: #2714)

This commit is contained in:
Takeshi KOMIYA 2016-06-26 11:28:54 +09:00
parent 45ddb6387c
commit 0859b338d8

View File

@ -265,24 +265,6 @@ class Table(object):
self.longtable = False
def width_to_latex_length(length_str):
"""Convert `length_str` with rst length to LaTeX length.
This function is copied from docutils' latex writer
"""
match = re.match('(\d*\.?\d*)\s*(\S*)', length_str)
if not match:
return length_str
value, unit = match.groups()[:2]
if unit in ('', 'pt'):
length_str = '%sbp' % value # convert to 'bp'
# percentage: relate to current line width
elif unit == '%':
length_str = '%.3f\\linewidth' % (float(value)/100.0)
return length_str
def escape_abbr(text):
"""Adjust spacing after abbreviations."""
return re.sub('\.(?=\s|$)', '.\\@', text)
@ -1352,13 +1334,22 @@ class LaTeXTranslator(nodes.NodeVisitor):
def depart_hlistcol(self, node):
pass
def latex_image_length(self, width_str):
def latex_image_length(self, width_str, figure=False):
"""Convert `width_str` with rst length to LaTeX length.
This function is copied from docutils' latex writer
The last parameter, ``figure`` is only for compatibility with 1.4.4.
It will be removed at Sphinx-1.5.
"""
match = re.match('(\d*\.?\d*)\s*(\S*)', width_str)
if not match:
# fallback
return width_str
res = width_str
amount, unit = match.groups()[:2]
if figure and unit in ('', 'pt'):
res = '%sbp' % amount # convert to 'bp'
if not unit or unit == "px":
# pixels: let LaTeX alone
return None
@ -1446,7 +1437,7 @@ class LaTeXTranslator(nodes.NodeVisitor):
if self.table:
# TODO: support align option
if 'width' in node:
length = width_to_latex_length(node['width'])
length = self.latex_image_length(node['width'], figure=True)
self.body.append('\\begin{sphinxfigure-in-table}[%s]\n\\centering\n' % length)
else:
self.body.append('\\begin{sphinxfigure-in-table}\n\\centering\n')
@ -1455,9 +1446,9 @@ class LaTeXTranslator(nodes.NodeVisitor):
self.context.append(ids + '\\end{sphinxfigure-in-table}\\relax\n')
elif node.get('align', '') in ('left', 'right'):
if 'width' in node:
length = width_to_latex_length(node['width'])
length = self.latex_image_length(node['width'], figure=True)
elif 'width' in node[0]:
length = width_to_latex_length(node[0]['width'])
length = self.latex_image_length(node[0]['width'], figure=True)
else:
length = '0pt'
self.body.append('\\begin{wrapfigure}{%s}{%s}\n\\centering' %