0

I am trying to create the following (type of) code with PyLatex:

{
     \centering
     \includegraphics{image.png}
     \includegraphics{image.png}
}

I am not able to 'group' the \centering command with the curly brackets around the \includegraphics; if I define a new Environment, I always end up with \begin and \end which add spacing.

So, do you know how I can nicely add the curly brackets with the \centering command around a snippet of code like \includegrahics?

To me, the most elegant solution would be something like:

with doc.centering():
    doc.append('Some text.')
    append(StandAloneGraphic(image))
    append(StandAloneGraphic(image2))

How can I define such '''with doc.centering()''' command? Of course, I can hard code this every time, but I want my code to be readable so it would be nice if I could write a nice function for this problem.

I posted this question earlier om Stack Exchange (https://tex.stackexchange.com/questions/490429/pylatex-how-to-apply-centering-x-around-graphic/490442), but over there I got the advice to post here because it is more of a Python programming problem than a PyLatex challenge.

Update: I would like to put teh code below in a nice with.group(): statement:

import pylatex as pl

doc = pl.Document()
doc.preamble.append(pl.Package('showframe'))

doc.append(pl.NoEscape('{'))
doc.append(pl.Command('centering'))
doc.append(pl.StandAloneGraphic('example-image',image_options='width=5cm'))
doc.append(pl.Command('par'))
doc.append(pl.NoEscape('}'))

doc.generate_tex('pylatexdemo')

This piece of code creates the correct output, but is not really readable. I would imagine such solution (but I don't know how to program it); the definition of the group-function is incorrect. My question is: how to implement such group-function?

import pylatex as pl


doc = pl.Document()
doc.preamble.append(pl.Package('showframe'))

def group(content):
    doc.append(pl.NoEscape('{'))
    doc.append(content)  
    doc.append(pl.Command('par'))
    doc.append(pl.NoEscape('}'))

with doc.group():
    doc.append(pl.Command('centering'))
    doc.append(pl.StandAloneGraphic('example-image',image_options='width=5cm'))

doc.generate_tex('pylatexdemo')
spectre
  • 31
  • 1
  • 5
  • The answer https://tex.stackexchange.com/a/490442/36296 makes me wonder if the whole problem boils down to the fact that `{}` are "used otherwise" by python. I don't know pylatex, so the following is just a crude guess, but could you try something like: `doc.append('Some text.') doc.append(pl.Command('begingroup')) doc.append(pl.Command('centering')) append(StandAloneGraphic(image)) doc.append(pl.Command('endgroup'))` ? – samcarter_is_at_topanswers.xyz May 13 '19 at 13:12
  • It would be helpful if you could crate a short example document to give us something to play with. – samcarter_is_at_topanswers.xyz May 13 '19 at 13:16
  • Hi @samcarter, I just updated the qustion. Is that clear for you? So, I know how the get the correct result, but I would like to have a little bit 'nicer' solution to make my code look much more clean and like other PyLatex statements (for example adding sections) – spectre May 13 '19 at 15:35

0 Answers0