I'm using python to populate a tex template with arbitrary strings - and I would like to escape the strings so they're interpreted and displayed properly inside the tex file (rather than having them mess up my formatting).

Where can I find a python code snippet, which will use the tex rules for escaping an arbitrary string?

To clarify, I want to do this:

[arbitrary string] --->{latex flags}[arbitrary string]{latex flags}

from inside of a python program, such that I can run latex on:

{latex flags}[arbitrary string]{latex flags}

and latex will not complain about strange characters inside [arbitrary strings]

closed as too localized by Thorsten, Stefan Kottwitz May 14 '13 at 5:36

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    According to this StackOverflow post: stackoverflow.com/questions/7495693/…, you can just use the r modifier to a string. So r"\foo" will take care of the escaping. – Matthew Leingang May 14 '13 at 0:38
  • That post talks about going the other way - escaping latex code so it is interpreted as a regular string in python. I'm looking for the reverse - to escape a string, using python code, so that it's interpreted verbatim by latex. – blueberryfields May 14 '13 at 0:44
  • 1
    should I post this on stackoverflow instead? – blueberryfields May 14 '13 at 0:47
  • 1
    It would be helpful if you could give a few actual ideal-use-cases for your function. (I'd agree that since this is a python question, it has a place on SO, but its specificity toward TeXnicalities gives it a firm leg to stand on here.) – Sean Allred May 14 '13 at 2:28
  • 1
    Found it, it is a duplicate: stackoverflow.com/questions/16259923/… – blueberryfields May 14 '13 at 3:36

Browse other questions tagged or ask your own question.