# pylatex: how to apply {\centering <x> } around graphic?

I am trying to create the following code with PyLatex:

{
\centering
\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))


How can I define such command?

NoEscape('{') will add an opening brace. For example

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


The PDF will look like this, where the frame indicates the text block, and is created by the showframe package.

• Thanks, that is indeed a possible solution! Do you know how I can wrap this into a single command (see my question, I added an example)? So I can call it like a section or Center Environment? – spectre May 12 '19 at 13:00
• @spectre Not really. I mean, I suppose you can always just def CenteredImage(doc, imagename): ..., but that's not precisely what you're asking. (This might be off-topic here by the way, and better suited at Stack Overflow, because it's really just a Python question.) – Torbjørn T. May 12 '19 at 13:54