Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 16921

pylatex: how to apply {\centering } around graphics and or text?

$
0
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 pldoc = 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 pldoc = 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')

Viewing all articles
Browse latest Browse all 16921

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>