2 static char *RCSid() { return RCSid("$Id: doc2tex.c,v 1.21 2006/08/13 17:23:23 mikulik Exp $"); }
5 /* GNUPLOT - doc2tex.c */
8 * Copyright 1986 - 1993, 1998, 2004 Thomas Williams, Colin Kelley
10 * Permission to use, copy, and distribute this software and its
11 * documentation for any purpose with or without fee is hereby granted,
12 * provided that the above copyright notice appear in all copies and
13 * that both that copyright notice and this permission notice appear
14 * in supporting documentation.
16 * Permission to modify the software is granted, but not the right to
17 * distribute the complete modified source code. Modifications are to
18 * be distributed as patches to the released version. Permission to
19 * distribute binaries produced by compiling modified sources is granted,
21 * 1. distribute the corresponding source modifications from the
22 * released version in the form of a patch file along with the binaries,
23 * 2. add special version identification to distinguish your version
24 * in addition to the base release version number,
25 * 3. provide your name and address as the primary contact for the
26 * support of your modified version, and
27 * 4. retain our contact information in regard to use of the base
29 * Permission to distribute the released version of the source code along
30 * with corresponding source modifications in the form of a patch file is
31 * granted with same provisions 2 through 4 for binary distributions.
33 * This software is provided "as is" without express or implied warranty
34 * to the extent permitted by applicable law.
38 * doc2tex.c -- program to convert Gnuplot .DOC format to LaTeX document
39 * Also will work for VMS .HLP files.
40 * Modified by Russell Lang from hlp2ms.c by Thomas Williams
41 * Extended by David Kotz to support quotes ("), backquotes, tables.
42 * Extended by Jens Emmerich to handle '_', '---', paired single
43 * quotes. Changed "-handling. Added pre/post-verbatim hooks.
46 * usage: doc2tex [file.doc [file.tex]]
48 * where file.doc is a Gnuplot .DOC file, and file.tex will be an
49 * article document suitable for printing with LaTeX.
51 * typical usage for GNUPLOT:
53 * doc2tex gnuplot.doc gnuplot.tex
54 * latex gnuplot.tex ; latex gnuplot.tex
65 void init __PROTO((FILE *));
66 void convert __PROTO((FILE *, FILE *));
67 void process_line __PROTO((char *, FILE *));
68 void section __PROTO((char *, FILE *));
69 void puttex __PROTO((char *, FILE *));
70 void finish __PROTO((FILE *));
72 static TBOOLEAN intable = FALSE;
73 static TBOOLEAN verb = FALSE;
74 static TBOOLEAN see = FALSE;
75 static TBOOLEAN inhref = FALSE;
78 main (int argc, char **argv)
87 fprintf(stderr, "Usage: %s [infile [outfile]]\n", argv[0]);
91 if ((infile = fopen(argv[1], "r")) == (FILE *) NULL) {
92 fprintf(stderr, "%s: Can't open %s for reading\n",
98 if ((outfile = fopen(argv[2], "w")) == (FILE *) NULL) {
99 fprintf(stderr, "%s: Can't open %s for writing\n",
105 convert(infile, outfile);
114 (void) fputs("\\input{titlepag.tex}\n", b);
119 convert(FILE *a, FILE *b)
121 static char line[MAX_LINE_LEN+1];
123 while (get_line(line, sizeof(line), a))
124 process_line(line, b);
129 process_line( char *line, FILE *b)
131 char string[MAX_LINE_LEN+1], c;
134 static TBOOLEAN parsed = FALSE;
136 initlen = strlen(line);
137 switch (line[0]) { /* control character */
138 case '?': /* interactive help entry */
139 /* convert '?xxx' to '\label{xxx}' */
140 line[strlen(line)-1]=NUL;
141 (void) fputs("\\label{",b);
143 (void) fputs("}\n",b);
144 if (!strpbrk(line+1," ")) { /* Make an index entry also */
145 (void) fputs("\\index{",b);
146 while ((ind = strpbrk(line+1,"-_")))
149 (void) fputs("}\n",b);
151 break; /* ignore */ /* <- don't ignore */
153 case '=': /* explicit index entry */
154 line[strlen(line)-1]=NUL;
155 (void) fputs("\\index{",b);
156 while ((ind = strpbrk(line+1,"-_")))
159 (void) fputs("}\n",b);
162 case '@':{ /* start/end table */
164 (void) fputs("\\hline\n\\end{tabular}\n", b);
165 (void) fputs("\\end{center}\n", b);
169 (void) fputs("\\end{verbatim}\n", b);
170 (void) fputs("\\postverbatim\n", b);
173 (void) fputs("\n\\begin{center}\n", b);
174 /* moved to gnuplot.doc by RCC
175 (void) fputs("\\begin{tabular}{|ccl|} \\hline\n", b);
179 /* ignore rest of line */
182 case '#':{ /* latex table entry */
184 (void) fputs(line + 1, b); /* copy directly */
186 fprintf(stderr, "warning: # line found outside of table\n");
187 fprintf(stderr, "%s\n", line + 11);
192 case '^':{ /* external link escape */
193 /* internal link escape */
194 /* convert '^ <a href="xxx">yyy</a>' to '\href{xxx}{yyy}' */
195 /* convert '^ <a href="#xxx"></a> to '\ref{xxx}' */
196 /* convert '^ <a name="xxx"></a> to '\label{xxx}' */
201 if (line[11] == '#') {
204 for (i = 12; (c = line[i]) != '"'; i++) {
209 for ( ; i < initlen-5; i++) {
212 fputs(" (p.~\\pageref{",b);
218 if (strstr(line,"</a>") == NULL){
219 fputs("\\par\\hskip2.7em\\href{",b);
224 for (i = 11; i < initlen-1 ; i++){
228 } else if ( c == '>' && parsed == 0) {
229 fputs("}{\\tt ",b); parsed = 1;
230 } else if ( c == '~') {
232 } else if ( c == '_' && parsed == 1) {
234 } else if ( c == '<' && parsed == 1) {
247 for (i = 11; (c = *(line +i)) != '"'; i++) {
259 if ( line[4] == 'a') {
269 case '%':{ /* troff table entry */
272 case '\n': /* empty text line */
273 case ' ':{ /* normal text line */
275 break; /* ignore while in table */
276 if ( inhref == TRUE){
280 if (line[1] == ' ') {
283 (void) fputs("\\preverbatim\n", b);
284 (void) fputs("\\begin{verbatim}\n", b);
287 (void) fputs(line + 2, b);
290 (void) fputs("\\end{verbatim}\n", b);
291 (void) fputs("\\postverbatim\n", b);
295 puttex(line, b); /* handle totally blank line */
302 if (isdigit((int) line[0])) { /* start of section */
303 if (!intable) /* ignore while in table */
306 fprintf(stderr, "unknown control code '%c' in column 1\n",
313 /* process a line with a digit control char */
314 /* starts a new [sub]section */
317 section(char *line, FILE *b)
319 static char string[MAX_LINE_LEN+1];
323 (void) fputs("\\end{verbatim}\n", b);
324 (void) fputs("\\postverbatim\n", b);
327 (void) sscanf(line, "%d %[^\n]s", &sh_i, string);
330 (void) fprintf(b, "\\part{");
333 (void) fprintf(b, "\\section{");
336 (void) fprintf(b, "\\subsection{");
339 (void) fprintf(b, "\\subsubsection{");
342 (void) fprintf(b, "\\paragraph{");
345 (void) fprintf(b, "\\subparagraph{");
351 if (islower((int) string[0]))
352 string[0] = toupper(string[0]);
354 (void) fprintf(b, "}\n");
357 /* put text in string str to file while buffering special TeX characters */
359 puttex( char *str, FILE *file)
362 char string[MAX_LINE_LEN+1], c;
363 static TBOOLEAN inquote = FALSE;
366 while ((ch = *str++) != NUL) {
374 (void) fputc('\\', file);
375 (void) fputc(ch, file);
378 (void) fputs("$\\backslash$", file);
381 (void) fputs("\\~{\\ }", file);
384 (void) fputs("\\verb+^+", file);
389 (void) fputc('$', file);
390 (void) fputc(ch, file);
391 (void) fputc('$', file);
394 (void) fputs("{\\tt\"}", file);
398 (void) fputs("{'\\,'}", file);
401 (void) fputc(ch, file);
405 if ((*str == '-') && (*(str + 1) == '-')) {
406 (void) fputs(" --- ", file);
409 (void) fputc(ch, file);
412 case '`': /* backquotes mean boldface */
415 char *index = string;
417 (void) fputs(" (p.~\\pageref{", file);
418 (void) fputs(string, file);
419 (void) fputs("})", file);
421 /* Make the final word an index entry also */
422 fputs("\\index{",file);
424 /* Aug 2006: no need to split index words at - or _ */
425 if (strrchr(index,'-'))
426 index = strrchr(index,'-')+1;
427 if (strrchr(index,'_'))
428 index = strrchr(index,'_')+1;
430 if (strrchr(index,' '))
431 index = strrchr(index,' ')+1;
432 while ((s = strchr(index,'_')) != NULL) /* replace _ by space */
439 (void) fputs("}", file);
442 (void) fputs("{\\bf ", file);
443 for (i=0; i<MAX_LINE_LEN && ((c=str[i]) != '`') ; i++){
450 case '_': /* emphasised text ? */
451 for (i = 0; isalpha((int) (*(str + i))); i++);
452 if ((i > 0) && (*(str + i) == '_') &&
453 isspace((int) (*(str + i + 1)))) {
454 (void) fputs("{\\em ", file);
455 for (; *str != '_'; str++) {
456 (void) fputc(*str, file);
459 (void) fputs("\\/}", file);
461 (void) fputs("\\_", file);
464 case 's': /* find backquote after 'see' {see `} */
466 (void) fputc(ch, file);
467 if ( str[0] == 'e' && str[1] == 'e' && isspace(str[2])){
475 (void) fputc(ch, file);
484 (void) fputs("\\part{Index}\n", b);
485 (void) fputs("\\printindex\n", b);
486 (void) fputs("\\end{document}\n", b);