As said before, MetaPost and Asymptote are the most forthcoming answers to your questions.
MetaPost is directly derived from Metafont, the language created by Knuth for font designing, and my favorite. Same syntax as Metafont, but to the contrary of MetaFont which produces bitmap fonts, it outputs PostScript pictures by default (possibility to produce them in SVG and PNG formats), which can be directly included in PDF files. I have found it quite easy to learn and very efficient. It has been recently turned into a C-library (mplib
), and as such, sort of incorporated into the LuaTeX engine. As a consequence it can be very easily used with Context-MkiV, and with LuaLaTeX via the luamplib
package. Its biggest drawback up to now is that it is not designed for 3D drawing (some packages exist, but have not convinced me).
MetaPost in its turn directly inspired another language, namely Asymptote. It has however a different syntax, C++-like, which is much more familiar to those who are used to the C-family programming languages (which is not my case). Most importantly, it supports 3D-drawing VERY efficiently. So I turn to Asymptote each time I need a 3D-drawing.
Both languages are actively supported and evolving rapidly.
To know more about MetaPost, its manual from the authors is a good read: https://www.tug.org/docs/metapost/mpman.pdf
A very good tutorial: Learning MetaPost by doing, by André Heck, and its answers to the exercises .
Also the Metafun Manual is pretty good. Metafun is the MetaPost format used in Context, but most of it is also useful for LaTeX users.
For Asymptote, the manual from its creators is a must-read. But to learn it, I find much more useful to pick up among the many great examples of these two sites:
http://www.piprime.fr/asymptote/ (from Philippe Ivaldi, now one of the developers);
http://marris.org/asymptote/index.html (from Gaétan Marris, in French).
Best Answer
It is of course possible it depends what tools you want to use and what subset of latex you want to support (you show an easy case, if the latex has fonts and colours or explicit spacing it gets harder). Wolfram alpha for example can compute with the expression exactly as given, in latex syntax.
https://www.wolframalpha.com/input/?i=%5Cfrac%7Bcos%28x%5E2%29+-+x%7D%7Bx%5E2+%2B+1%7D
It also works with
cos
corrected to\cos
https://www.wolframalpha.com/input/?i=%5Cfrac%7B%5Ccos%28x%5E2%29+-+x%7D%7Bx%5E2+%2B+1%7D