summaryrefslogtreecommitdiff
path: root/docutils
diff options
context:
space:
mode:
authorgrubert <grubert@929543f6-e4f2-0310-98a6-ba3bd3dd1d04>2006-11-02 12:30:40 +0000
committergrubert <grubert@929543f6-e4f2-0310-98a6-ba3bd3dd1d04>2006-11-02 12:30:40 +0000
commitae73e65d3df75f6be9feaa99f66bfdddbe6e8767 (patch)
treec808eb3c0672533973bdc3a72304d508e3b2af54 /docutils
parent696d4f8e102c7da7d35d893e46c12125a9bd9ead (diff)
downloaddocutils-ae73e65d3df75f6be9feaa99f66bfdddbe6e8767.tar.gz
- Image width unit ``px`` is translated to ``pt``:
- Add image height support. - Fix: image width ``70%`` is converted ``0.700\linewidth``. bug #1457388 git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk@4797 929543f6-e4f2-0310-98a6-ba3bd3dd1d04
Diffstat (limited to 'docutils')
-rw-r--r--docutils/HISTORY.txt4
-rw-r--r--docutils/docutils/writers/latex2e/__init__.py27
-rw-r--r--docutils/test/functional/expected/standalone_rst_latex.tex6
3 files changed, 31 insertions, 6 deletions
diff --git a/docutils/HISTORY.txt b/docutils/HISTORY.txt
index 22fadf854..7358f487e 100644
--- a/docutils/HISTORY.txt
+++ b/docutils/HISTORY.txt
@@ -82,6 +82,10 @@ Changes Since 0.4
* docutils/writers/latex2e/__init__.py:
+ - Image width unit ``px`` is translated to ``pt``:
+ - Add image height support.
+ - Fix: image width ``70%`` is converted ``0.700\linewidth``.
+ bug #1457388
- Fix: Do not escape underscores in citation reference labels if
use-latex-citations is set.
- Use centering instead of center for figure contents, to avoid vertical
diff --git a/docutils/docutils/writers/latex2e/__init__.py b/docutils/docutils/writers/latex2e/__init__.py
index 4432cd377..6619e8f0c 100644
--- a/docutils/docutils/writers/latex2e/__init__.py
+++ b/docutils/docutils/writers/latex2e/__init__.py
@@ -1515,6 +1515,20 @@ class LaTeXTranslator(nodes.NodeVisitor):
def depart_hint(self, node):
self.depart_admonition()
+ def latex_image_length(self, width_str):
+ 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 unit == "px":
+ # LaTeX does not know pixels but points
+ res = "%spt" % amount
+ elif unit == "%":
+ res = "%.3f\\linewidth" % (float(amount)/100.0)
+ return res
+
def visit_image(self, node):
attrs = node.attributes
# Add image URI to dependency list, assuming that it's
@@ -1522,7 +1536,7 @@ class LaTeXTranslator(nodes.NodeVisitor):
self.settings.record_dependencies.add(attrs['uri'])
pre = [] # in reverse order
post = []
- include_graphics_options = ""
+ include_graphics_options = []
inline = isinstance(node.parent, nodes.TextElement)
if attrs.has_key('scale'):
# Could also be done with ``scale`` option to
@@ -1530,7 +1544,11 @@ class LaTeXTranslator(nodes.NodeVisitor):
pre.append('\\scalebox{%f}{' % (attrs['scale'] / 100.0,))
post.append('}')
if attrs.has_key('width'):
- include_graphics_options = '[width=%s]' % attrs['width']
+ include_graphics_options.append('width=%s' % (
+ self.latex_image_length(attrs['width']), ))
+ if attrs.has_key('height'):
+ include_graphics_options.append('height=%s' % (
+ self.latex_image_length(attrs['height']), ))
if attrs.has_key('align'):
align_prepost = {
# By default latex aligns the top of an image.
@@ -1553,8 +1571,11 @@ class LaTeXTranslator(nodes.NodeVisitor):
post.append('\n')
pre.reverse()
self.body.extend( pre )
+ options = ''
+ if len(include_graphics_options)>0:
+ options = '[%s]' % (','.join(include_graphics_options))
self.body.append( '\\includegraphics%s{%s}' % (
- include_graphics_options, attrs['uri'] ) )
+ options, attrs['uri'] ) )
self.body.extend( post )
def depart_image(self, node):
diff --git a/docutils/test/functional/expected/standalone_rst_latex.tex b/docutils/test/functional/expected/standalone_rst_latex.tex
index db0fa90c6..efcbb0d8a 100644
--- a/docutils/test/functional/expected/standalone_rst_latex.tex
+++ b/docutils/test/functional/expected/standalone_rst_latex.tex
@@ -959,15 +959,15 @@ An image 2 em wide:
An image 2 em wide and 30 pixel high:
-\includegraphics[width=2em]{../../../docs/user/rst/images/biohazard.png}
+\includegraphics[width=2em,height=30pt]{../../../docs/user/rst/images/biohazard.png}
An image occupying 70{\%} of the line width:
-\includegraphics[width=70%]{../../../docs/user/rst/images/biohazard.png}
+\includegraphics[width=0.700\linewidth]{../../../docs/user/rst/images/biohazard.png}
An image 3 cm high:
-\includegraphics{../../../docs/user/rst/images/biohazard.png}
+\includegraphics[height=3cm]{../../../docs/user/rst/images/biohazard.png}
%___________________________________________________________________________