0

I would like to set different margins for different sections of a Pylatex document (incl header, sections and subsections). I'm new to latex

I am currently setting my geometry options as follows, using this minimal example. I tried to change the margins of subsequent sections by setting the geometry options, which didn't work:

from pylatex import Command, Document, Section

geometry_options = {"tmargin": "0.5in", "lmargin": "0in", "bmargin": "0.5in", "rmargin": "0in"}
doc = Document(geometry_options=geometry_options, fontenc='T1')

with doc.create(Section('Section1')) as Sect1:
    Sect1.append('some text')

with doc.create(Section('Section2')) as Sect2:
    Sect2.append('some more text')
    Sect2.geometry_options = Command({'margins':'1in'})

doc.generate_pdf("testoutput", clean_tex=False)

This is the corresponding latex output:

\documentclass{article}%
\usepackage[T1]{fontenc}%
\usepackage[utf8]{inputenc}%
\usepackage{lmodern}%
\usepackage{textcomp}%
\usepackage{lastpage}%
\usepackage[tmargin=0.5in,lmargin=0in,bmargin=0.5in,rmargin=0in]{geometry}%
%
%
%
\begin{document}%
\normalsize%
\section{Section1}%
\label{sec:Section1}%
some text

%
\section{Section2}%
\label{sec:Section2}%
some more text

%
\end{document}

How can I change the margins within the document in the Pylatex code?

0

You expect pylatex to translate

Sect2.geometry_options = Command({'margins':'1in'})

to something like

\newgeometry{margins=1in}

But probably pylatex has no idea of geometry.

Read the geometry manual (cmd: texdoc geometry), section User interface. Probably \savegeometryand \loadgeometrywill become your friends, if you can pass the necessary commands from your pylatex file to LaTeX. I have no idea how to do that, but to judge from your code you seem to know such things.

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.