# Graphing algebraic functions and converting to image (PNG)

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...

-
 For plotting functions of any kind pgfplots comes to my mind immediately. – hakaze Nov 13 '12 at 13:47 @hakaze Thanks, I've started reading the doc here ctan.mirrorcatalogs.com/graphics/pgf/contrib/pgfplots/doc/… – David Nov 13 '12 at 16:58

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]
\end{axis}
\end{tikzpicture}
\section{PGFPLOTS and GNUPLOT}

\begin{tikzpicture}
\begin{axis}[domain=-10:10, samples=50, smooth, no markers, enlargelimits=false]
\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]
\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]
\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.

-
 I copied this to TeXworks. Is this a padLaTeX solution? Compiling produced an error that suggested I add -shell-escape as a command line argument. I did this using TeXworks preferences, but the same error persists. – David Nov 13 '12 at 14:05 I added --shell-escape to every possible position--still doesn't compile. I have installed ImageMagick on my machine, but is there an argument for imagemagick? I tried adding it after --shell-escape but the error is: ! I can't find file imagemagick'. – David Nov 13 '12 at 14:25 version 6.8.0-4 2012-10-30 Q16 Features: OpenMP – David Nov 13 '12 at 16:48 I copied the whole of the second text in your answer into TeXworks, saved and ran pdflatex --escape-shell myfile.tex` and the error still persists. ! Package pgfplots Error: Sorry, the gnuplot-result file 'my-graph.pgf-plot.tab le' could not be found. Maybe you need to enable the shell-escape feature? For pdflatex, this is '>> pdflatex -shell-escape'. You can also invoke '>> gnuplot .gnuplot' manually on the respective gnuplot file.. – David Nov 13 '12 at 16:57 – David Nov 13 '12 at 16:58