首页 > 解决方案 > pylatex:以字符串形式传递的数量

问题描述

我正在使用 pylatex 和 python 数量包来创建格式化的 pdf,并希望能够编写如下内容:

我的自定义文本和数学:F = 1.982 × 10^20 N

我可以使用 pylatex 的 doc.append() 添加文本,我可以使用它添加“数量”值,但我不知道如何将两者放在同一个字符串中。即能够做类似的事情:

doc.append("My custom text and the math: {}".format(math))

doc.append 似乎包含一个 noescape 命令,导致输出为:

My custom text and the math: Math([’F=’, Quantity(array(1.98201661e+20) * N)])

代替:

My custom text and the math: F = 1.982 × 10^20 N

这是来自 pylatex 数量示例的示例代码,后跟一行我自己的代码。

G = pq.constants.Newtonian_constant_of_gravitation
moon_earth_distance = 384400 * pq.km
moon_mass = 7.34767309e22 * pq.kg
earth_mass = 5.972e24 * pq.kg
moon_earth_force = G * moon_mass * earth_mass / moon_earth_distance**2
q1 = Quantity(moon_earth_force.rescale(pq.newton),
              options={'round-precision': 4, 'round-mode': 'figures'})
math = Math(data=['F=', q1])

doc.append("My custom text and the math: {}".format(math))

我发现我可以通过更改为手动执行某些操作:

math = Math(data=['F=', q1], inline=True)

然后做:

doc.append("Test text")
doc.append(math)
doc.append("and moretext")

但我想要一些不那么麻烦的东西,让我这样写:

doc.append("My custom text and the math: {}".format(math))

标签: pythonlatexpylatexquantities

解决方案


推荐阅读