let active_mode mode = 
  current_active_mode := mode;
  match mode with
    | Formatter r ->
        set_all_formatter_output_functions 
          ~out:r.out
          ~flush:r.flush 
          ~newline:r.newline 
          ~spaces:r.spaces         
          
    | Latex out -> 
        let _ = Format.set_margin 80 in
        let outputf s p n =
          let s = (String.sub s p n) in
          let s' = 
            List.fold_left 
              (fun acu (s, s') -> 
                 Str.global_replace (Str.regexp s) s' acu)
              s [ 
                ("{""\\{");
                ("}""\\}");
                ("|""$|$");
                ("\\""$\\partial$");
                ("let rec ""\\textbf{let rec} ");
                ("let ""\\textbf{let} ");
                ("match ""\\textbf{match} ");
                ("end ""\\textbf{end} ");
                ("with ""\\textbf{with} ");
                ("type ""\\textbf{type} ");
                ("in ""\\textbf{in} ");
                ("=>""$\\Rightarrow$");
                ("*""$\\times$");
                ("forall""$\\forall$"); 
                ("exists""$\\exists$"); 
                ("->"" $\\rightarrow$ ");
                ("<""$<$"); 
                (">""$>$"); 
              ] in
          let s' = 
            List.fold_left 
              (fun acu (s, s') -> 
                 Str.global_replace (Str.regexp s) s' acu)
              s' [ ("_""\\_") ]
          in
            output_string out s'
        and flush () = flush out
        and newline () = output_string out "\\par\n"
        and spaces n = if (n != 0) then
          output_string out 
            ("\\hspace*{"^string_of_int n^"\\smallskipamount}"in
          set_all_formatter_output_functions 
            ~out:outputf 
            ~flush:flush 
            ~newline:newline 
            ~spaces:spaces 

    | Txt out ->           
        let _ = Format.set_margin 80 in
          set_all_formatter_output_functions 
            ~out:(fun s b e -> output_string out (String.sub s b e))
            ~flush:(fun () -> flush out) 
            ~newline:(fun () -> output_string out "\n"
            ~spaces:(fun n -> for i = 0 to n do output_string out " " done)