[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

(mgp-users 00449) Re: math?



On Wed, 13 Dec 2000, Steve Robbins wrote:

> > One way is to write a small preprocessor for MagicPoint that replaces
> > LaTeX-contents with eps-files. I have written a small program that does
> > this, called mgptex.
>
> Fantastic!  What you describe is what I was looking for.
> Can you post the script here?
>
> Thanks,
> -Steve

Here's the script, which is actually a small c-program. Compile with

	gcc -o mgptex mgptex.c

or something similar. I (or someone else) should probably rewrite it in
perl or whatever. (For shortness and convenience.)

Usage:

	mgptex presentation.mgptex

Yields the file

	presentation.mgptex.mgp

/Anders
/*

  This program replaces

  \beginlatex
  %position
  <LaTeX formula>
  \endlatex

  with

  %position
  %image "n.eps"

  where n is the number of the formula and generates the formula
  as encapsulates postscript in "n.eps".

  Anders Logg, logg@math.chalmers.se
  2000-03-15

  */

#include <stdio.h>
#include <stdlib.h>

#define IDENTIFIER_START       "\\beginlatex\n"
#define IDENTIFIER_END         "\\endlatex\n"
#define LINELENGTH             1024
#define DEFAULT                0
#define START                  1
#define READING                2
#define END                    3

int main(int argc, char **argv)
{
  FILE *fp_in, *fp_out, *fp_tex;
  char cLine[LINELENGTH];
  char cCommand[LINELENGTH];
  char cOutFileName[LINELENGTH];
  char cTeXFile[LINELENGTH];
  char c;
  int  iCounter = 1;
  int  iMode = DEFAULT;
  
  /* Check arguments */
  if ( argc != 2 ){
	 printf("Usage: mgptex presentation\n");
	 exit(0);
  } 
  
  /* Generate file name */
  sprintf(cOutFileName,"%s.mgp",argv[1]);
  
  /* Open files */
  fp_in  = fopen(argv[1],"r");
  fp_out = fopen(cOutFileName,"w");

  /* Go through the file */
  while ( (c=getc(fp_in)) != EOF ){
	 
	 /* Put the character pack */
	 ungetc(c,fp_in);

	 /* Read line */
	 fgets(cLine,LINELENGTH,fp_in);

	 /* Check for start */
	 if ( strcasecmp(cLine,IDENTIFIER_START) == 0 )
		iMode = START;

	 /* Check for end */
	 if ( strcasecmp(cLine,IDENTIFIER_END) == 0 )
		iMode = END;

	 /* Parse */
	 switch (iMode){
	 case START:
		/* A comment */
		fprintf(fp_out,"%%%% LaTeX formula\n",cLine);
		/* Read next line, which is the mgp specification for	the position */
		fgets(cLine,LINELENGTH,fp_in);
		fprintf(fp_out,"%s",cLine);
		/* Put in the image */
		fprintf(fp_out,"%%image \"%d.eps\"\n",iCounter);
		/* Start the latex file */
		sprintf(cTeXFile,"%d.tex",iCounter);
		fp_tex = fopen(cTeXFile,"w");
		fprintf(fp_tex,"\\documentclass[30pt]{foils}\n");
		fprintf(fp_tex,"\\thispagestyle{empty}\n");
		fprintf(fp_tex,"\\usepackage{graphicx}\n");
		fprintf(fp_tex,"\\usepackage{psfrag}\n");
		fprintf(fp_tex,"\\begin{document}\n");
		fprintf(fp_tex,"\\huge\n");
		/* Change the mode to READING */
		iMode = READING;
		break;
	 case READING:
		fprintf(fp_tex,"%s",cLine);
		break;
	 case END:
		/* End the tex file */
		fprintf(fp_tex,"\\end{document}\n");
		fclose(fp_tex);
		/* Write a message */
		printf("\nGenerating LaTeX formula number %d.\n",iCounter);
		/* Generate the eps file */
		sprintf(cCommand,"latex %d.tex; dvips -E -o %d.eps %d.dvi\n",
				  iCounter,iCounter,iCounter);
		system(cCommand);
		/* Step the counter */
		iCounter += 1;
		/* Set the mode to DEFAULT */
		iMode = DEFAULT;
		break;
	 default:
		fprintf(fp_out,"%s",cLine);
	 }
	   
  }

  /* Write a message */
  printf("\n%d formula(s) generated.\n",iCounter-1);
  
}