Header menu logo FSharp.Formatting

BinderScriptNotebook

Literate Scripts

The following example shows most of the features that can be used in a literate F# script file with .fsx extension. Most of the features should be quite self-explanatory:

(**
# First-level heading
Some more documentation using `Markdown`.
*)

let helloWorld() = printfn "Hello world!"

(**
## Second-level heading
With some more documentation
*)

let numbers = [ 0 .. 99 ]
(*** include-value: numbers ***)

List.sum numbers
(*** include-it ***)

The F# script files are processed as follows:

Literate Command

Description

(** ... *)

Markdown

(*** condition: prepare ***)

Utilise a code snippet when analyzing for tooltips or executing for outputs

(*** condition: ipynb ***)

Include a code snippet when making a .ipynb notebook

(*** condition: tex ***)

Include a code snippet when making a .tex output

(*** condition: html ***)

Include a code snippet when making HTML output

(*** hide ***)

Hide the subsequent snippet

(*** raw ***)

The subsequent code is treated as raw text

Naming and including snippets

The command define defines a named snippet (such as final-sample) and removes the command together with the following F# code block from the main document. The snippet can then be referred to in 'include'. This makes it possible to write documents without the ordering requirements of the F# language.

Literate Command

Description

(*** define: snippet-name ***)

Define a named snippet

(*** include: snippet-name ***)

Include the code of the named snippet

Naming and including outputs

Literate Command

Description

(*** define-output: output-name ***)

Define a name for the outputs of the preceding snippet

(*** include-output ***)

The console output of the preceding snippet

(*** include-output: output-name ***)

The console output of the snippet (named with define-output)

(*** include-fsi-output ***)

The F# Interactive output of the preceding snippet

(*** include-fsi-output: output-name ***)

The F# Interactive output of the snippet (named with define-output)

(*** include-fsi-merged-output ***)

The merge of console output and F# Interactive output of the preceding snippet

(*** include-fsi-merged-output: output-name ***)

The merge of console output and F# Interactive output of the snippet (named with define-output)

(*** include-it ***)

The formatted result of the preceding snippet

(*** include-it: output-name ***)

The formatted result of the snippet (named with define-output)

(*** include-it-raw ***)

The unformatted result of the preceding snippet

(*** include-it-raw: output-name ***)

The unformatted result of the snippet (named with define-output)

(*** include-value: value-name ***)

The formatted value, an F# identifier name

Hiding code snippets

The command hide specifies that the following F# code block (until the next comment or command) should be omitted from the output.

Evaluating and formatting results

The commands to evaluate and format results are explained in evaluation. You must build your documentation with evaluation turned on using --eval.

Substitutions

Substitutions are applied to content, see content.

Literate Markdown Documents

For files with .md extension, the entire file is a Markdown document, which may contain F# code snippets (but also other code snippets). As usual, snippets are indented with four spaces. In addition, the snippets can be annotated with special commands. Some of them are demonstrated in the following example:

# First-level heading

    [hide]
    let print s = printfn "%s" s

Some more documentation using `Markdown`.

    [module=Hello]
    let helloWorld() = print "Hello world!"

## Second-level heading
With some more documentation

    [lang=csharp]
    Console.WriteLine("Hello world!");

When processing the document, all F# snippets are copied to a separate file that is type-checked using the F# compiler (to obtain colours and tooltips). The commands are written on the first line of the named snippet, wrapped in [...]:

LaTeX in Literate Scripts and Markdown Documents

Literate Scripts may contain LaTeX sections in Markdown using these forms:

  1. Single line latex starting with $$.

  2. A block delimited by \begin{equation}...\end{equation} or \begin{align}...\end{align}.

  3. An indented paragraph starting with $$$. This is F#-literate-specific and corresponds to \begin{equation}...\end{equation}.

For example

$$\frac{x}{y}$$

\begin{equation}
   \frac{d}{dx} \left. \left( x \left( \left. \frac{d}{dy} x y \; \right|_{y=3} \right) \right) \right|_{x=2}
\end{equation}

Becomes

\[\frac{x}{y}\]

\[ \frac{d}{dx} \left. \left( x \left( \left. \frac{d}{dy} x y \; \right|_{y=3} \right) \right) \right|_{x=2}\]

The LaTeX will also be used in HTML and iPython notebook outputs.

Making literate scripts work for different outputs

Literate scripts and markdown can be turned into LaTex, Python Notebooks and F# scripts.

A header may be needed to get the code to load, a typical example is this:

    (*** condition: prepare ***)
    #nowarn "211"
    #I "../src/FSharp.Formatting/bin/Release/netstandard2.1"
    #r "FSharp.Formatting.Common.dll"
    #r "FSharp.Formatting.Markdown.dll"
    #r "FSharp.Formatting.CodeFormat.dll"
    #r "FSharp.Formatting.Literate.dll"
    (*** condition: fsx ***)
#if FSX
    #r "nuget: FSharp.Formatting,1.0.0"
#endif // FSX
    (*** condition: ipynb ***)
#if IPYNB
    #r "nuget: FSharp.Formatting,1.0.0"
#endif // IPYNB

Processing literate files programmatically

To process files use the two static methods to turn single documents into HTML as follows using functionality from the Literate type:

open System.IO
open FSharp.Formatting.Literate

let source = __SOURCE_DIRECTORY__
let template = Path.Combine(source, "template.html")

let script = Path.Combine(source, "../docs/script.fsx")

Literate.ConvertScriptFile(script, template)

let doc = Path.Combine(source, "../docs/document.md")

Literate.ConvertMarkdownFile(doc, template)

The following sample also uses the optional parameter parameters to specify additional keywords that will be replaced in the template file (this matches the template-project.html file which is included as a sample in the package):

// Load the template & specify project information
let projTemplate = source + "template-project.html"

let projInfo =
    [ "fsdocs-authors", "Tomas Petricek"
      "fsdocs-source-link", "https://github.com/fsprojects/FSharp.Formatting"
      "fsdocs-collection-name", "F# Formatting" ]

The methods used above (Literate.ConvertScriptFile, Literate.ConvertMarkdownFile) produce HTML output by default, but they can be also used to produce LaTeX output. This is done by setting the output kind. The following example shows how to call the methods to generate LaTeX documents:

let templateTex = Path.Combine(source, "template.tex")

let scriptTex = Path.Combine(source, "../docs/script.fsx")

Literate.ConvertScriptFile(scriptTex, templateTex, outputKind = OutputKind.Latex)

let docTex = Path.Combine(source, "../docs/document.md")

Literate.ConvertMarkdownFile(docTex, templateTex, outputKind = OutputKind.Latex)

The methods used above (ConvertScriptFile, ConvertMarkdownFile) can also produce iPython Notebook output. This is done by setting the named parameter format to OutputKind.Pynb:

// Process script file, Markdown document and a directory
let scriptPynb = Path.Combine(source, "../docs/script.fsx")

Literate.ConvertScriptFile(scriptPynb, outputKind = OutputKind.Pynb)

let docPynb = Path.Combine(source, "../docs/document.md")

Literate.ConvertMarkdownFile(docPynb, outputKind = OutputKind.Pynb)

All of the three methods discussed in the previous two sections take a number of optional parameters that can be used to tweak how the formatting works

val helloWorld: unit -> unit
val printfn: format: Printf.TextWriterFormat<'T> -> 'T
val numbers: int list
Multiple items
module List from Microsoft.FSharp.Collections

--------------------
type List<'T> = | op_Nil | op_ColonColon of Head: 'T * Tail: 'T list interface IReadOnlyList<'T> interface IReadOnlyCollection<'T> interface IEnumerable interface IEnumerable<'T> member GetReverseIndex: rank: int * offset: int -> int member GetSlice: startIndex: int option * endIndex: int option -> 'T list static member Cons: head: 'T * tail: 'T list -> 'T list member Head: 'T with get member IsEmpty: bool with get member Item: index: int -> 'T with get ...
val sum: list: 'T list -> 'T (requires member (+) and member Zero)
namespace System
namespace System.IO
Multiple items
namespace FSharp

--------------------
namespace Microsoft.FSharp
namespace FSharp.Formatting
namespace FSharp.Formatting.Literate
val source: string
val template: string
type Path = static member ChangeExtension: path: string * extension: string -> string static member Combine: path1: string * path2: string -> string + 3 overloads static member EndsInDirectorySeparator: path: ReadOnlySpan<char> -> bool + 1 overload static member Exists: path: string -> bool static member GetDirectoryName: path: ReadOnlySpan<char> -> ReadOnlySpan<char> + 1 overload static member GetExtension: path: ReadOnlySpan<char> -> ReadOnlySpan<char> + 1 overload static member GetFileName: path: ReadOnlySpan<char> -> ReadOnlySpan<char> + 1 overload static member GetFileNameWithoutExtension: path: ReadOnlySpan<char> -> ReadOnlySpan<char> + 1 overload static member GetFullPath: path: string -> string + 1 overload static member GetInvalidFileNameChars: unit -> char array ...
<summary>Performs operations on <see cref="T:System.String" /> instances that contain file or directory path information. These operations are performed in a cross-platform manner.</summary>
Path.Combine([<System.ParamArray>] paths: string array) : string
Path.Combine(path1: string, path2: string) : string
Path.Combine(path1: string, path2: string, path3: string) : string
Path.Combine(path1: string, path2: string, path3: string, path4: string) : string
val script: string
type Literate = static member ConvertMarkdownFile: input: string * ?template: string * ?output: string * ?outputKind: OutputKind * ?prefix: string * ?fscOptions: string * ?lineNumbers: bool * ?references: bool * ?substitutions: (ParamKey * string) list * ?generateAnchors: bool * ?imageSaver: (string -> string) * ?rootInputFolder: string * ?crefResolver: (string -> (string * string) option) * ?mdlinkResolver: (string -> string option) * ?onError: (string -> unit) * ?filesWithFrontMatter: FrontMatterFile array -> unit static member ConvertScriptFile: input: string * ?template: string * ?output: string * ?outputKind: OutputKind * ?prefix: string * ?fscOptions: string * ?lineNumbers: bool * ?references: bool * ?fsiEvaluator: IFsiEvaluator * ?substitutions: (ParamKey * string) list * ?generateAnchors: bool * ?imageSaver: (string -> string) * ?rootInputFolder: string * ?crefResolver: (string -> (string * string) option) * ?mdlinkResolver: (string -> string option) * ?onError: (string -> unit) * ?filesWithFrontMatter: FrontMatterFile array -> unit static member ParseAndCheckScriptFile: path: string * ?fscOptions: string * ?definedSymbols: string list * ?references: bool * ?fsiEvaluator: IFsiEvaluator * ?parseOptions: MarkdownParseOptions * ?rootInputFolder: string * ?onError: (string -> unit) -> LiterateDocument static member ParseMarkdownFile: path: string * ?fscOptions: string * ?definedSymbols: string list * ?references: bool * ?fsiEvaluator: IFsiEvaluator * ?parseOptions: MarkdownParseOptions * ?rootInputFolder: string * ?onError: (string -> unit) -> LiterateDocument static member ParseMarkdownString: content: string * ?path: string * ?fscOptions: string * ?definedSymbols: string list * ?references: bool * ?fsiEvaluator: IFsiEvaluator * ?parseOptions: MarkdownParseOptions * ?rootInputFolder: string * ?onError: (string -> unit) -> LiterateDocument static member ParsePynbString: content: string * ?path: string * ?definedSymbols: string list * ?references: bool * ?parseOptions: MarkdownParseOptions * ?rootInputFolder: string * ?onError: (string -> unit) -> LiterateDocument static member ParseScriptString: content: string * ?path: string * ?fscOptions: string * ?definedSymbols: string list * ?references: bool * ?fsiEvaluator: IFsiEvaluator * ?parseOptions: MarkdownParseOptions * ?rootInputFolder: string * ?onError: (string -> unit) -> LiterateDocument static member ToFsx: doc: LiterateDocument * ?substitutions: (ParamKey * string) list * ?crefResolver: (string -> (string * string) option) * ?mdlinkResolver: (string -> string option) -> string static member ToHtml: doc: LiterateDocument * ?prefix: string * ?lineNumbers: bool * ?generateAnchors: bool * ?substitutions: (ParamKey * string) list * ?crefResolver: (string -> (string * string) option) * ?mdlinkResolver: (string -> string option) * ?tokenKindToCss: (TokenKind -> string) -> string static member ToLatex: doc: LiterateDocument * ?prefix: string * ?lineNumbers: bool * ?generateAnchors: bool * ?substitutions: (ParamKey * string) list * ?crefResolver: (string -> (string * string) option) * ?mdlinkResolver: (string -> string option) -> string ...
<summary> This type provides three simple methods for calling the literate programming tool. The <c>ConvertMarkdownFile</c> and <c>ConvertScriptFile</c> methods process a single Markdown document and F# script, respectively. The <c>ConvertDirectory</c> method handles an entire directory tree (looking for <c>*.fsx</c> and <c>*.md</c> files). </summary>
<namespacedoc><summary>Functionality to support literate programming for F# scripts</summary></namespacedoc>
static member Literate.ConvertScriptFile: input: string * ?template: string * ?output: string * ?outputKind: OutputKind * ?prefix: string * ?fscOptions: string * ?lineNumbers: bool * ?references: bool * ?fsiEvaluator: Evaluation.IFsiEvaluator * ?substitutions: (FSharp.Formatting.Templating.ParamKey * string) list * ?generateAnchors: bool * ?imageSaver: (string -> string) * ?rootInputFolder: string * ?crefResolver: (string -> (string * string) option) * ?mdlinkResolver: (string -> string option) * ?onError: (string -> unit) * ?filesWithFrontMatter: FSharp.Formatting.Templating.FrontMatterFile array -> unit
val doc: string
static member Literate.ConvertMarkdownFile: input: string * ?template: string * ?output: string * ?outputKind: OutputKind * ?prefix: string * ?fscOptions: string * ?lineNumbers: bool * ?references: bool * ?substitutions: (FSharp.Formatting.Templating.ParamKey * string) list * ?generateAnchors: bool * ?imageSaver: (string -> string) * ?rootInputFolder: string * ?crefResolver: (string -> (string * string) option) * ?mdlinkResolver: (string -> string option) * ?onError: (string -> unit) * ?filesWithFrontMatter: FSharp.Formatting.Templating.FrontMatterFile array -> unit
val projTemplate: string
val projInfo: (string * string) list
val templateTex: string
val scriptTex: string
[<Struct>] type OutputKind = | Html | Latex | Pynb | Fsx | Markdown member Extension: string with get
<summary> Defines the possible output types from literate script (HTML, Latex, Pynb) </summary>
union case OutputKind.Latex: OutputKind
<summary> Requests LaTeX output </summary>
val docTex: string
val scriptPynb: string
union case OutputKind.Pynb: OutputKind
<summary> Requests Notebook output </summary>
val docPynb: string

Type something to start searching.