1%% GRAPHICS
2%
3% change this info string if making any custom modification
4\ProvidesFile{sphinxlatexgraphics.sty}[2021/01/27 graphics]
5
6% Provides support for this output mark-up from Sphinx latex writer:
7%
8% - macros:
9%
10%   - \sphinxfigcaption
11%   - \sphinxincludegraphics
12%
13% - environments:
14%
15%   - sphinxfigure-in-table
16%
17% May change:
18%
19% - \sphinxcaption (at begin document)
20%
21% Also provides:
22%
23% - \sphinxsafeincludegraphics (default of \sphinxincludegraphics since 2.0)
24% - \spx@image@maxheight dimension (used by sphinxlatexadmonitions.sty)
25% - \spx@image@box scratch box register (also used by sphinxlatexliterals.sty)
26%
27% Requires:
28% \RequirePackage{graphicx}% done in sphinx.sty
29\RequirePackage{amstext}% needed for \firstchoice@true(false)
30
31% \sphinxincludegraphics resizes images larger than the TeX \linewidth (which
32% is adjusted in indented environments), or taller than a certain maximal
33% height (usually \textheight and this is reduced in the environments which use
34% framed.sty to avoid infinite loop if image too tall).
35%
36% In case height or width options are present the rescaling is done
37% (since 2.0), in a way keeping the width:height ratio either native from
38% image or from the width and height options if both were present.
39%
40\newdimen\spx@image@maxheight
41\AtBeginDocument{\spx@image@maxheight\textheight}
42
43% box scratch register
44\newbox\spx@image@box
45\newcommand*{\sphinxsafeincludegraphics}[2][]{%
46    % #1 contains possibly width=, height=, but no scale= since 1.8.4
47    \setbox\spx@image@box\hbox{\includegraphics[#1,draft]{#2}}%
48    \in@false % use some handy boolean flag
49    \ifdim \wd\spx@image@box>\linewidth
50      \in@true % flag to remember to adjust options and set box dimensions
51      % compute height which results from rescaling width to \linewidth
52      % and keep current aspect ratio. multiply-divide in \numexpr uses
53      % temporarily doubled precision, hence no overflow. (of course we
54      % assume \ht is not a few sp's below \maxdimen...(about 16384pt).
55      \edef\spx@image@rescaledheight % with sp units
56           {\the\numexpr\ht\spx@image@box
57                        *\linewidth/\wd\spx@image@box sp}%
58      \ifdim\spx@image@rescaledheight>\spx@image@maxheight
59        % the rescaled height will be too big, so it is height which decides
60        % the rescaling factor
61        \def\spx@image@requiredheight{\spx@image@maxheight}% dimen register
62        \edef\spx@image@requiredwidth % with sp units
63         {\the\numexpr\wd\spx@image@box
64                      *\spx@image@maxheight/\ht\spx@image@box sp}%
65        % TODO: decide if this commented-out block could be needed due to
66        % rounding in numexpr operations going up
67        % \ifdim\spx@image@requiredwidth>\linewidth
68        %     \def\spx@image@requiredwidth{\linewidth}% dimen register
69        % \fi
70      \else
71        \def\spx@image@requiredwidth{\linewidth}% dimen register
72        \let\spx@image@requiredheight\spx@image@rescaledheight% sp units
73      \fi
74    \else
75      % width is ok, let's check height
76      \ifdim\ht\spx@image@box>\spx@image@maxheight
77        \in@true
78        \edef\spx@image@requiredwidth % with sp units
79            {\the\numexpr\wd\spx@image@box
80                         *\spx@image@maxheight/\ht\spx@image@box sp}%
81        \def\spx@image@requiredheight{\spx@image@maxheight}% dimen register
82      \fi
83    \fi % end of check of width and height
84    \ifin@
85      \setbox\spx@image@box
86      \hbox{\includegraphics
87            [%#1,% contained only width and/or height and overruled anyhow
88            width=\spx@image@requiredwidth,height=\spx@image@requiredheight]%
89            {#2}}%
90      % \includegraphics does not set box dimensions to the exactly
91      % requested ones, see https://github.com/latex3/latex2e/issues/112
92      \wd\spx@image@box\spx@image@requiredwidth
93      \ht\spx@image@box\spx@image@requiredheight
94      \leavevmode\box\spx@image@box
95    \else
96      % here we do not modify the options, no need to adjust width and height
97      % on output, they will be computed exactly as with "draft" option
98      \setbox\spx@image@box\box\voidb@x % clear memory
99      \includegraphics[#1]{#2}%
100    \fi
101}%
102% Use the "safe" one by default (2.0)
103\def\sphinxincludegraphics{\sphinxsafeincludegraphics}
104
105
106%% FIGURE IN TABLE
107%
108\newenvironment{sphinxfigure-in-table}[1][\linewidth]{%
109  \def\@captype{figure}%
110  \sphinxsetvskipsforfigintablecaption
111  \begin{minipage}{#1}%
112}{\end{minipage}}
113% tabulary expands twice contents, we need to prevent double counter stepping
114\newcommand*\sphinxfigcaption
115  {\ifx\equation$%$% this is trick to identify tabulary first pass
116       \firstchoice@false\else\firstchoice@true\fi
117   \spx@originalcaption }
118\newcommand*\sphinxsetvskipsforfigintablecaption
119  {\abovecaptionskip\smallskipamount
120   \belowcaptionskip\smallskipamount}
121
122\endinput
123