home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / source / gnuplotp.lzh / gnuplot#1.1 / docs / doc2tex.c next >
Encoding:
C/C++ Source or Header  |  1990-10-12  |  4.6 KB  |  231 lines

  1. /*
  2.  * doc2tex.c  -- program to convert Gnuplot .DOC format to LaTeX document
  3.  * Also will work for VMS .HLP files. 
  4.  * Modified by Russell Lang from hlp2ms.c by Thomas Williams 
  5.  * Extended by David Kotz to support quotes ("), backquotes, tables.
  6.  *
  7.  * usage:  doc2tex < file.doc > file.tex
  8.  *
  9.  *   where file.doc is a Gnuplot .DOC file, and file.tex will be an
  10.  *     article document suitable for printing with LaTeX.
  11.  *
  12.  * typical usage for GNUPLOT:
  13.  *
  14.  *   doc2tex < gnuplot.doc > gnuplot.tex 
  15.  *   latex gnuplot.tex ; latex gnuplot.tex
  16.  */
  17.  
  18. static char rcsid[] = "$Id: doc2tex.c,v 1.1 90/01/11 15:44:06 dfk Exp Locker: dfk $";
  19.  
  20. #include <stdio.h>
  21. #include <ctype.h>
  22.  
  23. #define MAX_NAME_LEN    256
  24. #define MAX_LINE_LEN    256
  25. #define TRUE 1
  26. #define FALSE 0
  27.  
  28. typedef int boolean;
  29.  
  30. boolean intable = FALSE;
  31. boolean verb = FALSE;
  32.  
  33. main()
  34. {
  35.     init(stdout);
  36.     convert(stdin,stdout);
  37.     finish(stdout);
  38.     exit(0);
  39. }
  40.  
  41.  
  42. init(b)
  43. FILE *b;
  44. {
  45.     (void) fputs("\\input{titlepage.tex}\n",b);
  46. }
  47.  
  48.  
  49. convert(a,b)
  50.     FILE *a,*b;
  51. {
  52.     static char line[MAX_LINE_LEN];
  53.  
  54.     while (fgets(line,MAX_LINE_LEN,a)) {
  55.        process_line(line, b);
  56.     }
  57. }
  58.  
  59. process_line(line, b)
  60.     char *line;
  61.     FILE *b;
  62. {
  63.     switch(line[0]) {        /* control character */
  64.        case '?': {            /* interactive help entry */
  65.           break;            /* ignore */
  66.        }
  67.        case '@': {            /* start/end table */
  68.           if (intable) {
  69.              (void) fputs("\\hline\n\\end{tabular}\n", b);
  70.              (void) fputs("\\end{center}\n",b);
  71.              intable = FALSE;
  72.           } else {
  73.              if (verb) {
  74.                 (void) fputs("\\end{verbatim}\n",b);
  75.                 verb=FALSE;
  76.              } 
  77.              (void) fputs("\n\\begin{center}\n", b);
  78.              (void) fputs("\\begin{tabular}{|ccl|} \\hline\n", b);
  79.              intable = TRUE;
  80.           }
  81.           /* ignore rest of line */
  82.           break;
  83.        }
  84.        case '#': {            /* latex table entry */
  85.           if (intable)
  86.             (void) fputs(line+1, b); /* copy directly */
  87.           else
  88.             fprintf(stderr, "error: # line found outside of table\n");
  89.           break;
  90.        }
  91.        case '%': {            /* troff table entry */
  92.           break;            /* ignore */
  93.        }
  94.        case '\n':            /* empty text line */
  95.        case ' ': {            /* normal text line */
  96.           if (intable)
  97.             break;        /* ignore while in table */
  98.           if (line[1] == ' ') {
  99.              /* verbatim mode */
  100.              if (!verb) {
  101.                 (void) fputs("\\begin{verbatim}\n",b);
  102.                 verb=TRUE;
  103.              }
  104.              (void) fputs(line+1,b); 
  105.           } else {
  106.              if (verb) {
  107.                 (void) fputs("\\end{verbatim}\n",b);
  108.                 verb=FALSE;
  109.              } 
  110.              puttex(line+1,b);
  111.           }
  112.           break;
  113.        }
  114.        default: {
  115.           if (isdigit(line[0])) { /* start of section */
  116.              if (!intable)    /* ignore while in table */
  117.                section(line, b);
  118.           } else
  119.             fprintf(stderr, "unknown control code '%c' in column 1\n", 
  120.                   line[0]);
  121.           break;
  122.        }
  123.     }
  124. }
  125.  
  126. /* process a line with a digit control char */
  127. /* starts a new [sub]section */
  128.  
  129. section(line, b)
  130.     char *line;
  131.     FILE *b;
  132. {
  133.     static char string[MAX_LINE_LEN];
  134.     int sh_i;
  135.  
  136.     if (verb) {
  137.        (void) fputs("\\end{verbatim}\n",b);
  138.        verb=FALSE;
  139.     } 
  140.     (void) sscanf(line,"%d %[^\n]s",&sh_i,string);
  141.     switch(sh_i)
  142.      {
  143.         case 1: 
  144.         (void) fprintf(b,"\\section{");
  145.         break;
  146.         case 2: 
  147.         (void) fprintf(b,"\\section{");
  148.         break;
  149.         case 3:
  150.         (void) fprintf(b,"\\subsection{");
  151.         break;
  152.         case 4: 
  153.         (void) fprintf(b,"\\subsubsection{");
  154.         break;
  155.         default:
  156.         case 5: 
  157.         (void) fprintf(b,"\\paragraph{");
  158.         break;
  159.      }
  160.     if (islower(string[0]))
  161.      string[0] = toupper(string[0]);
  162.     puttex(string,b);
  163.     (void) fprintf(b,"}\n");
  164. }
  165.  
  166. /* put text in string str to file while buffering special TeX characters */
  167. puttex(str,file)
  168. FILE *file;
  169. register char *str;
  170. {
  171. register char ch;
  172. static boolean inquote = FALSE;
  173.  
  174.      while( (ch = *str++) != '\0') {
  175.          switch(ch) {
  176.              case '#':
  177.              case '$':
  178.              case '%':
  179.              case '&':
  180.              case '_':
  181.              case '{':
  182.              case '}':
  183.                  (void) fputc('\\',file);
  184.                  (void) fputc(ch,file);
  185.                  break;
  186.              case '\\':
  187.                  (void) fputs("$\\backslash$",file);
  188.                  break;
  189.              case '~':
  190.                  (void) fputs("\\~{\\ }",file);
  191.                  break;
  192.              case '^':
  193.                  (void) fputs("\\verb+^+",file);
  194.                  break;
  195.              case '>':
  196.              case '<':
  197.              case '|':
  198.                  (void) fputc('$',file);
  199.                  (void) fputc(ch,file);
  200.                  (void) fputc('$',file);
  201.                  break;
  202.              case '"': 
  203.                  /* peek at next character: if space, end of quote */
  204.                  if (*str == NULL || isspace(*str) || ispunct(*str))
  205.                    (void) fputs("''", file);
  206.                  else
  207.                    (void) fputs("``", file);
  208.                  break;
  209.              case '`':    /* backquotes mean boldface */
  210.                  if (inquote) {
  211.                     fputs("}", file);
  212.                     inquote = FALSE;
  213.                  } else {
  214.                     fputs("{\\bf ", file);
  215.                     inquote = TRUE;
  216.                  }
  217.                  break;
  218.              default:
  219.                  (void) fputc(ch,file);
  220.                  break;
  221.          }
  222.      }
  223. }
  224.  
  225.  
  226. finish(b)
  227. FILE *b;
  228. {
  229.     (void) fputs("\\end{document}\n",b);
  230. }
  231.