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

(mgp-users 00280) tex in mgp



Hi!

Luigi Rizzo sent his latex.sh script some time ago for using latex in
MagicPoint. It works fine, but I thought it took too much time if the
eps-files were to be generated every time you started MagicPoint, so I've
written a simple pre-processor. It is written in C, since I don't know
sh/tsch/perl or any such script language well enough.

The usage is simple:

	mgptex presentation

generates the eps-files for the formulas as 1.eps, 2.eps ... and replaces

	\beginlatex
	%position
	<LaTeX formula>
	\endlatex

with

	%position
	%image "n.eps"

in the output file, presentation.mgp, that is generated.

Here comes the source code. Feel free the use it and change it, but if you
make any improvements, I would like to know so I can use them also...

/*

  mgptex

  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,"\\documentstyle[30pt]{foils}\n");
		fprintf(fp_tex,"\\thispagestyle{empty}\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);
  
}