Skip to content

E1. Running a script ​

Goal: a gnuplot script you already have, run by foresight, and kept running in gnuplot too.

gnuplot
# one script for gnuplot and foresight: the terminal chosen by what the program has
if (strstrt(GPVAL_TERMINALS, 'html') > 0) {
   set terminal svg size 600,360    # foresight: html, svg, pdfcairo, cairolatex, dumb, block
   set output 'pe01.svg'
} else {
   set terminal pngcairo size 600,360           # gnuplot
   set output 'pe01.png'
}
set title 'Bessel functions'
set xlabel 'x'
set key top right
set samples 200
plot [0:20] besj0(x) title 'J_0(x)', besj1(x) title 'J_1(x)' dt 2, besy0(x) title 'Y_0(x)' dt 3
console
$ foresight pe01_both.gp
foresight: pe01.svg
pe01
open SVG ↗

gnuplot pe01_both.gp runs the same file and writes pe01.png with gnuplot's pngcairo.

Why it works ​

  • foresight SCRIPT reads gnuplot 6.0's command language: the same set, plot, splot, fit, stats, the same expressions, functions (besj0, besy0, ...) and enhanced text (J_0), abbreviations included.
  • GPVAL_TERMINALS lists the terminals of the program that runs the script; html is foresight's alone, so strstrt(GPVAL_TERMINALS, 'html') > 0 tells the two apart.
  • -e runs commands before the script (foresight -e "set logscale y" run.gp), -c passes arguments as ARG1 ... ARG9, as gnuplot -c; without set output the output is the script's name with .html, or -o FILE.

Try this ​

  • foresight -c pe01_both.gp with an argument read as ARG1 in the title: set title ARG1.
  • foresight --watch pe01_both.gp: the script runs again whenever it or its data change (B2).

Pitfalls ​

  • A terminal foresight does not have (pngcairo, qt, wxt, x11) stops the script at its line: choose the terminal as above, or leave it to the command line (foresight -o plot.svg script.gp).
  • pause -1 waits for a line on the standard input, as in gnuplot: a script meant for a batch job should not have it (pause mouse only writes its message: there is no window to click).

Reference: Command Line, gnuplot subset. Next: E2. Terminals and outputs.

foresight is available under your choice of license: GPL-3.0-or-later, BSD-2-Clause, BSD-3-Clause or MIT (License).