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