## Copyright (C) 2011 Fotios Kasolis
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, see .
## -*- texinfo -*-
## @deftypefn {Function File} {} getpng (@var{file_name})
## @deftypefn {Function File} {} getpng (@var{file_name}, @var{res})
## Generate @code{png} file with latex formula.
##
## Given file @var{file_name} marked with @var{#f#} followed by a latex
## formula in the form @code{$ formula $} and a newline at several places,
## @code{getpng} generates a @code{png} file with describe formula.
## Several intermediate files (@code{tex}, @code{dvi}, @code{eps}) are
## created. To use @code{genpng} you need to have ghostscript and a latex
## distribution that are accessible trough a shell as @code{gs} for linux
## platforms, @code{gswin32c} for Windows, @code{texi2dvi}, and @code{dvips}.
## The additional argument @var{res} can be used to specify the size of the
## @code{png} image in pixels (default value is 150px).
## @end deftypefn
function genpng (file_name, res)
if ( ( nargin != 1 ) && ( nargin != 2 ) )
print_usage ();
endif
if ! ( ischar (file_name) )
error ("genpng: expected string argument");
endif
if ( nargin == 1 )
res = 150;
endif
m = getmarked (file_name, "#f#");
[fpath, namebody, fext, unused] = fileparts (file_name);
for i = 1:numel (m)
fname = [namebody, num2str(i)];
if ( exist ([ fname, ".tex" ]) )
ans = input ([ "The file ", fname, ".tex exists. Do you want to delete it? (y/n): " ], "s");
if ( strcmp (ans, "y") || strcmp (ans, "yes") )
system ([ "rm ", fname, ".tex" ]);
endif
endif
fid = fopen ([ fname, ".tex" ], "a+");
fprintf (fid, "\\documentclass{article}\n\\pagestyle{empty}\n\\begin{document}\n");
fprintf (fid, m{i});
fprintf (fid, "\\end{document}");
fclose (fid);
gsexe = "gswin32c ";
if ! ( ispc () )
gsexe = "gs ";
endif
options = [ "-q -dNOPAUSE -dBATCH -sDEVICE=pngalpha -r", ...
num2str(res), " -dEPSCrop -o ", fname, ".png ", fname, ".ps" ];
system ([ "texi2dvi ", fname, ".tex"]);
system ([ "dvips -E ", fname, ".dvi"]);
system ([ gsexe, options ]);
endfor
endfunction