src/Pure/Thy/latex.ML
author wenzelm
Tue Aug 08 16:57:44 2000 +0200 (2000-08-08)
changeset 9558 8d5221bf765b
parent 9505 09c75c801dde
child 9663 e4d58f1be05b
permissions -rw-r--r--
token translation: enclose "\\mbox{" "}";
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@7726
    31
  "$" => "\\$" |
wenzelm@7726
    32
  "&" => "\\&" |
wenzelm@7726
    33
  "%" => "\\%" |
wenzelm@7726
    34
  "#" => "\\#" |
wenzelm@7726
    35
  "_" => "\\_" |
wenzelm@7800
    36
  "{" => "{\\isabraceleft}" |
wenzelm@7800
    37
  "}" => "{\\isabraceright}" |
wenzelm@7800
    38
  "~" => "{\\isatilde}" |
wenzelm@7800
    39
  "^" => "{\\isacircum}" |
wenzelm@7752
    40
  "\"" => "{\"}" |
wenzelm@7800
    41
  "\\" => "{\\isabackslash}" |
wenzelm@7726
    42
  c => c;
wenzelm@7726
    43
wenzelm@7900
    44
fun output_sym s =
wenzelm@7900
    45
  if size s = 1 then output_chr s
wenzelm@7900
    46
  else
wenzelm@7900
    47
    (case explode s of
wenzelm@7900
    48
      cs as "\\" :: "<" :: "^" :: _ => implode (map output_chr cs)
wenzelm@7973
    49
    | "\\" :: "<" :: cs => "{\\isasym" ^ implode (#1 (Library.split_last cs)) ^ "}"
wenzelm@7900
    50
    | _ => sys_error "output_sym");
wenzelm@7900
    51
wenzelm@7900
    52
in
wenzelm@7900
    53
wenzelm@8192
    54
val output_symbols = implode o map output_sym;
wenzelm@8192
    55
val output_syms = output_symbols o Symbol.explode;
wenzelm@7900
    56
wenzelm@7900
    57
end;
wenzelm@7726
    58
wenzelm@7726
    59
wenzelm@7726
    60
(* token output *)
wenzelm@7726
    61
wenzelm@7726
    62
structure T = OuterLex;
wenzelm@7726
    63
wenzelm@8660
    64
datatype token =
wenzelm@8660
    65
  Basic of T.token |
wenzelm@9135
    66
  Markup of string |
wenzelm@9135
    67
  MarkupEnv of string |
wenzelm@9135
    68
  Verbatim;
wenzelm@7756
    69
wenzelm@7756
    70
wenzelm@7756
    71
val invisible_token = T.keyword_with (equal ";") orf T.is_kind T.Comment;
wenzelm@7756
    72
wenzelm@7756
    73
fun strip_blanks s =
wenzelm@7756
    74
  implode (#1 (Library.take_suffix Symbol.is_blank
wenzelm@7756
    75
    (#2 (Library.take_prefix Symbol.is_blank (explode s)))));
wenzelm@7756
    76
wenzelm@9135
    77
fun output_tok (Basic tok, _) =
wenzelm@7752
    78
      let val s = T.val_of tok in
wenzelm@7756
    79
        if invisible_token tok then ""
wenzelm@7800
    80
        else if T.is_kind T.Command tok then
wenzelm@8896
    81
          if s = "{" then "{\\isabeginblock}"
wenzelm@8896
    82
          else if s = "}" then "{\\isaendblock}"
wenzelm@7900
    83
          else "\\isacommand{" ^ output_syms s ^ "}"
wenzelm@7800
    84
        else if T.is_kind T.Keyword tok andalso Syntax.is_identifier s then
wenzelm@7900
    85
          "\\isakeyword{" ^ output_syms s ^ "}"
wenzelm@7900
    86
        else if T.is_kind T.String tok then output_syms (quote s)
wenzelm@7900
    87
        else if T.is_kind T.Verbatim tok then output_syms (enclose "{*" "*}" s)
wenzelm@7900
    88
        else output_syms s
wenzelm@7756
    89
      end
wenzelm@9135
    90
  | output_tok (Markup cmd, txt) = "%\n\\isamarkup" ^ cmd ^ "{" ^ strip_blanks txt ^ "}\n"
wenzelm@9135
    91
  | output_tok (MarkupEnv cmd, txt) = "%\n\\begin{isamarkup" ^ cmd ^ "}%\n" ^
wenzelm@8660
    92
      strip_blanks txt ^ "%\n\\end{isamarkup" ^ cmd ^ "}%\n"
wenzelm@9135
    93
  | output_tok (Verbatim, txt) = "%\n" ^ strip_blanks txt ^ "\n";
wenzelm@7745
    94
wenzelm@7756
    95
wenzelm@7756
    96
val output_tokens = implode o map output_tok;
wenzelm@7726
    97
wenzelm@7726
    98
wenzelm@7726
    99
(* theory presentation *)
wenzelm@7726
   100
wenzelm@9135
   101
val isabelle_file = enclose
wenzelm@9135
   102
  "\\begin{isabelle}%\n"
wenzelm@9135
   103
  "\\end{isabelle}%\n\
wenzelm@9135
   104
  \%%% Local Variables:\n\
wenzelm@9135
   105
  \%%% mode: latex\n\
wenzelm@9135
   106
  \%%% TeX-master: \"root\"\n\
wenzelm@9144
   107
  \%%% End:\n";
wenzelm@8192
   108
wenzelm@8499
   109
fun old_symbol_source name syms =
wenzelm@9135
   110
  isabelle_file ("\\isamarkupheader{" ^ output_syms name ^ "}%\n" ^ output_symbols syms);
wenzelm@7726
   111
wenzelm@9038
   112
fun theory_entry name = "\\input{" ^ name ^ ".tex}\n\n";
wenzelm@7726
   113
wenzelm@7726
   114
wenzelm@8460
   115
(* print mode *)
wenzelm@8460
   116
wenzelm@8965
   117
val latexN = "latex";
wenzelm@9135
   118
val modes = [latexN, Symbol.xsymbolsN, Symbol.symbolsN];
wenzelm@8965
   119
wenzelm@8460
   120
fun latex_output str =
wenzelm@8460
   121
  let val syms = Symbol.explode str
wenzelm@8460
   122
  in (output_symbols syms, real (Symbol.length syms)) end;
wenzelm@8460
   123
wenzelm@9558
   124
val token_translation =
wenzelm@9558
   125
  map (fn s => (latexN, s, apfst (enclose "\\mbox{" "}") o latex_output))
wenzelm@9558
   126
    Syntax.standard_token_classes;
wenzelm@8965
   127
wenzelm@8965
   128
val _ = Symbol.add_mode latexN latex_output;
wenzelm@8965
   129
val setup = [Theory.add_tokentrfuns token_translation];
wenzelm@8460
   130
wenzelm@8460
   131
wenzelm@7726
   132
end;