src/Pure/Thy/latex.ML
author wenzelm
Thu Oct 21 18:43:06 1999 +0200 (1999-10-21)
changeset 7900 e62973fccc97
parent 7852 d28dff7ac48d
child 7973 0d801c6e4dc0
permissions -rw-r--r--
output \isasymbols;
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@7756
    11
  val token_source: token list -> string
wenzelm@7726
    12
  val theory_entry: string -> string
wenzelm@7726
    13
end;
wenzelm@7726
    14
wenzelm@7726
    15
structure Latex: LATEX =
wenzelm@7726
    16
struct
wenzelm@7726
    17
wenzelm@7726
    18
wenzelm@7726
    19
(* symbol output *)
wenzelm@7726
    20
wenzelm@7900
    21
local
wenzelm@7900
    22
wenzelm@7726
    23
val output_chr = fn
wenzelm@7726
    24
  " " => "~" |
wenzelm@7726
    25
  "\n" => "\\isanewline\n" |
wenzelm@7726
    26
  "$" => "\\$" |
wenzelm@7726
    27
  "&" => "\\&" |
wenzelm@7726
    28
  "%" => "\\%" |
wenzelm@7726
    29
  "#" => "\\#" |
wenzelm@7726
    30
  "_" => "\\_" |
wenzelm@7800
    31
  "{" => "{\\isabraceleft}" |
wenzelm@7800
    32
  "}" => "{\\isabraceright}" |
wenzelm@7800
    33
  "~" => "{\\isatilde}" |
wenzelm@7800
    34
  "^" => "{\\isacircum}" |
wenzelm@7752
    35
  "\"" => "{\"}" |
wenzelm@7800
    36
  "\\" => "{\\isabackslash}" |
wenzelm@7726
    37
  c => c;
wenzelm@7726
    38
wenzelm@7900
    39
fun output_sym s =
wenzelm@7900
    40
  if size s = 1 then output_chr s
wenzelm@7900
    41
  else
wenzelm@7900
    42
    (case explode s of
wenzelm@7900
    43
      cs as "\\" :: "<" :: "^" :: _ => implode (map output_chr cs)
wenzelm@7900
    44
    | "\\" :: "<" :: cs => "{\\isasymbol" ^ implode (#1 (Library.split_last cs)) ^ "}"
wenzelm@7900
    45
    | _ => sys_error "output_sym");
wenzelm@7900
    46
wenzelm@7900
    47
in
wenzelm@7900
    48
wenzelm@7900
    49
val output_syms = implode o map output_sym o Symbol.explode;
wenzelm@7900
    50
wenzelm@7900
    51
end;
wenzelm@7726
    52
wenzelm@7726
    53
wenzelm@7726
    54
(* token output *)
wenzelm@7726
    55
wenzelm@7726
    56
structure T = OuterLex;
wenzelm@7726
    57
wenzelm@7789
    58
datatype token = Basic of T.token | Markup of string * string | Verbatim of string;
wenzelm@7756
    59
wenzelm@7756
    60
wenzelm@7756
    61
val invisible_token = T.keyword_with (equal ";") orf T.is_kind T.Comment;
wenzelm@7756
    62
wenzelm@7756
    63
fun strip_blanks s =
wenzelm@7756
    64
  implode (#1 (Library.take_suffix Symbol.is_blank
wenzelm@7756
    65
    (#2 (Library.take_prefix Symbol.is_blank (explode s)))));
wenzelm@7756
    66
wenzelm@7756
    67
fun output_tok (Basic tok) =
wenzelm@7752
    68
      let val s = T.val_of tok in
wenzelm@7756
    69
        if invisible_token tok then ""
wenzelm@7800
    70
        else if T.is_kind T.Command tok then
wenzelm@7800
    71
          if s = "{{" then "{\\isabeginblock}"
wenzelm@7800
    72
          else if s = "}}" then "{\\isaendblock}"
wenzelm@7900
    73
          else "\\isacommand{" ^ output_syms s ^ "}"
wenzelm@7800
    74
        else if T.is_kind T.Keyword tok andalso Syntax.is_identifier s then
wenzelm@7900
    75
          "\\isakeyword{" ^ output_syms s ^ "}"
wenzelm@7900
    76
        else if T.is_kind T.String tok then output_syms (quote s)
wenzelm@7900
    77
        else if T.is_kind T.Verbatim tok then output_syms (enclose "{*" "*}" s)
wenzelm@7900
    78
        else output_syms s
wenzelm@7756
    79
      end
wenzelm@7852
    80
  | output_tok (Markup (cmd, txt)) = "%\n\\isamarkup" ^ cmd ^ "{" ^ strip_blanks txt ^ "}\n"
wenzelm@7852
    81
  | output_tok (Verbatim txt) = "%\n" ^ strip_blanks txt ^ "\n";
wenzelm@7745
    82
wenzelm@7756
    83
wenzelm@7756
    84
val output_tokens = implode o map output_tok;
wenzelm@7726
    85
wenzelm@7726
    86
wenzelm@7726
    87
(* theory presentation *)
wenzelm@7726
    88
wenzelm@7726
    89
fun token_source toks =
wenzelm@7756
    90
  "\\begin{isabellesimple}\n" ^ output_tokens toks ^ "\\end{isabellesimple}\n";
wenzelm@7726
    91
wenzelm@7726
    92
fun theory_entry name = "\\input{" ^ name ^ ".tex}\n";
wenzelm@7726
    93
wenzelm@7726
    94
wenzelm@7726
    95
end;