Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
196 lines (127 sloc) 4.75 KB
\documentclass{article}
% Defining colors by names
\usepackage{xcolor}
% Verbatim enviroment
\usepackage{fancyvrb}
% Verbatim enviroment for unformatted source code
\usepackage{listings}
% Better font for backslash
\usepackage[T1]{fontenc}
\usepackage{hyperref}
% Providing more features than usual tabular
\usepackage{longtable}
% NOTE:
% Remember to use big letters for color codes
% Reference at http://tex.stackexchange.com/questions/18008/use-css-style-color-specifications-in-xcolor
% Identifiers
\newcommand{\id}[1]{\textcolor[HTML]{000000}{#1}}
% Strings
\newcommand{\str}[1]{\textcolor[HTML]{A31515}{#1}}
% Keywords
\newcommand{\kwd}[1]{\textcolor[HTML]{0000FF}{#1}}
% Comments
\newcommand{\com}[1]{\textcolor[HTML]{008000}{#1}}
% Operators
\newcommand{\ops}[1]{\textcolor[HTML]{000000}{#1}}
% Numbers
\newcommand{\num}[1]{\textcolor[HTML]{000000}{#1}}
% Line number
\newcommand{\lines}[1]{\textcolor[HTML]{96C2CD}{#1}}
% Types or modules
\newcommand{\ltyp}[1]{\textcolor[HTML]{2B91AF}{#1}}
% Functions
\newcommand{\lfun}[1]{\textcolor[HTML]{0000A0}{#1}}
% Patterns
\newcommand{\lpat}[1]{\textcolor[HTML]{800080}{#1}}
% Mutable vars
\newcommand{\lvar}[1]{\textbf{\textcolor[HTML]{000000}{#1}}}
% Printf
\newcommand{\lprf}[1]{\textcolor[HTML]{2B91AF}{#1}}
% Escaped characters
\newcommand{\lesc}[1]{\textcolor[HTML]{FF0080}{#1}}
% Inactive elements
\newcommand{\inact}[1]{\textcolor[HTML]{808080}{#1}}
% Preprocessors
\newcommand{\prep}[1]{\textcolor[HTML]{0000FF}{#1}}
% fsi output
\newcommand{\fsi}[1]{\textcolor[HTML]{808080}{#1}}
% Omitted parts
\newcommand{\omi}[1]{\textcolor[HTML]{808080}{#1}}
% Overriding color and style of line numbers
\renewcommand{\theFancyVerbLine}{
\lines{\small \arabic{FancyVerbLine}:}}
\lstset{%
backgroundcolor=\color{gray!15},
basicstyle=\ttfamily,
breaklines=true,
columns=fullflexible
}
\title{Literate sample
}
\date{}
\begin{document}
\maketitle
\section*{Literate sample}
This file demonstrates how to write literate F\# script
files (\texttt{*.fsx}) that can be transformed into nice HTML
using the \texttt{literate.fsx} script from the \href{http://fsprojects.github.io/FSharp.Formatting}{F\# Formatting
package}.
As you can see, a comment starting with double asterisk
is treated as part of the document and is transformed
using Markdown, which means that you can use:
\begin{itemize}
\item Unordered or ordered lists
\item Text formatting including \textbf{bold} and \emph{emphasis}
\end{itemize}
And numerous other \href{http://daringfireball.net/projects/markdown}{Markdown} features.
\subsection*{Writing F\# code}
Code that is not inside comment will be formatted as
a sample snippet (which also means that you can
run it in Visual Studio or MonoDevelop).
\begin{Verbatim}[commandchars=\\\{\}, numbers=left]
\com{/// The Hello World of functional languages!}
\kwd{let} \kwd{rec} \lfun{factorial} \id{x} \ops{=}
\kwd{if} \id{x} \ops{=} \num{0} \kwd{then} \num{1}
\kwd{else} \id{x} \ops{*} (\lfun{factorial} (\id{x} \ops{-} \num{1}))
\kwd{let} \id{f10} \ops{=} \lfun{factorial} \num{10}
\end{Verbatim}
\subsection*{Hiding code}
If you want to include some code in the source code,
but omit it from the output, you can use the \texttt{hide}
command.
The value will be deffined in the F\# code and so you
can use it from other (visible) code and get correct
tool tips:
\begin{Verbatim}[commandchars=\\\{\}, numbers=left]
\kwd{let} \id{answer} \ops{=} \id{hidden}
\end{Verbatim}
\subsection*{Moving code around}
Sometimes, it is useful to first explain some code that
has to be located at the end of the snippet (perhaps
because it uses some definitions discussed in the middle).
This can be done using \texttt{include} and \texttt{define} commands.
The following snippet gets correct tool tips, even though
it uses \texttt{laterFunction}:
\begin{Verbatim}[commandchars=\\\{\}, numbers=left]
\kwd{let} \id{sample} \ops{=}
\lfun{laterFunction}()
\ops{|>} \lfun{printfn} \str{"Got: }\lprf{\%s}\str{"}
\end{Verbatim}
Then we can explain how \texttt{laterFunction} is defined:
\begin{Verbatim}[commandchars=\\\{\}, numbers=left]
\kwd{let} \lfun{laterFunction}() \ops{=}
\str{"Not very difficult, is it?"}
\end{Verbatim}
This example covers pretty much all features that are
currently implemented in \texttt{literate.fsx}, but feel free
to \href{https://github.com/fsprojects/FSharp.Formatting}{fork the project on GitHub} and add more
features or report bugs!
\subsection*{Other features}
The tool-tips also work for double-backtick identifiers.
This might be useful to generate nice documents from tests:
\begin{Verbatim}[commandchars=\\\{\}, numbers=left]
\kwd{let} \lfun{``1 + 1 should be equal to 2``}() \ops{=}
\num{1} \ops{+} \num{1} \ops{=} \num{2}
\end{Verbatim}
Others examples follow here.
\end{document}