Search:   Help

Navigation

Groups

LyX documentation

Edit

Shared groups

Links

GnuplotInLyX

<< | Page list | >>

Template for external gnuplots and converter scripts for LyX

With gnuplots as external material (gnuplot.xtemplate) something similar to the inkscape.xtemplate and the xfig.xtemplate happens: Labels and numbers appear in the document font, which looks better and is less confusing than having different fonts in the plot. And LaTeX math input used in gnuplot is rendered.

1.  Tested LyX and OS Versions

LyX 2.3.7 and LyX 2.4.0~RC4 with Ubuntu 22.04 and Debian Bookworm.

2.  gnuplot.xtemplate

Copy the following code lines into a file gnuplot.xtemplate and place that file into the folder $LyXDir/xtemplates:

#
# Gnuplot External Template
#
# author Tobias Hilbricht
#
# based on external templates by:
#
# author Asger Alstrup Nielsen
# author Angus Leeming
# author Johnathan Burchill
#
# Full author contact details are available in file CREDITS.


PreambleDef WarnNotFound
	%% Print a warning encased in an fbox.
	\def\lyxstripprefix#1>{}
	\newcommand{\warnNotFound}[1]{%
	        \def\lyxtempfilename{#1}%
	        \fbox{Could not find
	                \ttfamily\expandafter\lyxstripprefix\meaning\lyxtempfilename!}%
	        \typeout{Could not find \lyxtempfilename!}%
	}
PreambleDefEnd


PreambleDef InputOrWarn
	%% Input the file if it exists, using \input, else print a warning
	\newcommand{\inputOrWarn}[1]{%
	        \IfFileExists{#1}{\input{#1}}{\warnNotFound{#1}}}
PreambleDefEnd


Template Gnuplot
	GuiName "Gnuplot"
	HelpText
		A Gnuplot figure.
		Note that using this template automatically uses the 
		document text in the image (like with the Xfig template).
		The extension of the gnuplot file has to be .gp as in the
		LyX example file "gnuplot-example.gp".

		Data have to be provided in gnuplotfile.gp in "heredocs". See e. g.
		Philipp Janert, "Gnuplot in Action Second Edition" page 72.

		For an example, put the following lines in a file "withdata.gp" and 
		call "withdata.gp" using this template:

		$d << EOD
		1 2
		3 4
		5 6
		7 8
		EOD

		plot $d u 2:1 t "Messwerte" w lp pt 5 ps 1
	HelpTextEnd
	InputFormat gnuplot
	FileFilter "*.{gp}"
	AutomaticProduction true
	Transform Rotate
	Transform Resize
	Preview InstantPreview
	Format PDFLaTeX
		TransformCommand Rotate RotationLatexCommand
		TransformCommand Resize ResizeLatexCommand
		Product "$$RotateFront$$ResizeFront\\input{$$AbsOrRelPathMaster$$Basename.pdf_tex}$$ResizeBack$$RotateBack"
		UpdateFormat pdftex
		UpdateResult "$$AbsPath$$Basename.pdf_tex"
		Requirement "xcolor"
		Requirement "graphicx"
		Requirement "mathtools"
		# Preamble WarnNotFound
		# Preamble InputOrWarn
		ReferencedFile pdflatex "$$AbsOrRelPathMaster$$Basename.pdf_tex"
		ReferencedFile pdflatex "$$AbsPath$$Basename.pdf"
	FormatEnd
	Format LaTeX
		TransformCommand Rotate RotationLatexCommand
		TransformCommand Resize ResizeLatexCommand
		Product "$$RotateFront$$ResizeFront\\input{$$AbsOrRelPathMaster$$Basename.eps_tex}$$ResizeBack$$RotateBack"
		UpdateFormat pstex
		UpdateResult "$$AbsPath$$Basename.eps_tex"
		Requirement "xcolor"
		Requirement "graphicx"
		Requirement "mathtools"
		# Preamble WarnNotFound
		# Preamble InputOrWarn
		ReferencedFile latex "$$AbsOrRelPathMaster$$Basename.eps_tex"
		ReferencedFile latex "$$AbsPath$$Basename.eps"
		ReferencedFile dvi   "$$AbsPath$$Basename.eps"
	FormatEnd
	Format Ascii
		Product "[InkscapeGraphics: $$FName]"
	FormatEnd
	Format DocBook
		Product "<graphic fileref=\"$$AbsOrRelPathMaster$$Basename.eps\"></graphic>"
		UpdateFormat eps
		UpdateResult "$$AbsPath$$Basename.eps"
		ReferencedFile docbook     "$$AbsPath$$Basename.eps"
		ReferencedFile docbook-xml "$$AbsPath$$Basename.eps"
	FormatEnd
	Format XHTML
		Product "<img src=\"$$AbsOrRelPathMaster$$Basename.svg\" />"
		UpdateFormat svg
		UpdateResult "$$AbsPath$$Basename.svg"
		ReferencedFile xhtml "$$AbsPath$$Basename.svg"
	FormatEnd
TemplateEnd

3.  Converter script gnuplot2pdftex.py

Copy the following code lines into a file gnuplot2pdftex.py and place that file into the folder $LyXDir/scripts:

#!/usr/bin/python3
# -*- coding: utf-8 -*-

# file gnuplot2pdftex.py

# author Tobias Hilbricht

# This script converts a gnuplot file to two files that can be processed
# with pdflatex into high quality PDF. It requires gnuplot.
# The extension of the gnuplot file has to be .gp as in gnuplotfile.gp
# Data have to be provided in gnuplotfile.gp in "heredocs". See e. g.
# Philipp Janert, "Gnuplot in Action Second Edition" page 72

# Usage:
#   python gnuplot2pdftex.py gnuplotfile.gp
# This command generates
#   1. gnuplotfile.pdf     -- the converted PDF file (text stripped)
#   2. gnuplotfile.pdf_tex -- a TeX file that can be included in your
#                             LaTeX document using '\input{gnuplotfile.pdf_tex}'

import os, sys

# We expect one arg: the name of the gnuplotfile

if len(sys.argv) != 2:
    print("Usage: python gnuplot2pdftex.py gnuplotfile.gp")

gnuplot_file = sys.argv[1]
# Strip the extension from ${gnuplotfile}
base_name = os.path.splitext(os.path.basename(gnuplot_file))[0]
# Three letter file extension of TeX file necessary for gnuplot
gnuplot_name = base_name + ".ptx"
# TeX file extension known to LyX
lyx_name = base_name + ".pdf_tex"

# Call gnuplot    
os.system(f"gnuplot -e \"set term cairolatex pdf ; set output '{gnuplot_name}'\" {gnuplot_file}")
# Change TeX file extension 
os.rename(gnuplot_name, lyx_name)

4.  Converter script gnuplot2pstex.py

Copy the following code lines into a file gnuplot2pstex.py and place that file into the folder $LyXDir/scripts:

#!/usr/bin/python3
# -*- coding: utf-8 -*-

# file gnuplot2pstex.py

# author Tobias Hilbricht

# This script converts a gnuplot file to two files that can be processed
# with latex into high quality PDF. It requires gnuplot.
# The extension of the gnuplot file has to be .gp as in gnuplotfile.gp
# Data have to be provided in gnuplotfile.gp in "heredocs". See e. g.
# Philipp Janert, "Gnuplot in Action Second Edition" page 72

# Usage:
#   python gnuplot2pstex.py gnuplotfile.gp
# This command generates
#   1. gnuplotfile.eps     -- the converted EPS file (text stripped)
#   2. gnuplotfile.eps_tex -- a TeX file that can be included in your
#                             LaTeX document using '\input{gnuplotfile.eps_tex}'

import os, sys

# We expect one arg: the name of the gnuplotfile

if len(sys.argv) != 2:
    print("Usage: python gnuplot2pstex.py gnuplotfile.gp")

gnuplot_file = sys.argv[1]
# Strip the extension from ${gnuplotfile}
base_name = os.path.splitext(os.path.basename(gnuplot_file))[0]
# Three letter file extension of TeX file necessary for gnuplot
gnuplot_name = base_name + ".etx"
# TeX file extension known to LyX
lyx_name = base_name + ".eps_tex"

# Call gnuplot    
os.system(f"gnuplot -e \"set term cairolatex eps ; set output '{gnuplot_name}'\" {gnuplot_file}")
# Change TeX file extension 
os.rename(gnuplot_name, lyx_name)

5.  Make converter scripts known to LyX

Check if the gnuplot file format is known to LyX (Tools > Preferences > File handling > Formats). If not, add this line to $LyXDir/preferences:

\Format gnuplot     "gp, gnuplot"    "Gnuplot"     "" "" ""  "vector"	"text/plain"

In any case, add these two lines to $LyXDir/preferences:

\converter "gnuplot" "pdftex" "$${python} $$s/scripts/gnuplot2pdftex.py $$i $$o" "needauth"
\converter "gnuplot" "pstex" "$${python} $$s/scripts/gnuplot2pstex.py $$i $$o" "needauth"

6.  Test

Open a LyX document and insert gnuplot-example.gp (in the example/External_Material folder) via Insert > File > External Material > Template "Gnuplot". Read the help text displayed for further information.

7.  Consider security issues related to gnuplotfiles

Gnuplot scripts can contain commands which are potentially dangerous, e. g. cause loss of data.

8.  Categories

Category: Gnuplot External Material Converter

Edit - History - Print - Recent Changes - All Recent Changes - Search
Page last modified on 2024-04-28 12:30 UTC