src/Pure/Thy/latex.ML
author wenzelm
Mon Aug 21 18:38:27 2000 +0200 (2000-08-21)
changeset 9668 b5e709fd1e42
parent 9663 e4d58f1be05b
child 9707 067c25edd1bd
permissions -rw-r--r--
more \isachars;
added \isadigit;
simplified command markup;
wenzelm@7726
     1
(*  Title:      Pure/Thy/latex.ML
wenzelm@7726
     2
    ID:         $Id$
wenzelm@7726
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@8808
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
wenzelm@7726
     5
wenzelm@8460
     6
LaTeX presentation primitives (based on outer lexical syntax).
wenzelm@7726
     7
*)
wenzelm@7726
     8
wenzelm@7726
     9
signature LATEX =
wenzelm@7726
    10
sig
wenzelm@9135
    11
  datatype token = Basic of OuterLex.token | Markup of string | MarkupEnv of string | Verbatim
wenzelm@9135
    12
  val output_tokens: (token * string) list -> string
wenzelm@9135
    13
  val isabelle_file: string -> string
wenzelm@8499
    14
  val old_symbol_source: string -> Symbol.symbol list -> string
wenzelm@7726
    15
  val theory_entry: string -> string
wenzelm@9135
    16
  val modes: string list
wenzelm@8965
    17
  val setup: (theory -> theory) list
wenzelm@7726
    18
end;
wenzelm@7726
    19
wenzelm@7726
    20
structure Latex: LATEX =
wenzelm@7726
    21
struct
wenzelm@7726
    22
wenzelm@7726
    23
wenzelm@7726
    24
(* symbol output *)
wenzelm@7726
    25
wenzelm@7900
    26
local
wenzelm@7900
    27
wenzelm@7726
    28
val output_chr = fn
wenzelm@9505
    29
  " " => "\\ " |
wenzelm@7726
    30
  "\n" => "\\isanewline\n" |
wenzelm@9668
    31
  "!" => "{\\isacharbang}" |
wenzelm@9668
    32
  "\"" => "{\\isachardoublequote}" |
wenzelm@9663
    33
  "#" => "{\\isacharhash}" |
wenzelm@9663
    34
  "$" => "{\\isachardollar}" |
wenzelm@9663
    35
  "%" => "{\\isacharpercent}" |
wenzelm@9663
    36
  "&" => "{\\isacharampersand}" |
wenzelm@9663
    37
  "'" => "{\\isacharprime}" |
wenzelm@9663
    38
  "(" => "{\\isacharparenleft}" |
wenzelm@9663
    39
  ")" => "{\\isacharparenright}" |
wenzelm@9663
    40
  "*" => "{\\isacharasterisk}" |
wenzelm@9668
    41
  "+" => "{\\isacharplus}" |
wenzelm@9668
    42
  "," => "{\\isacharcomma}" |
wenzelm@9663
    43
  "-" => "{\\isacharminus}" |
wenzelm@9668
    44
  "." => "{\\isachardot}" |
wenzelm@9668
    45
  "/" => "{\\isacharslash}" |
wenzelm@9668
    46
  ":" => "{\\isacharcolon}" |
wenzelm@9668
    47
  ";" => "{\\isacharsemicolon}" |
wenzelm@9663
    48
  "<" => "{\\isacharless}" |
wenzelm@9668
    49
  "=" => "{\\isacharequal}" |
wenzelm@9663
    50
  ">" => "{\\isachargreater}" |
wenzelm@9668
    51
  "?" => "{\\isacharquery}" |
wenzelm@9668
    52
  "@" => "{\\isacharat}" |
wenzelm@9663
    53
  "[" => "{\\isacharbrackleft}" |
wenzelm@9663
    54
  "\\" => "{\\isacharbackslash}" |
wenzelm@9663
    55
  "]" => "{\\isacharbrackright}" |
wenzelm@9663
    56
  "^" => "{\\isacharcircum}" |
wenzelm@9663
    57
  "_" => "{\\isacharunderscore}" |
wenzelm@9668
    58
  "`" => "{\\isacharbackquote}" |
wenzelm@9663
    59
  "{" => "{\\isacharbraceleft}" |
wenzelm@9663
    60
  "|" => "{\\isacharbar}" |
wenzelm@9663
    61
  "}" => "{\\isacharbraceright}" |
wenzelm@9663
    62
  "~" => "{\\isachartilde}" |
wenzelm@9668
    63
  c => if Symbol.is_digit c then enclose "\\isadigit{" "}" c else c;
wenzelm@7726
    64
wenzelm@7900
    65
fun output_sym s =
wenzelm@7900
    66
  if size s = 1 then output_chr s
wenzelm@7900
    67
  else
wenzelm@7900
    68
    (case explode s of
wenzelm@7900
    69
      cs as "\\" :: "<" :: "^" :: _ => implode (map output_chr cs)
wenzelm@7973
    70
    | "\\" :: "<" :: cs => "{\\isasym" ^ implode (#1 (Library.split_last cs)) ^ "}"
wenzelm@7900
    71
    | _ => sys_error "output_sym");
wenzelm@7900
    72
wenzelm@7900
    73
in
wenzelm@7900
    74
wenzelm@8192
    75
val output_symbols = implode o map output_sym;
wenzelm@8192
    76
val output_syms = output_symbols o Symbol.explode;
wenzelm@7900
    77
wenzelm@7900
    78
end;
wenzelm@7726
    79
wenzelm@7726
    80
wenzelm@7726
    81
(* token output *)
wenzelm@7726
    82
wenzelm@7726
    83
structure T = OuterLex;
wenzelm@7726
    84
wenzelm@8660
    85
datatype token =
wenzelm@8660
    86
  Basic of T.token |
wenzelm@9135
    87
  Markup of string |
wenzelm@9135
    88
  MarkupEnv of string |
wenzelm@9135
    89
  Verbatim;
wenzelm@7756
    90
wenzelm@7756
    91
wenzelm@7756
    92
val invisible_token = T.keyword_with (equal ";") orf T.is_kind T.Comment;
wenzelm@7756
    93
wenzelm@7756
    94
fun strip_blanks s =
wenzelm@7756
    95
  implode (#1 (Library.take_suffix Symbol.is_blank
wenzelm@7756
    96
    (#2 (Library.take_prefix Symbol.is_blank (explode s)))));
wenzelm@7756
    97
wenzelm@9135
    98
fun output_tok (Basic tok, _) =
wenzelm@7752
    99
      let val s = T.val_of tok in
wenzelm@7756
   100
        if invisible_token tok then ""
wenzelm@7800
   101
        else if T.is_kind T.Command tok then
wenzelm@9668
   102
          "\\isacommand{" ^ output_syms s ^ "}"
wenzelm@7800
   103
        else if T.is_kind T.Keyword tok andalso Syntax.is_identifier s then
wenzelm@7900
   104
          "\\isakeyword{" ^ output_syms s ^ "}"
wenzelm@7900
   105
        else if T.is_kind T.String tok then output_syms (quote s)
wenzelm@7900
   106
        else if T.is_kind T.Verbatim tok then output_syms (enclose "{*" "*}" s)
wenzelm@7900
   107
        else output_syms s
wenzelm@7756
   108
      end
wenzelm@9135
   109
  | output_tok (Markup cmd, txt) = "%\n\\isamarkup" ^ cmd ^ "{" ^ strip_blanks txt ^ "}\n"
wenzelm@9135
   110
  | output_tok (MarkupEnv cmd, txt) = "%\n\\begin{isamarkup" ^ cmd ^ "}%\n" ^
wenzelm@8660
   111
      strip_blanks txt ^ "%\n\\end{isamarkup" ^ cmd ^ "}%\n"
wenzelm@9135
   112
  | output_tok (Verbatim, txt) = "%\n" ^ strip_blanks txt ^ "\n";
wenzelm@7745
   113
wenzelm@7756
   114
wenzelm@7756
   115
val output_tokens = implode o map output_tok;
wenzelm@7726
   116
wenzelm@7726
   117
wenzelm@7726
   118
(* theory presentation *)
wenzelm@7726
   119
wenzelm@9135
   120
val isabelle_file = enclose
wenzelm@9135
   121
  "\\begin{isabelle}%\n"
wenzelm@9135
   122
  "\\end{isabelle}%\n\
wenzelm@9135
   123
  \%%% Local Variables:\n\
wenzelm@9135
   124
  \%%% mode: latex\n\
wenzelm@9135
   125
  \%%% TeX-master: \"root\"\n\
wenzelm@9144
   126
  \%%% End:\n";
wenzelm@8192
   127
wenzelm@8499
   128
fun old_symbol_source name syms =
wenzelm@9135
   129
  isabelle_file ("\\isamarkupheader{" ^ output_syms name ^ "}%\n" ^ output_symbols syms);
wenzelm@7726
   130
wenzelm@9038
   131
fun theory_entry name = "\\input{" ^ name ^ ".tex}\n\n";
wenzelm@7726
   132
wenzelm@7726
   133
wenzelm@8460
   134
(* print mode *)
wenzelm@8460
   135
wenzelm@8965
   136
val latexN = "latex";
wenzelm@9135
   137
val modes = [latexN, Symbol.xsymbolsN, Symbol.symbolsN];
wenzelm@8965
   138
wenzelm@8460
   139
fun latex_output str =
wenzelm@8460
   140
  let val syms = Symbol.explode str
wenzelm@8460
   141
  in (output_symbols syms, real (Symbol.length syms)) end;
wenzelm@8460
   142
wenzelm@9558
   143
val token_translation =
wenzelm@9558
   144
  map (fn s => (latexN, s, apfst (enclose "\\mbox{" "}") o latex_output))
wenzelm@9558
   145
    Syntax.standard_token_classes;
wenzelm@8965
   146
wenzelm@8965
   147
val _ = Symbol.add_mode latexN latex_output;
wenzelm@8965
   148
val setup = [Theory.add_tokentrfuns token_translation];
wenzelm@8460
   149
wenzelm@8460
   150
wenzelm@7726
   151
end;