TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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]

share|improve this question

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.

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
should I post this on stackoverflow instead? – blueberryfields May 14 '13 at 0:47
Oh, I see. I think "arbitrary" is going to be a problem. If you could tell latex to how to properly interpret an arbitrary string there would be no need for latex markup. – Matthew Leingang May 14 '13 at 0:48
This is possibly a duplicate of tex.stackexchange.com/questions/885/… – Papiro May 14 '13 at 0:51