[Tex/LaTex] Graphing algebraic functions and converting to image (PNG)

conversiongraphspngwindows

My goal is to be able to generate, on-the-fly, a small variety of graphs–linear functions, quadratics, inequalities, multiple plots of each. Nothing more complex than what you'd see in a high school Algebra I course. I'd also like to be able to shade the graphs of inequalities. Let me know if I should open different questions for each type of function.

Let's start this question with how to graph y = 2x + 1 and convert to PNG. I don't need much detail in the graph–labeled axes, number markings and the function. A background grid would be a bonus.

I'd also like to convert to PNG images. Right now, the closest process I have is text (Let me be clear, I need graphs) in TeXWorks (Windows) generating a {standalone} PDF that I have to ultimately convert using the ImageMagick convert command.

\documentclass[preview]{standalone}
\begin{document}
Hello, World!
y = 2x + 1
\end{document}

I select pdfLaTeX and this generates a PDF. I follow this with the ImageMagick conversion:

convert myfile.pdf myimage.png

This process does work, but it's clunky. These images will be used in a web application, so is generating them on the fly with all this file I/O really the best way?

tex2png

I've tried tex2png but it hangs on the PostScript File Generation… step.

e:\downloads\tex2png>tex2png "\sum_{i=1}^N{i}=\frac{N(N+1)}{2}" result.png
[>] LaTeX File Generation...
[>] LaTeX File Compilation...
[>] PostScript File Generation...

Best Answer

This is for generating the plots. It needs the pdflatex option --shell-escape. You can use gnuplottex or pgfplots (with/without gnuplot) packages for plotting.

\documentclass[a4paper]{article}
\usepackage[miktex]{gnuplottex}
\usepackage{pgfplots}

\begin{document}
\section{GnuplotTeX}
\begin{gnuplot}[terminal=pdf, terminaloptions={font "Arial"}]
plot 2*x+1
\end{gnuplot}

\section{PGFPLOTS}

\begin{tikzpicture}
\begin{axis}[domain=-10:10, samples=50, smooth, no markers, enlargelimits=false]
\addplot {2*x+1};
\end{axis}
\end{tikzpicture}
\section{PGFPLOTS and GNUPLOT}

\begin{tikzpicture}
\begin{axis}[domain=-10:10, samples=50, smooth, no markers, enlargelimits=false]
\addplot gnuplot {2*x+1};
\end{axis}
\end{tikzpicture}
\end{document}

To produce .png files you can use standalone class with imagemagick. But my preferred way is by using write18 facility.

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{my-graph}
%
\documentclass[tikz]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[xlabel={$x-$ axis label},
ylabel={$y-$ axis label},xtick={-10,-8,-6,-4,-2,0,2,4,6,8,10},ytick={-18,-14,-10,-6,-2,2,6,10,14,18,21},
grid=both,domain=-10:10, samples=50, smooth, no markers, enlargelimits=false]
\addplot {2*x+1};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[xlabel={$x-$ axis label},
ylabel={$y-$ axis label},xtick={-10,-8,-6,-4,-2,0,2,4,6,8,10},ytick={-18,-14,-10,-6,-2,2,6,10,14,18,21},
grid=both,domain=-10:10, samples=50, smooth, no markers, enlargelimits=false]
\addplot gnuplot {2*x+1};
\end{axis}
\end{tikzpicture}
\end{document}
\end{filecontents*}
%
%pdflatexing my-graph
%
\immediate\write18{pdflatex --shell-escape my-graph}
% convert to PNG
\makeatletter
\immediate\write18{convert -density 200 -alpha on my-graph.pdf my-graph-\@percentchar02d.png}
\makeatother
\begin{document}
Check the folder containing this file. you will have \texttt{my-graph-00.png} and \texttt{my-graph-01.png} there.
\end{document}

For this image magick should have been installed. And you will find .png files in the same folder as this main file.

enter image description here

Related Question