Search code examples
latexpython-sphinxfigure

Sphinx to Latex Figure Placement


I'm trying to used Sphinx to generate a manual in latex and HTML, but I'm having issues with the figure placement within latex. In Sphinx my figures are as follows:

.. figure:: _images/somepicture.png
    :figwidth: 100 %
    :width: 100 %
    :align: center

    some caption

So that when it writes to HTML, the figures span the whole width of the page. This works fine.

The trouble comes in latex, where a typical figure in latex writes as:

\begin{figure}[htbp]
\centering

\includegraphics[width=1.000\linewidth]{reset.png}
\end{figure}

The trouble is the [htbp] placement option. Since the images are \linewidth in length, they are very large and end up floating to the next page, and any often the order of text and figures in the document is not preserved in the the pdf output. I want to change [htbp] to [H].

I found an option to put into the conf.py file under latex elements section called 'figure_align', but when I use it, it doesn't work. The link is here http://sphinx.readthedocs.org/en/latest/config.html#options-for-latex-output

in my conf.py document, I have the following:

latex_elements = {
# The paper size ('letterpaper' or 'a4paper').
#'papersize': 'letterpaper',

# The font size ('10pt', '11pt' or '12pt').
#'pointsize': '10pt',

# Additional stuff for the LaTeX preamble.
#'preamble':'',

#Figure placement within LaTeX paper NOT WORKING
'figure_align': 'H'

}

My question is, why isn't this option working and what can I do to make it work?


Solution

  • I found the Pull Request which added this feature here: https://bitbucket.org/birkenfeld/sphinx/pull-request/227/latex-allow-alternative-float-options-for/diff

    I looked through my source code and found that my version of sphinx did not include the latex element 'figure_align' in the writers files. I added the code from the pull request and it works now.

    The version stated in the "build configuration file" documentation for sphinx (found here) states that all the latex elements options are available in version 0.5 or later, it appears that some of the elements were only added in later versions of sphinx. I am using version 1.2.2 and 'figure_align" was not a valid latex element.

    In short I could have upgraded to the newest version of sphinx however I simply added the code from the pull request and it works as intended now.