src/Pure/Thy/latex.ML
author wenzelm
Fri Mar 17 16:30:45 2000 +0100 (2000-03-17)
changeset 8499 8958ece3bbdf
parent 8460 274426d1adbc
child 8660 e5048a26e1d8
permissions -rw-r--r--
old_symbol_source: include header;
wenzelm@7726
     1
(*  Title:      Pure/Thy/latex.ML
wenzelm@7726
     2
    ID:         $Id$
wenzelm@7726
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@7726
     4
wenzelm@8460
     5
LaTeX presentation primitives (based on outer lexical syntax).
wenzelm@7726
     6
*)
wenzelm@7726
     7
wenzelm@7726
     8
signature LATEX =
wenzelm@7726
     9
sig
wenzelm@7789
    10
  datatype token = Basic of OuterLex.token | Markup of string * string | Verbatim of string
wenzelm@8499
    11
  val old_symbol_source: string -> Symbol.symbol list -> string
wenzelm@7756
    12
  val token_source: token list -> string
wenzelm@7726
    13
  val theory_entry: string -> string
wenzelm@7726
    14
end;
wenzelm@7726
    15
wenzelm@7726
    16
structure Latex: LATEX =
wenzelm@7726
    17
struct
wenzelm@7726
    18
wenzelm@7726
    19
wenzelm@7726
    20
(* symbol output *)
wenzelm@7726
    21
wenzelm@7900
    22
local
wenzelm@7900
    23
wenzelm@7726
    24
val output_chr = fn
wenzelm@7726
    25
  " " => "~" |
wenzelm@7726
    26
  "\n" => "\\isanewline\n" |
wenzelm@7726
    27
  "$" => "\\$" |
wenzelm@7726
    28
  "&" => "\\&" |
wenzelm@7726
    29
  "%" => "\\%" |
wenzelm@7726
    30
  "#" => "\\#" |
wenzelm@7726
    31
  "_" => "\\_" |
wenzelm@7800
    32
  "{" => "{\\isabraceleft}" |
wenzelm@7800
    33
  "}" => "{\\isabraceright}" |
wenzelm@7800
    34
  "~" => "{\\isatilde}" |
wenzelm@7800
    35
  "^" => "{\\isacircum}" |
wenzelm@7752
    36
  "\"" => "{\"}" |
wenzelm@7800
    37
  "\\" => "{\\isabackslash}" |
wenzelm@7726
    38
  c => c;
wenzelm@7726
    39
wenzelm@7900
    40
fun output_sym s =
wenzelm@7900
    41
  if size s = 1 then output_chr s
wenzelm@7900
    42
  else
wenzelm@7900
    43
    (case explode s of
wenzelm@7900
    44
      cs as "\\" :: "<" :: "^" :: _ => implode (map output_chr cs)
wenzelm@7973
    45
    | "\\" :: "<" :: cs => "{\\isasym" ^ implode (#1 (Library.split_last cs)) ^ "}"
wenzelm@7900
    46
    | _ => sys_error "output_sym");
wenzelm@7900
    47
wenzelm@7900
    48
in
wenzelm@7900
    49
wenzelm@8192
    50
val output_symbols = implode o map output_sym;
wenzelm@8192
    51
val output_syms = output_symbols o Symbol.explode;
wenzelm@7900
    52
wenzelm@7900
    53
end;
wenzelm@7726
    54
wenzelm@7726
    55
wenzelm@7726
    56
(* token output *)
wenzelm@7726
    57
wenzelm@7726
    58
structure T = OuterLex;
wenzelm@7726
    59
wenzelm@7789
    60
datatype token = Basic of T.token | Markup of string * string | Verbatim of string;
wenzelm@7756
    61
wenzelm@7756
    62
wenzelm@7756
    63
val invisible_token = T.keyword_with (equal ";") orf T.is_kind T.Comment;
wenzelm@7756
    64
wenzelm@7756
    65
fun strip_blanks s =
wenzelm@7756
    66
  implode (#1 (Library.take_suffix Symbol.is_blank
wenzelm@7756
    67
    (#2 (Library.take_prefix Symbol.is_blank (explode s)))));
wenzelm@7756
    68
wenzelm@7756
    69
fun output_tok (Basic tok) =
wenzelm@7752
    70
      let val s = T.val_of tok in
wenzelm@7756
    71
        if invisible_token tok then ""
wenzelm@7800
    72
        else if T.is_kind T.Command tok then
wenzelm@7800
    73
          if s = "{{" then "{\\isabeginblock}"
wenzelm@7800
    74
          else if s = "}}" then "{\\isaendblock}"
wenzelm@7900
    75
          else "\\isacommand{" ^ output_syms s ^ "}"
wenzelm@7800
    76
        else if T.is_kind T.Keyword tok andalso Syntax.is_identifier s then
wenzelm@7900
    77
          "\\isakeyword{" ^ output_syms s ^ "}"
wenzelm@7900
    78
        else if T.is_kind T.String tok then output_syms (quote s)
wenzelm@7900
    79
        else if T.is_kind T.Verbatim tok then output_syms (enclose "{*" "*}" s)
wenzelm@7900
    80
        else output_syms s
wenzelm@7756
    81
      end
wenzelm@7852
    82
  | output_tok (Markup (cmd, txt)) = "%\n\\isamarkup" ^ cmd ^ "{" ^ strip_blanks txt ^ "}\n"
wenzelm@7852
    83
  | output_tok (Verbatim txt) = "%\n" ^ strip_blanks txt ^ "\n";
wenzelm@7745
    84
wenzelm@7756
    85
wenzelm@7756
    86
val output_tokens = implode o map output_tok;
wenzelm@7726
    87
wenzelm@7726
    88
wenzelm@7726
    89
(* theory presentation *)
wenzelm@7726
    90
wenzelm@8460
    91
val isabelle_env = enclose "\\begin{isabelle}%\n" "\\end{isabelle}%\n";
wenzelm@8192
    92
wenzelm@8499
    93
fun old_symbol_source name syms =
wenzelm@8499
    94
  isabelle_env ("\\isamarkupheader{" ^ output_syms name ^ "}%\n" ^ output_symbols syms);
wenzelm@8499
    95
wenzelm@8460
    96
val token_source = isabelle_env o output_tokens;
wenzelm@7726
    97
wenzelm@7726
    98
fun theory_entry name = "\\input{" ^ name ^ ".tex}\n";
wenzelm@7726
    99
wenzelm@7726
   100
wenzelm@8460
   101
(* print mode *)
wenzelm@8460
   102
wenzelm@8460
   103
fun latex_output str =
wenzelm@8460
   104
  let val syms = Symbol.explode str
wenzelm@8460
   105
  in (output_symbols syms, real (Symbol.length syms)) end;
wenzelm@8460
   106
wenzelm@8460
   107
val _ = Symbol.add_mode "latex" latex_output;
wenzelm@8460
   108
wenzelm@8460
   109
wenzelm@7726
   110
end;