Generate TeX Snippets

From Isabelle Community Wiki
Jump to navigation Jump to search

Usually Isabelle's document preparation system directly generates PDF files from theory files. Sometimes (e.g., when writing a document in cooperation with people not using Isabelle) it is needed to generate only TeX snippets and include them into a normal TeX document.

The idea is to add a TeX command that marks the parts of a theory file that should be exported. The generated file is then transformed by an easy sed invocation. Afterwards, the snippets can simply be used in the TeX file by calling \Snippet{<snippet_name>}.

To annotate a snippet in an Isabelle theory use

 text ‹
 \DefineSnippet{filter_simps}{
    @{thm [display] filter.simps}
    @{thm [display] (concl) filter_True}
 }%EndSnippet
 ›

when referring to existing facts, and

 text_raw ‹\DefineSnippet{<snippet_name>}{›
 ...
 text_raw ‹}%EndSnippet›

when referring to arbitrary parts of a theory file in theory mode and proof mode, respectively (i.e., out- or inside of a proof).

At the beginning of your main TeX file add:

 \usepackage{ifthen}
 
 \newcommand{\DefineSnippet}[2]{%
   \expandafter\newcommand\csname snippet--#1\endcsname{%
     \begin{quote}
     \begin{isabelle}
     #2
     \end{isabelle}
     \end{quote}}}
 \newcommand{\Snippet}[1]{%
   \ifcsname snippet--#1\endcsname{\csname snippet--#1\endcsname}%
   \else+++++++ERROR: Snippet ``#1 not defined+++++++ \fi}
 
 \input{generated/snippets}

Then do the following:

Create a file ROOT containing

 session Snippets = "HOL" +
   options [
     document = "pdf",
     document_output = "generated"]
   theories
     File
   document_files
     build

which tells the system to generate files in the subdirectory "generated". Then create a directory "document" containing an executable file "build" with the following content:

 #!/bin/bash
 find . -type f -name "*.tex" -exec sed -n '/DefineSnippet/,/EndSnippet/p' {} + > ../snippets.tex
 touch ../document.pdf

This script will be run in a subdirectory of "generated", hence the occurrences of ".." to refer to "generated".

In order to generate the snippets run

 isabelle build -d . Snippets

and then compile your TeX sources as usual.

Necessary imports

Your main *.tex file needs to import at least the package

 \usepackage{isabelle}

which resides in

 isabelle.sty

which in turn depends on

 comment.sty

These files (and others) can be generated by invoking

 isabelle latex -o sty

Note that the latex distribution also has a comment.sty, but that one is in conflict with Isabelle's. Therefore, do not import this package yourself, that is, do not write this:

\usepackage{comment}

Style

You can choose a style for your code, e.g. italic:

   \isabellestyle{it}