Text FormattingF# source code

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
krislodz
Posts: 42
Joined: Sun Nov 08, 2009 1:13 pm

F# source code

Post by krislodz »

Hello,

I have to include code snippets from F#. I am using listing but apparently it does not support F#. Anyone did that before, has some experience ? I cannot find anything.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX books
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

F# source code

Post by gmedina »

Hi,

the listings package offers you a machanism to define a new language; see section 3.2 Language definitions (p. 20ff) and section 4.18 Language definitions (p. 41ff) of the package documentation.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
krislodz
Posts: 42
Joined: Sun Nov 08, 2009 1:13 pm

F# source code

Post by krislodz »

I started defining F# for listing package. Just basic keywords and coloring. Font and signs looks not like f#. Anyone has some suggestions ?

Code: Select all

\documentclass[a4paper,11pt]{article}

\usepackage{listings}
\usepackage{color}

\definecolor{bluekeywords}{rgb}{0.13,0.13,1}
\definecolor{greencomments}{rgb}{0,0.5,0}
\definecolor{turqusnumbers}{rgb}{0.17,0.57,0.69}
\definecolor{redstrings}{rgb}{0.5,0,0}

\lstdefinelanguage{FSharp}
		{morekeywords={let, new, match, with, rec, open, module, namespace, type, of, member, and, for, in, do, begin, end, fun, function, try, mutable, if, then, else},
    keywordstyle=\color{bluekeywords},
    sensitive=false,
    morecomment=[l][\color{greencomments}]{///},
    morecomment=[l][\color{greencomments}]{//},
    morecomment=[s][\color{greencomments}]{{(*}{*)}},
    morestring=[b]",
    stringstyle=\color{redstrings}
    }

\begin{document}

\lstset{language=FSharp}
\begin{lstlisting}
(* Fibonacci Number formula *)
let rec fib n =
    match n with
    | 0 | 1 -> n
    | _ -> fib (n - 1) + fib (n - 2)
 
(* An alternative approach - a lazy recursive sequence of Fibonacci numbers *)
let rec fibs = seq {
    yield! [1; 1];
    for (x, y) in Seq.zip fibs (Seq.skip 1 fibs) -> x + y }
 
(* Print even fibs *)
[1 .. 10]
|> List.map     fib
|> List.filter  (fun n -> (n % 2) = 0)
|> printlist
 
(* Same thing, using Comprehension syntax *)
[ for i in 1..10 do
    let r = fib i
    if r % 2 = 0 then yield r ]
|> printlist 
 
\end{lstlisting}

\end{document}
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

F# source code

Post by localghost »

krislodz wrote:[…] Font and signs looks not like f#. […]
Please describe how F# code should look like in your opinion. For the present you can start with some supplements for the settings.

Code: Select all

\lstset{
  language=FSharp,
  basicstyle=\ttfamily,
  breaklines=true,
  columns=fullflexible
}
For further information see Section 4.6 (Figure out the appearance, p. 28ff) of the listings manual. But I think that the entire Section 4 should help to get things work.
krislodz
Posts: 42
Joined: Sun Nov 08, 2009 1:13 pm

Re: F# source code

Post by krislodz »

localghost, code looks much neater with these options. I wanted to obtain Visual Studio look but with the current state i am very much satisfied. Thanks.
Post Reply