rst and pypandoc

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Mar 2 07:33:40 EST 2015


alb wrote:

[...]
> For all the above reasons I'm writing snippets of pure latex in my rst
> doc, but I'm having issues with the escape characters:
> 
> i = '\ref{fig:abc}'

Since \r is an escape character, that will give you carriage return followed
by "ef{fig:abc".

The solution to that is to either escape the backslash:

i = '\\ref{fig:abc}'


or use a raw string:

i = r'\\ref{fig:abc}'


Oh, by the way, "i" is normally a terrible variable name for a string. Not
only doesn't it explain what the variable is for, but there is a very
strong convention in programming circles (not just Python, but hundreds of
languages) that "i" is a generic variable name for an integer. Not a
string.



> print pypandoc.convert(i, 'latex', format='rst')
> ef\{fig:abc\}
> 
> because of the \r that is interpreted by python as special character.
> 
> If I try to escape with '\' I don't seem to find a way out...

Can you show what you are doing? Escaping the backslash with another
backslash does work:

py> for c in '\\ref':
...     print(c, ord(c))
...
\ 92
r 114
e 101
f 102

so either you are doing something wrong, or the error lies elsewhere.


-- 
Steven




More information about the Python-list mailing list