From 67fe3d6da0c045da35005dd03e817e3385879952 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20B?= Date: Wed, 2 Nov 2016 14:50:41 +0100 Subject: [PATCH] Fix #2865: let LaTeX image inclusion obey ``scale`` before textwidth fit (#3059) --- sphinx/texinputs/sphinx.sty | 30 +++++++++++++++++------------- sphinx/writers/latex.py | 16 +++++++++++----- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/sphinx/texinputs/sphinx.sty b/sphinx/texinputs/sphinx.sty index f6f677cc9..f49371b8f 100644 --- a/sphinx/texinputs/sphinx.sty +++ b/sphinx/texinputs/sphinx.sty @@ -867,23 +867,27 @@ \raggedright} {\end{list}} -% Re-define \includegraphics to resize images larger than the line width -% if the size is not specified. +% Redefine \includegraphics to resize images larger than the line width, +% except if height or width option present. +% +% If scale is present, rescale before fitting to line width. (since 1.5) +% % Warning: future version of Sphinx will not modify original \includegraphics, -% Below custom code will be direct definition of \sphinxincludegraphics, with -% \py@Oldincludegraphics replaced by direct use of original \includegraphics. +% below code will be definition only of \sphinxincludegraphics. \let\py@Oldincludegraphics\includegraphics \newbox\spx@image@box -\renewcommand*{\includegraphics}[2][\@empty]{% - \ifx\@empty #1% attention, #1 could be bb.., bad if first after \ifx - \setbox\spx@image@box=\hbox{\py@Oldincludegraphics{#2}}% - \ifdim \wd\spx@image@box>\linewidth - \py@Oldincludegraphics[width=\linewidth]{#2}% - \else - \leavevmode\box\spx@image@box - \fi - \else +\renewcommand*{\includegraphics}[2][]{% + \in@{height}{#1}\ifin@\else\in@{width}{#1}\fi + \ifin@ % height or width present \py@Oldincludegraphics[#1]{#2}% + \else % no height nor width (but #1 may be "scale=...") + \setbox\spx@image@box\hbox{\py@Oldincludegraphics[#1,draft]{#2}}% + \ifdim \wd\spx@image@box>\linewidth + \setbox\spx@image@box\box\voidb@x % clear memory + \py@Oldincludegraphics[#1,width=\linewidth]{#2}% + \else + \py@Oldincludegraphics[#1]{#2}% + \fi \fi } % Writer will put \sphinxincludegraphics in LaTeX source, and with this, diff --git a/sphinx/writers/latex.py b/sphinx/writers/latex.py index 4b2834d8e..d5895399d 100644 --- a/sphinx/writers/latex.py +++ b/sphinx/writers/latex.py @@ -1400,11 +1400,6 @@ class LaTeXTranslator(nodes.NodeVisitor): post = [] include_graphics_options = [] is_inline = self.is_inline(node) - if 'scale' in attrs: - # Could also be done with ``scale`` option to - # ``\includegraphics``; doing it this way for consistency. - pre.append('\\scalebox{%f}{' % (attrs['scale'] / 100.0,)) - post.append('}') if 'width' in attrs: w = self.latex_image_length(attrs['width']) if w: @@ -1413,6 +1408,17 @@ class LaTeXTranslator(nodes.NodeVisitor): h = self.latex_image_length(attrs['height']) if h: include_graphics_options.append('height=%s' % h) + if 'scale' in attrs: + if include_graphics_options: + # unfortunately passing "height=1cm,scale=2.0" to \includegraphics + # does not result in a height of 2cm. We must scale afterwards. + pre.append('\\scalebox{%f}{' % (attrs['scale'] / 100.0,)) + post.append('}') + else: + # if no "width" nor "height", \sphinxincludegraphics will fit + # to the available text width if oversized after rescaling. + include_graphics_options.append('scale=%s' + % (float(attrs['scale']) / 100.0)) if 'align' in attrs: align_prepost = { # By default latex aligns the top of an image.