From 5dd36a96de6da84708f0dcaeb8592932de943e18 Mon Sep 17 00:00:00 2001 From: milde Date: Mon, 6 May 2013 11:26:00 +0000 Subject: [PATCH] Writer comment with (currently ignored) figure alignment to the LaTeX source. git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk/docutils@7658 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- docutils/writers/latex2e/__init__.py | 34 +++++++++++------------ test/functional/expected/standalone_rst_latex.tex | 4 +-- test/functional/expected/standalone_rst_xetex.tex | 4 +-- 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/docutils/writers/latex2e/__init__.py b/docutils/writers/latex2e/__init__.py index 9b40ddf22..88c5eac45 100644 --- a/docutils/writers/latex2e/__init__.py +++ b/docutils/writers/latex2e/__init__.py @@ -1528,7 +1528,7 @@ class LaTeXTranslator(nodes.NodeVisitor): ## return head + '\n' + body def is_inline(self, node): - """Check whether a node represents an inline element""" + """Check whether a node represents an inline or block-level element""" return isinstance(node.parent, nodes.TextElement) def append_hypertargets(self, node): @@ -1656,7 +1656,7 @@ class LaTeXTranslator(nodes.NodeVisitor): self.out.append('}') def visit_caption(self, node): - self.out.append( '\\caption{' ) + self.out.append('\n\\caption{') def depart_caption(self, node): self.out.append('}\n') @@ -2131,21 +2131,18 @@ class LaTeXTranslator(nodes.NodeVisitor): def visit_figure(self, node): self.requirements['float_settings'] = PreambleCmds.float_settings - # ! the 'align' attribute should set "outer alignment" ! - # For "inner alignment" use LaTeX default alignment (similar to HTML) - ## if ('align' not in node.attributes or - ## node.attributes['align'] == 'center'): - ## align = '\n\\centering' - ## align_end = '' - ## else: - ## # TODO non vertical space for other alignments. - ## align = '\\begin{flush%s}' % node.attributes['align'] - ## align_end = '\\end{flush%s}' % node.attributes['align'] - ## self.out.append( '\\begin{figure}%s\n' % align ) - ## self.context.append( '%s\\end{figure}\n' % align_end ) - self.out.append('\\begin{figure}') + # The 'align' attribute sets the "outer alignment", + # for "inner alignment" use LaTeX default alignment (similar to HTML) + alignment = node.attributes.get('align', 'center') + if alignment != 'center': + # The LaTeX "figure" environment always uses the full textwidth, + # so "outer alignment" is ignored. Just write a comment. + # TODO: use the wrapfigure environment? + self.out.append('\\begin{figure} %% align = "%s"\n' % alignment) + else: + self.out.append('\\begin{figure}\n') if node.get('ids'): - self.out += ['\n'] + self.ids_to_labels(node) + self.out += self.ids_to_labels(node) + ['\n'] def depart_figure(self, node): self.out.append('\\end{figure}\n') @@ -2299,7 +2296,6 @@ class LaTeXTranslator(nodes.NodeVisitor): pre = [] post = [] include_graphics_options = [] - display_style = ('block-', 'inline-')[self.is_inline(node)] align_codes = { # inline images: by default latex aligns the bottom. 'bottom': ('', ''), @@ -2310,6 +2306,7 @@ class LaTeXTranslator(nodes.NodeVisitor): 'left': (r'\noindent{', r'\hfill}'), 'right': (r'\noindent{\hfill', '}'),} if 'align' in attrs: + # TODO: warn or ignore non-applicable alignment settings? try: align_code = align_codes[attrs['align']] pre.append(align_code[0]) @@ -2325,7 +2322,8 @@ class LaTeXTranslator(nodes.NodeVisitor): if 'width' in attrs: include_graphics_options.append('width=%s' % self.to_latex_length(attrs['width'])) - if not self.is_inline(node): + if not (self.is_inline(node) or + isinstance(node.parent, nodes.figure)): pre.append('\n') post.append('\n') pre.reverse() diff --git a/test/functional/expected/standalone_rst_latex.tex b/test/functional/expected/standalone_rst_latex.tex index bdb68babd..f3b59ddbd 100644 --- a/test/functional/expected/standalone_rst_latex.tex +++ b/test/functional/expected/standalone_rst_latex.tex @@ -970,7 +970,7 @@ This paragraph is also part of the legend. \end{figure} A left-aligned figure: -\begin{figure} +\begin{figure} % align = "left" \noindent\makebox[\textwidth][c]{\includegraphics[width=40px]{../../../docs/user/rst/images/biohazard.png}} \caption{This is the caption.} \begin{DUlegend} @@ -1002,7 +1002,7 @@ The specific behavior depends upon the style sheet and the browser or rendering software used. A right-aligned figure: -\begin{figure} +\begin{figure} % align = "right" \noindent\makebox[\textwidth][c]{\includegraphics[width=40px]{../../../docs/user/rst/images/biohazard.png}} \caption{This is the caption.} \begin{DUlegend} diff --git a/test/functional/expected/standalone_rst_xetex.tex b/test/functional/expected/standalone_rst_xetex.tex index fc9a4768e..86a496d16 100644 --- a/test/functional/expected/standalone_rst_xetex.tex +++ b/test/functional/expected/standalone_rst_xetex.tex @@ -969,7 +969,7 @@ This paragraph is also part of the legend. \end{figure} A left-aligned figure: -\begin{figure} +\begin{figure} % align = "left" \noindent\makebox[\textwidth][c]{\includegraphics[width=40\pdfpxdimen]{../../../docs/user/rst/images/biohazard.png}} \caption{This is the caption.} \begin{DUlegend} @@ -1001,7 +1001,7 @@ The specific behavior depends upon the style sheet and the browser or rendering software used. A right-aligned figure: -\begin{figure} +\begin{figure} % align = "right" \noindent\makebox[\textwidth][c]{\includegraphics[width=40\pdfpxdimen]{../../../docs/user/rst/images/biohazard.png}} \caption{This is the caption.} \begin{DUlegend} -- 2.11.4.GIT