3

I'm using PyLaTeX to generate automatically a report file. This report is to be filled with matplotlib plots and I want them to be placed exactly where the code says so. Usually in Latex, I would use the [h] suffix or even [H], but here, no matter what I put in the position, it doesn't work (I mean that the plots are not placed where I want).

Here's my code:

from pylatex import Document, Section, Subsection, Command, Figure
from pylatex.utils import italic, bold, NoEscape

import matplotlib.pyplot as plt 
plt.style.use('ggplot')
import numpy as np 

x = np.linspace(-np.pi/2, np.pi/2,100)
y = np.sin(x)
plt.plot(x,y)

doc = Document('Test')

doc.preamble.append(Command('title', 'Test PyLaTex'))
doc.preamble.append(Command('author', 'Mehdi'))
doc.preamble.append(Command('date', NoEscape(r'\today')))
doc.append(NoEscape(r'\maketitle'))


with doc.create(Section('Premiere Section')):

    doc.append('Premiers resultats \n')
    doc.append('Second resultats')

with doc.create(Section('Deuxieme section')):
    doc.append('Premiers res 2')
    with doc.create(Figure(position = 'htbp')) as fig: 
        fig.add_image('m.jpg')
        fig.add_caption('Celui qui a tout fait')

    with doc.create(Figure(position = 'htbp')) as plot: 
        plot.add_plot()
        plot.add_caption('Avec matplolib')

with doc.create(Section('Troisieme')): 
    doc.append('test image position')

with doc.create(Section('Conclusion')): 
    doc.append('Iz practical')



doc.generate_pdf(clean_tex = False)
doc.generate_tex()

Thanks !

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
Mounsif Mehdi
  • 83
  • 1
  • 7
  • Could it be that you just don't have enough text in your document in order for the figure to be placed as you wish? According to the latex documentation, `h` has these notes: `Place the float here, i.e., approximately at the same point it occurs in the source text (however, not exactly at the spot)` and `H` has these notes: `Places the float at precisely the location in the LaTeX code. Requires the float package (\usepackage{float}).` You could also try `h!`. – Thomas Kühn Feb 13 '18 at 13:41
  • PS: There is a `lipsum` package to fill your document with (otherwise useless) text to quickly check your layout. – Thomas Kühn Feb 13 '18 at 13:43
  • Yop ! It was exactly like this ! Thanks ;) – Mounsif Mehdi Feb 14 '18 at 08:02

0 Answers0