5

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]

  • 2
    According to this StackOverflow post: http://stackoverflow.com/questions/7495693/how-do-i-get-latex-code-to-be-stored-literally-in-python, 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 00: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 00:44
  • 1
    should I post this on stackoverflow instead? – blueberryfields May 14 '13 at 00: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 00:48
  • This is possibly a duplicate of http://tex.stackexchange.com/questions/885/how-can-i-use-latex-from-python –  May 14 '13 at 00:51
  • @Papiro None of the answers there are directly relevant to my question (though maybe one of the referenced tools or libraries has this snippet of code available somewhere - a quick search through them hasn't found it) – blueberryfields May 14 '13 at 00:54
  • Could you instead run some regex replacements on the string to make sure that the common characters with category codes other than 11, 12, and 13 get translated to their escaped counterparts? Like s/\$/\\$/g etc.? – Matthew Leingang May 14 '13 at 01:18
  • 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 02:28
  • 1
    Found it, it is a duplicate: http://stackoverflow.com/questions/16259923/how-can-i-escape-latex-special-characters-inside-django-templates – blueberryfields May 14 '13 at 03:36

0 Answers0