Hi all, The attached is a proposal for a new external template for gnuplot, a graph plotting program. I have been using it for my personal use for a while, but I thought it may be better included in the main body as it is convenient.
With this template, users can directly edit the gnuplot source code from lyx, whereas its consequent graphics is automatically included in outputs. Although the attached script can generate other graphics formats as well, in the purpose of external template, it is only used for generation of an EPS graphics file from a gnuplot source code. If necessary, users can add additional converters for each graphics format. I think it should be better checked by professionals, but I hope it is useful. How do you think? Best regards, Koji
--- lib/configure.py.orig 2008-05-06 22:20:27.000000000 +0900 +++ lib/configure.py 2008-05-15 01:37:48.000000000 +0900 @@ -244,6 +244,10 @@ checkViewer('a FEN viewer and editor', ['xboard -lpf $$i -mode EditPosition'], rc_entry = [r'\Format fen fen FEN "" "%%" "%%" ""']) # + checkViewer('a Gnuplot viewer and editor', ['emacs', 'emcws', 'xemacs', 'gvim', 'kedit', 'kwrite', \ + 'kate', 'nedit', 'gedit', 'notepad'], + rc_entry = [r'\Format gnuplot gnuplot Gnuplot "" "%%" "%%" "vector"']) + # path, iv = checkViewer('a raster image viewer', ['xv', 'kview', 'gimp-remote', 'gimp']) path, ie = checkViewer('a raster image editor', ['gimp-remote', 'gimp']) addToRC(r'''\Format bmp bmp BMP "" "%s" "%s" "" @@ -499,6 +503,10 @@ checkProg('a Noteedit -> LilyPond converter', ['noteedit --export-lilypond $$i'], rc_entry = [ r'\converter noteedit lilypond "%%" ""', '']) # + checkProg('a Gnuplot -> EPS converter', ['gnuplot'], + rc_entry = [ r'\converter gnuplot eps "python -tt $$s/scripts/gnuplot.py $$i $$o" ""', '']) + # + # # FIXME: no rc_entry? comment it out # checkProg('Image converter', ['convert $$i $$o']) # --- lib/external_templates.orig 2008-05-15 01:43:46.000000000 +0900 +++ lib/external_templates 2008-05-15 01:44:46.000000000 +0900 @@ -314,3 +314,58 @@ UpdateResult "$$Tempname" FormatEnd TemplateEnd + +Template gnuplot + GuiName "gnuplot: $$AbsOrRelPathParent$$Basename" + HelpText + Create plots and graphics with Gnuplot + + 'gnuplot' binary must be found in path for it + to work correctly. + If you use emacs as an editor, you may want + to use it in the gnuplot mode. + HelpTextEnd + InputFormat gnuplot + FileFilter "*.{gnuplot,plt,plot,gnu}" + AutomaticProduction true + Transform Rotate + Transform Resize + Transform Clip + Transform Extra + Format LaTeX + Requirement "graphicx" + TransformOption Rotate RotationLatexOption + TransformOption Resize ResizeLatexOption + TransformOption Clip ClipLatexOption + TransformOption Extra ExtraOption + Option Arg "[$$Extra,$$Rotate,$$Resize,$$Clip]" + Product "\\includegraphics$$Arg{$$AbsOrRelPathMaster$$Basename.e +ps}" + UpdateFormat eps + UpdateResult "$$AbsPath$$Basename.eps" + ReferencedFile latex "$$AbsOrRelPathMaster$$Basename.eps" + ReferencedFile dvi "$$AbsPath$$Basename.eps" + FormatEnd + Format PDFLaTeX + Requirement "graphicx" + TransformOption Rotate RotationLatexOption + TransformOption Resize ResizeLatexOption + TransformOption Clip ClipLatexOption + TransformOption Extra ExtraOption + Option Arg "[$$Extra,$$Rotate,$$Resize,$$Clip]" + Product "\\includegraphics$$Arg{$$AbsOrRelPathMaster$$Basename.e +ps}" + UpdateFormat eps + UpdateResult "$$AbsPath$$Basename.eps" + ReferencedFile pdflatex "$$AbsOrRelPathMaster$$Basename.eps" + FormatEnd + Format Ascii + Product "[gnuplot: $$FName]" + FormatEnd + Format DocBook + Product "[gnuplot: $$FName]" + FormatEnd + Format LinuxDoc + Product "[gnuplot: $$FName]" + FormatEnd +TemplateEnd --- lib/scripts/gnuplot.py.orig 2008-05-15 01:46:32.000000000 +0900 +++ lib/scripts/gnuplot.py 2008-05-11 03:11:09.000000000 +0900 @@ -0,0 +1,123 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# file gnuplot.py +# This file is part of LyX, the document processor. +# Licence details can be found in the file COPYING. + +# \author Koji Yokota + +# Full author contact details are available in file CREDITS. + +# This script creates a graphic file from a GnuPlot source file. +# It is aimed to be called from external templates. +# For it to work correctly, gnuplot must be found in path. +# +# Usage: python gnuplot.py [-t type [-o option]] \ +# <GnuPlot source file> <EPS output file> +# type: terminal type +# option: comma-separated options for a given terminal type +# Default values are: +# type: "postscript" +# option: "eps enhanced color" +# (It will be reset to null once type is specified) + +import re, sys, os, getopt, tempfile + +class Error: pass + +# Get options and arguments +opts, args = getopt.getopt(sys.argv[1:], "t:o:") +try: + if (len(args) != 2): + raise Error() +except Error: + print '\n' + sys.argv[0] + print '\nThis script outputs a graphics file from a Gnuplot source file.\n' + print 'Usage:\n python ' + sys.argv[0] + ' [-t type [-o option]] \\' + print ' <gnuplot source file> <output graphics file>\n' + print ' type: terminal type (see "help terminal" in gnuplot for' + print ' available values)' + print ' option: comma-separated options for a given terminal type' + print ' (see "help terminal <terminal type>" in gnuplot for' + print ' available values)' + print '\n Default values are:' + print ' type: "postscript"' + print ' option: "eps enhanced color" (It will be reset to null' + print ' once type is specified)\n' + sys.exit() + +# Set output graphic type and options +# It's very likely that terminal type and its options are set for review +# purpose (e.g. x11) or else. This script automates specification of +# output format. +terminal_type = 'postscript' +terminal_option = ['eps', 'enhanced', 'color'] +type_specified = 0 +option_specified = 0 +for o,v in opts: + if o == '-t': + terminal_type = v + type_specified = 1 + if o == '-o': + terminal_option = v.split(',') + option_specified = 1 +if type_specified and not option_specified: + terminal_option = [] + +# Set "set terminal" and "set output" lines in the gnuplot source file +terminal_line = 'set terminal ' + terminal_type +if terminal_option != []: + for o in terminal_option: + terminal_line = terminal_line + ' ' + o +output_line = 'set output "' + args[1] + '"' + +# Definition of strings to be replaced +find_terminal = re.compile(r"(^\s*set\s+terminal.*$)",re.IGNORECASE) +find_output = re.compile(r"(^\s*set\s+output.*$)",re.IGNORECASE) +find_comment = re.compile(r"(^#|^\s*$)",re.IGNORECASE) + +# comment_out function comments out 'set terminal...' and 'set output...' +# lines and outputs other lines as is +def comment_out(line): + if find_terminal.search(line): + print find_terminal.sub(r'# \1', line[:-1]) + count_terminal = 1 + elif find_output.search(line): + print find_output.sub(r'# \1', line[:-1]) + count_output = 1 + else: + print line[:-1] + +# Flag to show whether head comment part is finished +comment_ended = 0 + +# Open a temporary file for a modified source +tmp_filename = tempfile.mktemp() +tmp_file = open(tmp_filename, 'w') +sys.stdout = tmp_file + +# Replace and output lines one by one +for line in open(args[0]): + if comment_ended == 1: + comment_out(line) + else: + if not find_comment.search(line): + # This is the first line after the comment part + # Insert new 'set terminal' and 'set output' + print terminal_line + print output_line + comment_out(line) + comment_ended = 1 + else: + # Comment part still continues, just output lines + print line[:-1] + +tmp_file.close() + +# Execute gnuplot to generate a graphics file from the source +try: + os.system('gnuplot %(tmp_filename)s' % vars()) +finally: + # Remove the temporary file + os.remove(tmp_filename) \ No newline at end of file