Commits

Dan Drake  committed fd22f76

Imagemagick option implemented; needs documentation though

  • Participants
  • Parent commits 2fbd378

Comments (0)

Files changed (3)

 * magic bits written to .sage so we can keep the Python module in the
   same place as the .sty file?
 
-* add option to use Imagemagick to convert bimaps to eps?
+* document imagemagick option
 
+* improve manual
 
 
 \documentclass{article}
 \title{Examples of embedding Sage in \LaTeX}
+\author{\relax}
+
 \usepackage{sagetex}
+% 
+% If you want sagetex to use Imagemagick's convert utility to make eps
+% files from png files when generating a dvi file, add the
+% "imagemagick" option above:
+%
+%    \usepackage[imagemagick]{sagetex}
 
 \begin{document}
 \maketitle
 \sageplot{graphs.FlowerSnark().plot()}
 
 \begin{sageblock}
-G4 = DiGraph({1:[2,2,3,5], 2:[3,4], 3:[4], 4:[5,7], 5:[6]}, multiedges=True)
+G4 = DiGraph({1:[2,2,3,5], 2:[3,4], 3:[4], 4:[5,7], 5:[6]},\
+             multiedges=True)
 G4plot = G4.plot(layout='circular')
 \end{sageblock}
 
 
  matrixprogram = matrix_plot(M,cmap='Greys')
 \end{sageblock}
-And here the picture:
+And here's the picture:
 
 \sageplot{matrixprogram}
 
 \verb|sageplot| command defaults to EPS and PDF, \emph{you must specify
 a valid format for 3D plotting}. Sage right now can't produce EPS or PDF
 files from plot3d objects, so if you don't specify a valid format,
-things will go badly.
+things will go badly. You can specify the ``\texttt{imagemagick}''
+option, which will use the Imagemagick \texttt{convert} utility to make
+EPS files. See the documentation for details.
 
 Here's the famous Sage cube graph:
 

File sagetexpackage.dtx

 % \centerline{\framebox[2cm]{\rule[-1cm]{0cm}{2cm}\textbf{??}}}
 % 
 % \noindent That's supposed to resemble the traditional ``\textbf{??}''
-% that \LaTeX{} uses to indicate missing reference, and also the
+% that \LaTeX{} uses to indicate missing references, and also the
 % image-not-found graphics used by web browsers.
 %
 % You needn't worry about the filenames; they are automatically
 %    \begin{macrocode}
 \RequirePackage{verbatim}
 %    \end{macrocode}
-% Unsurprisingly, the |sageplot| command works poorly without graphics
+% Unsurprisingly, the |\sageplot| command works poorly without graphics
 % support.
 %    \begin{macrocode}
 \RequirePackage{graphicx}
 %    \end{macrocode}
-% The |makecmds| package gives us a |provideenvironment| which we need,
-% and we use |ifpdf| and |ifthen| in |sageplot| so we know what kind of
+% The |makecmds| package gives us a |\provideenvironment| which we need,
+% and we use |ifpdf| and |ifthen| in |\sageplot| so we know what kind of
 % files to look for.
 %    \begin{macrocode}
 \RequirePackage{makecmds}
 \setlength{\sagetexindent}{5ex}
 %    \end{macrocode}
 %
+% \begin{macro}{\@epsmagick}
+% By default, we don't use ImageMagick to create EPS files:
+%    \begin{macrocode}
+\newcommand{\@epsmagick}{False}
+%    \end{macrocode}
+% That macro gets put into a Python function call, so it works to have
+% it be one of the strings ``|True|'' or ``|False|''.
+% \end{macro}
+% 
+% Declare the |imagemagick| option and process it:
+%    \begin{macrocode}
+\DeclareOption{imagemagick}{\renewcommand{\@epsmagick}{True}}
+\ProcessOptions\relax
+%    \end{macrocode}
+% The |\relax| is a little incantation suggested by the ``\LaTeX2e for
+% class and package writers'' manual, section 4.7.
+%
 % It's time to deal with files. Open the |.sage| file:
 %    \begin{macrocode}
 \newwrite\@sagefile
 \def\sageplot@ii[#1][#2]#3{%
 \@wsf{try:}%
 \@wsf{  sagetex.initplot('\jobname')}%
-\@wsf{  sagetex.sageplot(\the@sageplot, \the\inputlineno, #3, format='#2')}%
+\@wsf{  sagetex.sageplot(\the@sageplot, \the\inputlineno, #3,
+    format='#2', epsmagick=\@epsmagick)}%
 \@wsf{except:}%
 \@wsf{  sagetex.goboom(\the\inputlineno)}%
 %    \end{macrocode}
 %    \end{macrocode}
 % \end{macro}
 %
-% \begin{macro}{\@sagetexincludegrphics}
+% \begin{macro}{\@sagetexincludegraphics}
 % This command includes the requested graphics file with the requested
 % options if the file exists (note that it just needs to know the
 % extension, since we use a counter in the filename).
     {\framebox[2cm]{\rule[-1cm]{0cm}{2cm}\textbf{??}}%
      \PackageWarning{sagetex}{Graphics file
      \@plotdir/plot-\the@sageplot\space on page \thepage\space does not
-     exist}}%
-     \gdef\@rerunsage{x}}
+     exist}%
+     \gdef\@rerunsage{x}}}
 %    \end{macrocode}
 % \end{macro}
 %
 import os.path
 import hashlib
 import traceback
+import subprocess
 initplot_done  = False
 dirname        = None
 filename       = ""
 % for keyword arguments. The |#3| argument to |\sageplot| becomes
 % |p| and |**kwargs| below.
 %    \begin{macrocode}
-def sageplot(counter, line, p, format='notprovided', **kwargs):
+def sageplot(counter, line, p, format='notprovided', epsmagick=False, **kwargs):
   global dirname
   progress('Plot %s, line %s' % (counter, line))
 %    \end{macrocode}
     #print('  plotting %s with args %s' % (plotfilename, kwargs))
     p.save(filename=plotfilename, **kwargs)
 %    \end{macrocode}
+% If the user provides a format \emph{and} specifies the |imagemagick|
+% option, we try to convert the newly-created file into EPS format.
+%    \begin{macrocode}
+    if format != 'notprovided' and epsmagick is True:
+      print('calling toeps(%s, %s)...' % (counter, format))
+      toeps(counter, format)
+%    \end{macrocode}
+% \end{macro}
+%
+% \begin{macro}{toeps}
+% This function calls the Imagmagick utility |convert| to, well, convert
+% something into EPS format. This gets called when the user has
+% requested the ``|imagemagick|'' option to the \ST\ style file and is
+% making a graphic file with a nondefault extension.
+%    \begin{macrocode}
+def toeps(counter, ext):
+  global dirname
+  subprocess.check_call(['convert',\
+    '%s/plot-%s.%s' % (dirname, counter, ext), \
+    '%s/plot-%s.eps' % (dirname, counter)])
+%    \end{macrocode}
+% We are blindly assuming that the |convert| command exists and will do
+% the conversion for us; the |check_call| function raises an exception
+% which, since all these calls get wrapped in try/excepts in the |.sage|
+% file, should result in a reasonable error message if something strange
+% happens.
 % \end{macro}
 %
 % \begin{macro}{goboom}