如何使用 Pylatex 将子图置于中心?

如何使用 Pylatex 将子图置于中心?

我一直在使用 Python 中的 Pylatex 包来生成 pdf 格式的报告,但在如何设置报告中子图片的位置而不使用页面的全尺寸宽度(使用居中的小子图片)方面遇到了问题。有什么猜测吗?

我尝试使用“placement='c'”,但没有任何效果......

# 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)
#

结果是文档左侧的子图片。

printscreen of pdf doc

答案1

对于positionsubfigure来说,与水平的定位,但垂直的定位。它决定了如何subfigure放置在基线上。

您应该做的是Command从导入pylatex,然后添加

doc.append(Command('centering'))

在每个图形的开头。

此外,您还应该从图像包含中删除,这除了向您的文档中placement='c'添加内容外,不会执行任何操作。c

# 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)

相关内容