Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a good way to transform syntax from other computer algebra systems or from latex to mathematica syntax?

share|improve this question
I think this would be a better question, and attract more useful answers, if it were more specific. Eg: "How do I convert TeX to Mathematica syntax", "How do I convert Excel formulas to Mathematica", etc... – Brett Champion Jan 18 '12 at 16:29
@BrettChampion Maybe. Personally, I am interested in Latex, maple and sage, and the answers correspond to my intention. I would prefer to discus this on meta. This is the private beta after all, I can reformulate, partition or delete my question also tomorrow, there is no hurry. – Phira Jan 18 '12 at 17:26
Meta thread here:… – Phira Jan 18 '12 at 17:33
What you're asking for is essentially machine translation applied to programming languages. – Mike Bailey Jan 19 '12 at 2:19
up vote 12 down vote accepted

There is support for TeX files built in to the Import function. For example:


Will load the sample TeX file located at that URL into a Mathematica notebook. You can of course replace the URL with a path to a file on your computer.

The TeX file is imported with "structure" intact (I suppose we'd expect this from Mathematica), so you can play around with how exactly it is formatted.

As for other computer algebra systems, it would surprise me if there was an easy way to translate syntax in general--but you can use StringReplace for simple syntax modifications, as in this example:

ToExpression[StringReplace["Sin(x)+Cos(y)", {"(" -> "[", ")" -> "]"}]]
share|improve this answer

Other people have answered for TeX, since that functionality is present in Mathematica. They have also given tips for semi-manually making the translation.

One way would be to use Sage as an intermediate. Sage has interfaces to most of the major computer algebra systems out there. See:

Here's an example of going from Maxima to Mathematica

sage: a = maxima('(sin(%e^x) + sqrt(2))^5'); a
sage: mathematica(a)
(Sqrt[2] + Sin[E^x])^5

This could be done from within Mathematica by calling Sage from the Mathematica frontend. One neat way of doing this might be to use a new input style that evaluates in Sage, see e.g., WReach's "Mathematica tool bag" post.

share|improve this answer
There was some recent discussion about this on the sage-devel group: Mathematica/Magma/Sage dictionaries – Simon Jan 18 '12 at 11:24

This is a practical workaround which I've used in the past to transfer even huge expressions from other systems:

What I often do in practice is create a TraditionalForm input cell, and paste the expression there. The biggest difference between Mathematica and other systems is that Mathematica uses f[x] while most others use f(x). f(x) is interpreted correctly in TraditionalForm, and the rest of the editing can be done manually (party on the string input, and partly by Replaceing the resulting function)

If it is only an expression (not a program) that you need to convert, this often works very well in practice.

share|improve this answer

An excellent book into the subject is Language Implementation Patterns by Terence Parr ( prof. and lead developer of ANTLR ). Part IV of the book covers the topic you are interested in: "Translating and Generating Languages".

share|improve this answer

Yes, there is, but it is not particularly simple. Generally, you will have to get somewhere a grammar of the language of interest, and write a parser, which would transform the program given in that languages (as a string) into some Mathematica expression representing the AST (Abstract Syntax Tree) of that program. For this stage, you will need familiarity with some lower-level language (in practice, C or Java), and parser generators with bindings for that languages (Flex/Bison for C and say ANTLR for Java). With some effort, you can then import the program as a Mathematica expression, into Mathematica, using MathLink or LibraryLink if you choose C and J/Link for Java.

The second part you can do in Mathematica. The good news is that replacement rules are very well suited for programming code transformations. One of the biggest problems you will face at this stage is premature evaluation of pieces of code you generate, which you will have to prevent. Some discussion of one simple way of how to do this can be found here.

As of now, however, this is a non-trivial undertaking. We may hope that some day there will be Mathematica bindings for things like Flex/Bison, so that the first part could be done entirely in Mathematica. We may also hope that some better / more powerful ways /libraries will emerge which will facilitate easier generation of Mathematica code.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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