Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LaTeX ; double slash or single slash #71

Open
firasm opened this issue Jul 12, 2023 · 7 comments
Open

LaTeX ; double slash or single slash #71

firasm opened this issue Jul 12, 2023 · 7 comments
Labels
bug Something isn't working priority

Comments

@firasm
Copy link
Contributor

firasm commented Jul 12, 2023

This was testing using pbs v0.8.1.

Standard LaTeX to produce this code

gif latex

is:

\sqrt{\rho_\mathrm{Cu} / \rho_\mathrm{Al}}

but in PrairieLearn question.html files, the \rho needs to be \\rho when passed in from server.py via mustache templating

 data2["params"]["part1"]["ans1"]["value"] = '$\sqrt{\\rho_\mathrm{Cu} / \\rho_\mathrm{Al}}$'

Alternatively, the string can be made a raw string (which also works):

 data2["params"]["part1"]["ans1"]["value"] = r'$\sqrt{\rho_\mathrm{Cu} / \rho_\mathrm{Al}}$'

Aside:

I thought another alternative, in question.html, would be to surround the passed variable with triple curly braces:

<pl-answer correct= {{{ params.part1.ans1.correct }}}

but this does not work.


Due to recent changes in how pbs handles unicode (namely #51), I think I have to do a find/replace to convert all \\ to \ in the instructor_physics_bank, for example in this question from OPBP.

Unfortunately, because of the issue outlined above, it's not a simple matter of doing a find and replace to convert all \\ to \.

So need to think about a way to do this without reviewing every question one by one.

@firasm firasm added bug Something isn't working priority labels Jul 12, 2023
@Bluesy1
Copy link
Collaborator

Bluesy1 commented Jul 12, 2023

This seems odd, something with python?

>>> "\sqrt{\\rho_\mathrm{Cu} / \\rho_\mathrm{Al}}"
'\\sqrt{\\rho_\\mathrm{Cu} / \\rho_\\mathrm{Al}}'
>>> r"\sqrt{\\rho_\mathrm{Cu} / \\rho_\mathrm{Al}}"
'\\sqrt{\\\\rho_\\mathrm{Cu} / \\\\rho_\\mathrm{Al}}'

Is this due to \r being a valid escape sequence?
I tested we nu, which has another another escape sequence:

>>> "\nu"
'\nu'
>>> "\\nu"
'\\nu'
>>> "\sqrt{\\nu}"
'\\sqrt{\\nu}'
>>> "\sqrt{\\nu}"

I think thats whats happening here, its not a prarielearn thing, its that python won't autoescape escape sequences without raw strings, but will escape invalid escape backslashes

@firasm
Copy link
Contributor Author

firasm commented Jul 12, 2023

Ah yes, you're right. So \nrtfh are special sequences in python.

So what would you do:

  1. use the vscode find and replace to switch all \\ to \, and then switch \nrtfh to double slashes?

  2. Set all answers with latex in python as raw strings?

  3. Handle this in pbs somehow

  4. Something else?

I think I'm leaning towards option 2.

@Bluesy1
Copy link
Collaborator

Bluesy1 commented Jul 12, 2023

I would do 2, it makes it the most readable to people not familiar with the issue here, but that will require a 1 time fix to remove our manual escaping of them, otherwise they will not render in the same vein, since the backslash will be escaped and defeat the purpose of the fix, so there will need to be a 1 time fix either way

@firasm
Copy link
Contributor Author

firasm commented Jul 12, 2023

Yes there has to be a one time fix no matter what.

I think I'll do it next week after the Apsc 181 questions are merged in so I can do it all at once.

In the meantime I'll add that guidance to the training docs so we pick a way forward.

Thanks for your help.

@Bluesy1
Copy link
Collaborator

Bluesy1 commented Aug 15, 2024

Just as an FYI - the guidance (and solution), needs to be changed to use raw strings.

In 3.12, it was changed in cpython such that invalid escapes generate SyntaxWarnings instead of DepreciationWarnings and they will eventually be changed to SyntaxErrors. (See the second bullet here: https://docs.python.org/3.12/whatsnew/3.12.html#other-language-changes)

We can't rely on always using 3.10, or any python version below this change, so it would be better to use raw strings (which can still have formatting, rf"...{var}..." is legal)

(See also: python/cpython#98401)

@firasm
Copy link
Contributor Author

firasm commented Aug 15, 2024

I agree raw strings are what we need to do. This change would happen script-side only right? IOW, no need to change the MD files?

If so, and you have time, feel free to take care of this too as part of the 1.0 update.

@Bluesy1
Copy link
Collaborator

Bluesy1 commented Aug 15, 2024

I don't think this can be done script side, since we have no way to know if a valid escape should be left alone or not

There's too much ambiguity about this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working priority
Projects
None yet
Development

No branches or pull requests

2 participants