1. Dan Drake
  2. SageTeX


Dan Drake  committed fd22f76

Imagemagick option implemented; needs documentation though

  • Participants
  • Parent commits 2fbd378
  • Branches default

Comments (0)

Files changed (3)


View file
 * 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

File example.tex

View file
 \title{Examples of embedding Sage in \LaTeX}
+% 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}
-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')
  matrixprogram = matrix_plot(M,cmap='Greys')
-And here the picture:
+And here's the picture:
 \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

View file
 % \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}
 %    \end{macrocode}
-% Unsurprisingly, the |sageplot| command works poorly without graphics
+% Unsurprisingly, the |\sageplot| command works poorly without graphics
 % support.
 %    \begin{macrocode}
 %    \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}
 %    \end{macrocode}
+% \begin{macro}{\@epsmagick}
+% By default, we don't use ImageMagick to create EPS files:
+%    \begin{macrocode}
+%    \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}
+%    \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}
 \@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{  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).
      \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}