0

我正在尝试使用 PyLatex 创建以下(类型)代码:

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

我无法用;\centering周围的花括号对命令进行“分组”。如果我定义了一个新的环境,\includegraphics我总是以添加间距结束。\begin\end

\centering那么,你知道我怎样才能很好地用命令在代码片段周围添加大括号\includegrahics吗?

对我来说,最优雅的解决方案是:

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

如何使用 doc.centering()''' 命令定义这样的 '''?当然,我每次都可以硬编码,但我希望我的代码可读,所以如果我能为这个问题写一个很好的函数就好了。

我早些时候在 Stack Exchange 上发布了这个问题(https://tex.stackexchange.com/questions/490429/pylatex-how-to-apply-centering-x-around-graphic/490442),但在那里我得到了建议在这里发帖是因为它更像是一个 Python 编程问题,而不是 PyLatex 挑战。

更新:我想把下面的代码放在一个很好的with.group():声明中:

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

这段代码创建了正确的输出,但不是真正可读的。我会想象这样的解决方案(但我不知道如何编程);-function的定义group不正确。我的问题是:如何实现这样的group功能?

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

0 回答 0