src/Pure/Thy/latex.ML
author wenzelm
Fri Feb 04 21:44:38 2000 +0100 (2000-02-04)
changeset 8192 45a7027136e3
parent 8117 0a6173c9b2d0
child 8460 274426d1adbc
permissions -rw-r--r--
added old_symbol_source;
tuned;
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@7726
     5
Simple 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@8192
    11
  val old_symbol_source: 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@8192
    91
val isabelle_simple = enclose "\\begin{isabellesimple}%\n" "\\end{isabellesimple}%\n";
wenzelm@8192
    92
wenzelm@8192
    93
val old_symbol_source = isabelle_simple o output_symbols;
wenzelm@8192
    94
val token_source = isabelle_simple o output_tokens;
wenzelm@7726
    95
wenzelm@7726
    96
fun theory_entry name = "\\input{" ^ name ^ ".tex}\n";
wenzelm@7726
    97
wenzelm@7726
    98
wenzelm@7726
    99
end;