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