Skip to content

F1. A figure in a report ​

Goal: a figure and the text a report writes around it, the image's alternative text and its caption, from the program that draws it.

fortran
program pf01_figure
!< A figure and what a report needs of it: `save(file, info=meta)`, then the Markdown of the figure.
use foresight, only : figure_info, figure_object, I4P, R8P
implicit none
type(figure_object) :: fig     !< Figure.
type(figure_info)   :: meta    !< What the report needs of it.
real(R8P)           :: t(50)   !< Time.
integer(I4P)        :: k       !< Counter.

t = [(0.2_R8P * k, k = 1, 50)]
call fig%init(width=600_I4P, height=320_I4P)
call fig%set_title('Decay')
call fig%set_xlabel('time [s]')
call fig%set_ylabel('amplitude')
call fig%plot(t, exp(-t / 3.0_R8P) * cos(2.0_R8P * t), title='measured')
call fig%plot(t, exp(-t / 3.0_R8P), title='envelope')
call fig%set_caption('A damped oscillation and its envelope, exp(-t/3).')
call fig%save('pf01.svg', info=meta)

print '(A)', 'file:    '//meta%file
print '(A)', 'format:  '//meta%format
print '(A,F0.1,A,F0.1,A,F0.2,A,F0.2,A)', 'size:    ', meta%width_px, ' x ', meta%height_px, ' px, ', &
                                         meta%width_cm, ' x ', meta%height_cm, ' cm'
print '(A)', 'alt:     '//meta%alt
print '(A)', 'caption: '//meta%caption
print '(A)', ''
print '(A)', '!['//meta%alt//']('//meta%file//')'
print '(A)', ''
print '(A)', '*Figure 1. '//meta%caption//'*'
endprogram pf01_figure
console
$ pf01_figure
file:    pf01.svg
format:  svg
size:    600.0 x 320.0 px, 15.88 x 8.47 cm
alt:     Plot: Decay. x axis: time [s]. y axis: amplitude. Series: measured, envelope.
caption: A damped oscillation and its envelope, exp(-t/3).

![Plot: Decay. x axis: time [s]. y axis: amplitude. Series: measured, envelope.](pf01.svg)

*Figure 1. A damped oscillation and its envelope, exp(-t/3).*
pf01
open SVG ↗

save('pf01.svg', info=meta) writes the figure and returns a figure_info: the file and its format, the size in pixels and in centimetres, an alternative text and the caption. The last three lines are the figure in a Markdown report: an image whose alternative text describes it, its caption in italics below.

Why it works ​

  • The alternative text is made of the texts already in the figure: the title, each axis label, the key titles, as sentences (Plot: Decay. x axis: time [s]. ...). A screen reader reads it, a renderer shows it when the image is missing, a search engine indexes it. A multiplot gives one sentence group per panel (Panel 2, plot: drag. ...), a 3D plot says Surface plot.
  • set_caption (in scripts set caption "...", a foresight extension) is not drawn: a caption belongs to the document, below the image or in LaTeX's \caption, where the document numbers it.
  • The size in centimetres is the page size at 96 px to the inch, the size of the PDF page: what a converter needs to print the figure at its size (F4).

Try this ​

  • Save to pf01.pdf and pf01.tex: meta%format follows, and for the LaTeX picture meta%graphics names the PDF it includes.
  • Leave out the title: the alternative text starts with Plot.; leave out every text and it is Plot. alone, a hint that the figure needs labels.

Pitfalls ​

  • gnuplot's enhanced markup is dropped from the alternative text (c_d reads cd), TeX math is kept as written: a screen reader spells $\alpha$ out. Write the caption for the reader who cannot see the figure.
  • info is filled by save: a figure saved twice returns the second file's information.

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