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

Spaces and underscores are not escaped in TeX representation #995

Closed
ovk opened this Issue Dec 24, 2017 · 15 comments

Comments

Projects
None yet
4 participants
@ovk
Contributor

ovk commented Dec 24, 2017

To illustrate the issue, take a look at TeX representation of the following expression:
foo_bar = "Hello World"
TeX:
foo_{bar}:=\mathtt{"Hello World"}

There are two problems here:

  1. The underscore symbol is unescaped and thus makes bar to be a subscript of foo. It should be escaped as \_.
  2. Whitespace inside "Hello World" string is unescaped and thus ignored. It should be replaced with some of the TeX space commands, for example \:.

You can see both issues in action by opening this link https://clcalc.net#eyJjIjpbImZvb19iYXLCoD3CoFwiSGVsbG/CoFdvcmxkXCIiXSwidiI6IjEifQ== and clicking on T button on the left panel to open TeX panel. You'll see that bar is subscripted and space is ignored.

I think the correct TeX representation for this string should be:
foo\_bar:=\mathtt{"Hello\:World"}

@harrysarson

This comment has been minimized.

Collaborator

harrysarson commented Dec 27, 2017

In the correct representation the space is still unescaped, is that intentional?

@ovk

This comment has been minimized.

Contributor

ovk commented Dec 27, 2017

@harrysarson oops no, fixed it now.

@harrysarson

This comment has been minimized.

Collaborator

harrysarson commented Dec 27, 2017

To fix the string escaping https://github.com/josdejong/mathjs/blob/master/lib/type/string.js#L62.

args[0] should be replaced by args[0].replace(' ', ':/').

Although newlines my also need escaping.

@ovk

This comment has been minimized.

Contributor

ovk commented Dec 27, 2017

@harrysarson I think you meant args[0].replace(' ', '\:').
Also a non-breaking space probably needs to be replaced as well, so a better option would be:

args[0].replace(/\s/g, '\:')

since \s works for both regular and non-breakable space (char code 160).

@FSMaxB

This comment has been minimized.

Collaborator

FSMaxB commented Dec 28, 2017

The foo_{bar} was intentional because it allows variable names with subscripts.

Strings should be properly escaped though, I will have to fix that! @harrysarson: Note that the args[0] in the link you used isn't actually javascript code, but a template string that is expanded here.

@ovk

This comment has been minimized.

Contributor

ovk commented Dec 28, 2017

@FSMaxB treating one of the characters in MathJS code as a TeX command is very weird. Not only this is inconsistent (why would you only treat _ as a TeX command but not the other commands) but it also makes it impossible to use an extremely common underscore-separated naming convention for functions and variables. It is even inconsistent with built-in MathJS constants like SQRT1_2. If this was intentional, I really think it should be reconsidered.

@FSMaxB

This comment has been minimized.

Collaborator

FSMaxB commented Dec 28, 2017

Yes, maybe you are right. I don't fully remember the rationale behind it. I probably just felt like it should behave this way but didn't really think about it.

About escaping strings: Do you know a good method to print strings as is in a LaTeX formula? Because currently only spaces would be fixed with this string replacement, but none of the other characters because there is absolutely no escaping going on at all.

@josdejong

This comment has been minimized.

Owner

josdejong commented Dec 28, 2017

Your point of inconsistency in handling some TeX commands and others not makes sense Oleg, sounds good simply escape everything (spaces, underscores, others?) in toTex function .

@FSMaxB since there is an option to hook into the toTex function and override it, I think we already have a a way to do special things like create a variable name with subscripts, right?

@FSMaxB

This comment has been minimized.

Collaborator

FSMaxB commented Dec 28, 2017

Since there is an option to hook into the toTex function and override it, I think we already have a a way to do special things like create a variable name with subscripts, right?

Yes, this can be achieved by overriding the toTex handler of ConstantNode, AssignmentNode and FunctionAssignmentNode. But this is a quite intrusive and tedious thing to do!

Would it be ok to add a new dependency (something like https://github.com/dangmai/escape-latex) for handling the toTex of strings?

@josdejong

This comment has been minimized.

Owner

josdejong commented Dec 28, 2017

Would it be ok to add a new dependency (something like https://github.com/dangmai/escape-latex) for handling the toTex of strings?

Yes that makes sense. It's not much code and if we would implement it ourselves we would probably end up with about the same amount of code (but reinventing the wheel...)

@FSMaxB

This comment has been minimized.

Collaborator

FSMaxB commented Dec 28, 2017

I'll work on it then. I can give no time estimate at this point though.

@josdejong

This comment has been minimized.

Owner

josdejong commented Dec 28, 2017

👍 thanks beforehand. Take it easy, it's holidays ;)

@josdejong

This comment has been minimized.

Owner

josdejong commented Jan 31, 2018

Fixed via #1022 (fix is not yet released)

@harrysarson

This comment has been minimized.

Collaborator

harrysarson commented May 31, 2018

This was merged into master for version 4 :)

@josdejong

This comment has been minimized.

Owner

josdejong commented May 31, 2018

thanks Harry 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment