Skip to content

F2. Tables ​

Goal: the numbers behind a figure as tables a Markdown renderer shows, written by the script that plots them.

gnuplot
# the runs of a mesh study (mesh.dat: a header row, then one run per line) as a Markdown table
set datafile columnheaders
set table 'pf02_runs.md' markdown
plot 'mesh.dat' using (strcol(1)):'cells':'error':'time' with table
unset table
gnuplot
# a plot's points as a table: a series per table, its title before it, numbers in the axis formats
set table 'pf02_fit.md' markdown
set format y '%.2e'
set samples 4
plot [1e3:1e6] 3.1*x**(-2.0/3) title 'model error' with lines
unset table
console
$ foresight pf02_tables.gp && cat pf02_runs.md pf02_fit.md
foresight: pf02.svg
| case | cells | error | time |
| --- | --: | --: | --: |
| coarse | 1000 | 0.032 | 0.4 |
| medium | 8000 | 0.0081 | 2.9 |
| fine | 64000 | 0.002 | 23.5 |
| finest | 512000 | 0.00051 | 190.2 |

model error

| x | y |
| --: | --: |
| 1000 | 3.10e-02 |
| 334000 | 6.44e-04 |
| 667000 | 4.06e-04 |
| 1e+06 | 3.10e-04 |

Rendered, the first table is:

casecellserrortime
coarse10000.0320.4
medium80000.00812.9
fine640000.00223.5
finest5120000.00051190.2

and the figure the same script draws from the same file:

pf02
open SVG ↗

Why it works ​

  • set table 'FILE' markdown writes the tables of the plots that follow as Markdown pipe tables, the tables of GitHub, GitLab, pandoc and VitePress (whose markdown-it parses them back into the same cells in foresight's tests). with table markdown does it for one item.
  • The header is the data's: with set datafile columnheaders a field that reads a column whole ('cells', strcol(1), 2) takes its column's name; another field is named as written (($3*1e3)).
  • Numbers are right aligned, texts left aligned, as a reader compares them. A | in a cell is escaped.
  • A plot's table (any style but table) is the points foresight draws, a table per series after its title, the numbers in the axis formats: set format y '%.2e' above.

Try this ​

  • using 'cells':(sprintf('%.1f s', $4)): a formatted column, its header the field as written.
  • set table $T markdown writes into a datablock: print $T shows it.
  • Append to an open report: set table 'report.md' append markdown (F3).

Pitfalls ​

  • A table of with table writes numbers as %g (gnuplot's): set format applies to a plot's table only. Format with sprintf in the using field.
  • A computed field is named as written: put the quantity in the data file, with its name, when the header matters.
  • splot tables stay gnuplot's text: markdown refuses them.

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