Skip to content

F5. LaTeX reports ​

Goal: a report in LaTeX written by a program: each figure a LaTeX picture whose texts the report sets, in a figure environment the program writes with its caption.

fortran
call fig%save('pf05.svg')
call fig%save('pf05.tex', info=meta)
open(newunit=tex, file='pf05_figure.tex', status='replace', action='write')
write(tex, '(A)') '\begin{figure}', '  \centering', '  % '//meta%alt, &
                  '  \input{'//meta%file//'}', '  \caption{'//meta%caption//'}', '  \label{fig:friction}', &
                  '\end{figure}'
close(tex)
console
$ pf05_latex && cat pf05_figure.tex
\begin{figure}
  \centering
  % Plot. x axis: $Re$. y axis: $C_f$. Series: $0.074\,Re^{-1/5}$.
  \input{pf05.tex}
  \caption{Skin friction of a flat plate, the turbulent correlation.}
  \label{fig:friction}
\end{figure}
pf05
open SVG ↗

pf05.tex is the picture of C6 (gnuplot's cairolatex): its drawing pf05.pdf, its labels \puts LaTeX sets in the report's fonts, $Re$ and $C_f$ in its math. pf05_figure.tex is the environment around it; the report's main file \inputs it where the figure goes.

Why it works ​

  • meta%file is the picture to \input, meta%graphics the PDF it includes (pf05.pdf): a build script copies both.
  • The caption and the label come from the program, the numbering from LaTeX: Figure~\ref{fig:friction} stays right when figures move.
  • The alternative text goes in as a comment, kept with the figure for whoever edits the report.

Try this ​

  • call fig%set_cairolatex(standalone=.true.): pf05.tex becomes a whole document, compiled on its own to a PDF figure with the report's fonts (graphics pf05-inc.pdf, in meta%graphics).
  • Write one fragment per figure and a figures.tex that inputs them in order.

Pitfalls ​

  • The picture needs \usepackage{graphicx,color} in the report's preamble (C6).
  • A caption is LaTeX: escape %, &, _ and # in it, or write it in math.

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