# [PATCH] \includegraphics redefinition is broken in sphinx.sty

Leandro Lucarella created an issue

I noticed some problems when trying to include centered figures using raw latex for the figure but reST images (or images included using the graphviz extension), see http://groups.google.com/group/sphinx-dev/browse_thread/thread/2beb7aef4739f739/db445196c9e607bf for details.

I tracked down the problem and found out that is the sphinx.sty redefinition of the tex command \includegraphics:

% Redefine includgraphics for avoiding images larger than the screen size
% If the size is not specified.
\let\py@Oldincludegraphics\includegraphics

\newbox\image@box%
\newdimen\image@width%
\renewcommand\includegraphics[2][\@empty]{%
\ifx#1\@empty%
\setbox\image@box=\hbox{\py@Oldincludegraphics{#2}}%
\image@width\wd\image@box%
\ifdim \image@width>\linewidth%
\setbox\image@box=\hbox{\py@Oldincludegraphics[width=\linewidth]{#2}}%
\fi%
\box\image@box%
\else%
\py@Oldincludegraphics[#1]{#2}%
\fi%
}



If I comment that code in sphinx.sty, I get the expected results (the image centered in the figure). I don't know how useful is that redefinition, maybe it's doing more harm than good (maybe not, I barely know LaTeX =).

Attached is a pure-latex minimal example to reproduce the bug/solution, just run pdflatex test.tex (you need sphinx.sty and manual.cls) and see the results before and after commenting the offending code in sphinx.sty.