A much more general solution with the omnipotent Mathematica, LaTeX and PSTricks. In this answer I show how to
- evaluate a function,
- factor it into the product of linear real binomials,
- plot its graph.
Step 1
Purchase Mathematica Home Edition (about USD $250), download and install it.
Step 2
Create a template named main.mtex
(for example). The extension .mtex
is important because Mathematica uses "convention over configuration" philosophy.
% main.mtex
\documentclass[preview,border=12pt,12pt]{standalone}
\usepackage{amsmath}
\usepackage{pst-plot}
\usepackage{verbatim}
\begin{comment}
<*
f[x_] := x^2 - 5 x + 6;
rep:= x_Times -> Infix[x, "*"];
*>
\end{comment}
\begin{document}
\section*{Problem}
Let $ y = f(x) = <* f[x] *>$,
\begin{enumerate}
\item factor $f(x)$
\item evaluate $f(\tfrac 1 2)$
\item plot its graph.
\end{enumerate}
\section*{Solution}
\begin{enumerate}
\item $f(x) = <* Factor[f[x]] *>$
\item $f(\tfrac 1 2)= <* f[1/2] *> $
\item \hspace{20pt}
\begin{pspicture}(0,-1)(5,3)
\psaxes{->}(0,0)(0,-1)(4.5,2.5)[$x$,0][$y$,90]
\psplot[algebraic,linecolor=blue]{1}{4}{<* f[x] /. rep *>}
\end{pspicture}
\end{enumerate}
\end{document}
Everything in <* ... *>
contains Mathematica commands and they will be replaced by invoking Splice
function that will be explained shortly.
Step 3
Create an new Mathematica document named Splicer.nb
(for example) as follows.
Splice["C:\\Users\\Donut E. Knot\\Documents\\main.mtex"];
And execute the command by pressing Shift+Enter. Mathematica will generate a file
main.tex
containing code as follows.
% main.tex
\documentclass[preview,border=12pt,12pt]{standalone}
\usepackage{amsmath}
\usepackage{pst-plot}
\usepackage{verbatim}
\begin{comment}
\text{Null}
\end{comment}
\begin{document}
\section*{Problem}
Let $ y = f(x) = x^2-5 x+6$,
\begin{enumerate}
\item factor $f(x)$
\item evaluate $f(\tfrac 1 2)$
\item plot its graph.
\end{enumerate}
\section*{Solution}
\begin{enumerate}
\item $f(x) = (x-3) (x-2)$
\item $f(\tfrac 1 2)= \frac{15}{4} $
\item \hspace{20pt}
\begin{pspicture}(0,-1)(5,3)
\psaxes{->}(0,0)(0,-1)(4.5,2.5)[$x$,0][$y$,90]
\psplot[algebraic,linecolor=blue]{1}{4}{x^2+-5*x+6}
\end{pspicture}
\end{enumerate}
\end{document}
Step 4
Compile the main.tex
with latex-dvips-ps2pdf
as it contains PSTricks code.
The output is...

Step 5
Done. :-)
l3fp
module ofexpl3
(texdoc interface3
), or at PGF.tikz
package), you can do the following:\newcommand\myMathFunction[1]{\pgfmathparse{5*#1 - (#1)^2}\pgfmathresult} \myMathFunction{2}
.