1

I have been using Pylatex package in Python to generate reports in pdf format, but I got a problem in how to set the position of subpictures at a report without to use the full size width of page (using small subpictures centered). Any guess about?

I try to use "placement='c'" and nothing...

# the picture to plot
[![kitten][1]][1]

# my code 
import os

from pylatex import Document, PageStyle, NewPage, SubFigure, Figure, Section
from pylatex.utils import NoEscape

def generate_pdf_report(imageFilename):
    geometry_options = { "head": "30pt",
                     "margin": "0.3in",
                     "top": "0.2in",
                     "bottom": "0.4in",
                     "includeheadfoot": True}

    doc = Document(geometry_options=geometry_options)
    first_page = PageStyle("firstpage")
    doc.preamble.append(first_page)
    doc.change_document_style("firstpage")

    with doc.create(Section('Plotting multiple figures')):
        doc.append("How to set the pictures at center of page without change the width?")

        with doc.create(Figure(position='h!')) as imagesRow1:
            with doc.create(
                SubFigure(position='c',  width=NoEscape(r'0.33\linewidth'))) as left_imagesRow1:
                left_imagesRow1.add_image(imageFilename,placement='c', width=NoEscape(r'0.95\linewidth'))
                left_imagesRow1.add_caption("img 1")

            with doc.create(
                SubFigure(position='c', width=NoEscape(r'0.33\linewidth'))) as right_imagesRow1:
                right_imagesRow1.add_image(imageFilename,placement='c', width=NoEscape(r'0.95\linewidth'))
                right_imagesRow1.add_caption("img 2")

        with doc.create(Figure(position='h!')) as imagesRow2:
            with doc.create(
                SubFigure(position='c',  width=NoEscape(r'0.33\linewidth'))) as left_imagesRow2:
                left_imagesRow2.add_image(imageFilename,placement='c', width=NoEscape(r'0.95\linewidth'))
                left_imagesRow2.add_caption("img 3")

            with doc.create(
                SubFigure(position='c', width=NoEscape(r'0.33\linewidth'))) as right_imagesRow2:
                right_imagesRow2.add_image(imageFilename, placement='c', width=NoEscape(r'0.95\linewidth'))
                right_imagesRow2.add_caption("img 4")

        imagesRow2.add_caption("Setting the subpictures at center")

    doc.append(NewPage())


    doc.generate_pdf("test_image_positions", compiler="pdflatex", clean=True, clean_tex=False)

imageFilename = os.path.join(os.getcwd(),"kitten.jpg")

generate_pdf_report( imageFilename)
#

The result is the subpictures on the left side of document.

printscreen of pdf doc

  • Your Python code doesn't work, the indentation is incorrect, can you fix it so that it works? – Torbjørn T. Dec 29 '17 at 13:13
  • @TorbjørnT. I fix it, sorry about it.. – Isaque Daniel Dec 29 '17 at 13:24
1

The position for a subfigure is not related to horizontal positioning, but vertical positioning. It determines how the subfigure is placed on the baseline.

What you should do instead is to import Command from pylatex, and then add

doc.append(Command('centering'))

at the start of each figure.

In addition you should remove placement='c' from the image inclusion, which doesn't do anything, except adding a c to your document.

# the picture to plot
# [![kitten][1]][1]

# my code 
import os

from pylatex import Document, PageStyle, NewPage, SubFigure, Figure, Section, Command
from pylatex.utils import NoEscape

def generate_pdf_report(imageFilename):
    geometry_options = { "head": "30pt",
                     "margin": "0.3in",
                     "top": "0.2in",
                     "bottom": "0.4in",
                     "includeheadfoot": True}

    doc = Document(geometry_options=geometry_options)
    first_page = PageStyle("firstpage")
    doc.preamble.append(first_page)
    doc.change_document_style("firstpage")

    with doc.create(Section('Plotting multiple figures')):
        doc.append("How to set the pictures at center of page without change the width?")

        with doc.create(Figure(position='h!')) as imagesRow1:
            doc.append(Command('centering'))
            with doc.create(
                SubFigure(position='c',  width=NoEscape(r'0.33\linewidth'))) as left_imagesRow1:
                left_imagesRow1.add_image(imageFilename, width=NoEscape(r'0.95\linewidth'))
                left_imagesRow1.add_caption("img 1")

            with doc.create(
                SubFigure(position='c', width=NoEscape(r'0.33\linewidth'))) as right_imagesRow1:
                right_imagesRow1.add_image(imageFilename, width=NoEscape(r'0.95\linewidth'))
                right_imagesRow1.add_caption("img 2")

        with doc.create(Figure(position='h!')) as imagesRow2:
            doc.append(Command('centering'))
            with doc.create(
                SubFigure(position='c',  width=NoEscape(r'0.33\linewidth'))) as left_imagesRow2:
                left_imagesRow2.add_image(imageFilename, width=NoEscape(r'0.95\linewidth'))
                left_imagesRow2.add_caption("img 3")

            with doc.create(
                SubFigure(position='c', width=NoEscape(r'0.33\linewidth'))) as right_imagesRow2:
                right_imagesRow2.add_image(imageFilename,  width=NoEscape(r'0.95\linewidth'))
                right_imagesRow2.add_caption("img 4")

        imagesRow2.add_caption("Setting the subpictures at center")

    doc.append(NewPage())


    doc.generate_pdf("test_image_positions", compiler="pdflatex", clean=True, clean_tex=False)

imageFilename = os.path.join(os.getcwd(),"out-inc.pdf")

generate_pdf_report( imageFilename)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.