2
$\begingroup$

I am trying to create a function that needs to transform its input into a string. The objective is to transform a $\LaTeX$ mathematical expression into an executable Mathematica expression.

latextoma[x_] := ToExpression[InputForm[x], TeXForm]

Here, I'm trying to use InputForm because the function needs to accept inputs of the following form:

\sum_ {n = 1}^{4} 0.9^{n - 1} (1 - 0.9)

Ideally, the input would not be a string. Is there any way I can tell the function to work with this input as a string?

$\endgroup$
4
  • 1
    $\begingroup$ I don't think mathematica could possibly accept f[\.... The question is, where does the input come from? I do recall, however, that there was a related question somewhere here, about forcing Mathematica to take input verbatim. Is it completely out of the question to wrap the input in quotes? $\endgroup$
    – LLlAMnYP
    Commented May 8, 2015 at 15:01
  • 1
    $\begingroup$ Where would this input come from? $\endgroup$
    – C. E.
    Commented May 8, 2015 at 15:09
  • 1
    $\begingroup$ I agree with @LLlAMnYP that I don't see how a MMA function could accept input starting with \s (or even how one could feed such input to a function, for that matter!). I think the key here is where you get your expressions, as others have mentioned. Also, why would it be ideal for your input not to be a string? It actually seems to complicate things quite a bit. Finally, if you don't want your resulting expression to be automatically evaluated, you may want to have ToExpression wrap it in HoldForm, i.e. ToExpression[x, TeXForm, HoldForm]. $\endgroup$
    – MarcoB
    Commented May 8, 2015 at 17:35
  • $\begingroup$ The expression is a Latex expression; but I suppose I may be asking too much. Is there a way to make mathematica automatically always interpret the stuff I copy from latex as InputForm? It always pops up a warning asking me about it. $\endgroup$
    – user191919
    Commented May 10, 2015 at 16:07

2 Answers 2

3
$\begingroup$

Yes, it is possible to get Mathematica to accept TeXForm input in raw form without quotes, using a method similar to the one described in this answer.

Using $PreRead to intercept the input before it is interpreted by Mathematica and then convert it to a string, we can define the latextoma function as a replacement rule with

$PreRead = (# /. RowBox@{"latextoma", "[", expr__, "]"} :> 
  RowBox@{"ToExpression[", 
    ToString@InputForm@StringJoin@Cases[{expr}, _String, \[Infinity]], 
    ", TeXForm]"
  }
) &;

Here, $PreRead gets the input in box form, Cases and StringJoin picks out and reassembles the input in its original form, and InputForm and ToString adds the necessary quotes and escape characters. Then the box form of ToExpression is inserted so that it will take effect when the input is interpreted. Any whitespace in the input is lost during this procedure; luckily, $\LaTeX$ ignores most whitespace as well, so that's not too much of an issue.

Now we can do (look ma, no quotes!):

latextoma[\sum_ {n = 1}^{m} n^2]

1/6 m (1 + m) (1 + 2 m)

There may be ways to break this, though -- you'd have to decide if the benefit is worth the potential trouble.

$\endgroup$
1
$\begingroup$

Would this help?

ToString[FromCharacterCode[92]] <> "\sum_{n = 1}^{4} 0.9^{n - 1} (1 - 0.9)"

(*

\\sum_{n = 1}^{4} 0.9^{n - 1} (1 - 0.9)

*)

$\endgroup$
1
  • 1
    $\begingroup$ The idea is to make the function automatically write the '\sum_{n = 1}^{4} 0.9^{n - 1} (1 - 0.9)' part as "\\sum_{n = 1}^{4} 0.9^{n - 1} (1 - 0.9)", instead of manually doing it. Thats why I need the input form. $\endgroup$
    – user191919
    Commented May 8, 2015 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.