Wednesday, September 2, 2009

New-style String Formatting and LaTeX

The recommended new way (since 2.6) of doing string formating in Python is to use the format method of string objects instead of the % operator and %s (and variants) placemarks.

I decided to check it out to generate some LaTeX tables programatically. Bad Idea. the method expects placemarks like this: "some string {key}".format(key=123) and that is a big problem when formatting LaTeX strings. Curly braces are a very significant character in LaTeX, and therefore you will get a Key Errors all the time. The only solution I see is that format would silently ignore Key Errors. But that is not currently an option.

Has anyone else devised a solution to this? The scary part of this is that the official documentation says that the % operator for string formatting will eventually be removed (?!?) from the language.

5 comments:

Marius Gedminas said...

>>> '\\begin{{{env}}}'.format(env='article')

'\\begin{article}'

Flavio Coelho said...

@Marius: That is a bit of an overkill! having to rewrite all your LaTeX statements just to be able to insert a single string!
;-)

BTW, you don't need to escape backslashes if you use raw strings:
>>> r'\begin{article}'
'\\begin{article}'

Anonymous said...

Maybe instead, generate your table in a plain text format (such as the one used by reST or pandoc's enhanced Markdown), and then convert it to LaTeX in another pass?

Anonymous said...

Just keep using the % formatting. I still have my doubts it will ever go away from the language, and the moment it does you'll have somebody who'll write a module to emulate them.

Kao said...

It's not clear to me what you want to do and why it is a problem to escape the curly brackets like Marius suggested. Should you use the % syntax to populate templates on any language where % have special meaning you would run on the exact same problem and have to escape the % too.

Escaping the curly brackets on a plain LaTeX snippet is a simple substitution (no manual rewriting needed :) ), but if you already have your format keys on there they would be escaped too. Maybe you can list your special {keys} and substitute every other { or } on the text by {{ and }}. It's cumbersome, but I can't see any other way to specify that some of these are LaTeX and some are keys for the format function.

ccp

Amazon