2

I am using an API to convert LaTeX to PNG format. The latex strings that I am converting are written in latex, .tex, and so they use phrases such as '\n'.

On example of a string that I have is

query = "$\displaystyle \binom n r = \dfrac{n!}{r!(n-r)!}$"

However, Python recognizes the \b in \binom and thus the string is recognized as having a line break, even though all I want it to do is just recognize the individual characters.

If at all possible, I would like to not have to modify the string itself, as the string too is taken from an API. So is there any way to ignore these string literals such as '\b' or '\n'?

Destaq
  • 655
  • 9
  • 23
  • "\" is the escape character. Anything following it is interpreted as a special character. If you want a literal "\", you can escape the escape character by adding another "\" -- so "\\b or \\n". – Mark J Jun 02 '20 at 13:29

2 Answers2

3

Use r"$\displaystyle \binom n r = \dfrac{n!}{r!(n-r)!}$". This is called a raw string. You can read more about it here

Generally, you can use raw strings in the following format:

Normal string:

'Hi\nHow are you?'

output:

   Hi
   How are you?

Raw string:

r'Hi\nHow are you?'

output:

   Hi\nHow are you?
Jeries Haddad
  • 79
  • 1
  • 1
  • 9
paradocslover
  • 2,932
  • 3
  • 18
  • 44
  • What if that's the string that's received by the API? – gust Jun 02 '20 at 13:26
  • Thank you! Is there anyway to convert a normal string to a raw string? – Destaq Jun 02 '20 at 13:27
  • @Gust Can you please elaborate on what you are trying to point out? – paradocslover Jun 02 '20 at 13:27
  • Raw and normal strings are just different ways of writing string literals in source code. A string that is read via an API is always "raw" in that there is no interpretation of escape sequences. So you only need to take care of this for string literals within your program's source code. – Hans-Martin Mosner Jun 02 '20 at 13:59
  • I think what Gust is trying to point out is that you can't explicitly state `query = r"data"` if the data comes from an API call, because you need to use something like `query = apiCall()` instead. I mentioned in my answer that the API data is already going to be in the "rawest" format available to Python since it comes from a binary stream. – h0r53 Jun 02 '20 at 14:23
2

I've updated my answer for clarity.

If the string comes directly from an API then it should already be in a raw format (or the rawest that will be accessible to you), such as r"$\displaystyle \binom n r = \dfrac{n!}{r!(n-r)!}$". Therefore, Python won't be assuming escaped characters and there shouldn't be an issue.

To answer your other question about raw strings - to print a string as a raw string in Python try the repr function, which returns a printable representational string of the given object.

query = "$\displaystyle \binom n r = \dfrac{n!}{r!(n-r)!}$"
print(repr(query))

Here is the output: '$\\displaystyle \x08inom n r = \\dfrac{n!}{r!(n-r)!}$'

Note that in the true raw data of query above, the \b character is still technically stored as the \b encoding (or \x08), not as two separate characters. Why isn't \d stored as an encoding, you may ask? Because \d is not a valid encoded escape sequence, so it is overlooked and Python treats the \ as a character. (Ahh... silently ignoring parsing errors, isn't this why we love Python?)

Then what about this example?

query = r"$\displaystyle \binom n r = \dfrac{n!}{r!(n-r)!}$"
print(repr(query))

Looks good, but wait, Python prints '$\\displaystyle \\binom n r = \\dfrac{n!}{r!(n-r)!}$'.

Why the \\? Well, the repr function returns a printable representational string of the given object, so to avoid any confusion the \ character is properly escaped with \, creating \\.

All of that coming full circle and back to your question - if the value of a string comes directly from an API call, then the string data should already be translated from a binary encoding and things like escape sequences shouldn't be an issue (because the aren't in the raw data). But in the example you provided you declared a string in a query = "st\ring" format, which unfortunately isn't equivalent to retrieving a string from an API, and the obvious solution would be to use the query = r"st\ring" format.

h0r53
  • 3,034
  • 2
  • 16
  • 25
  • The output for `repr(query)` is `'$\\displaystyle \x08inom n r = \\dfrac{n!}{r!(n-r)!}$'` . Is it possible to see the actual content of the string i.e. `"$\displaystyle \binom n r = \dfrac{n!}{r!(n-r)!}$"`? – paradocslover Jun 02 '20 at 13:47
  • 1
    @paradoxlover I've improved my answer for clarity. That's for pointing this out. – h0r53 Jun 02 '20 at 14:22