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 tablegnuplot
# 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 tableconsole
$ 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:
| 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 |
and the figure the same script draws from the same file:
Why it works
set table 'FILE' markdownwrites 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 markdowndoes it for one item.- The header is the data's: with
set datafile columnheadersa 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 markdownwrites into a datablock:print $Tshows it.- Append to an open report:
set table 'report.md' append markdown(F3).
Pitfalls
- A table of
with tablewrites numbers as%g(gnuplot's):set formatapplies to a plot's table only. Format withsprintfin theusingfield. - A computed field is named as written: put the quantity in the data file, with its name, when the header matters.
splottables stay gnuplot's text:markdownrefuses them.