I'm using pylatex and the python quantities packages to create a formatted pdf and want to be able to write something like:
My custom text and the math: F = 1.982 × 10^20 N
I can use pylatex's doc.append() to add text and I can use it to add a 'quantities' value but I don't know how to have both in the same string. i.e. be able to do something like:
doc.append("My custom text and the math: {}".format(math))
The doc.append seems to include a noescape command resulting in the output being:
My custom text and the math: Math([’F=’, Quantity(array(1.98201661e+20) * N)])
instead of:
My custom text and the math: F = 1.982 × 10^20 N
This is the sample code from pylatex quantities exmaple followed by one line of my own code.
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))
I've found that I can do something manually by changing to:
math = Math(data=['F=', q1], inline=True)
and then doing:
doc.append("Test text")
doc.append(math)
doc.append("and moretext")
but I want something less cumbersome and let's me write it just like:
doc.append("My custom text and the math: {}".format(math))