src/Pure/Thy/latex.ML
author schirmer
Wed Apr 14 12:19:16 2004 +0200 (2004-04-14)
changeset 14561 c53396af770e
parent 14364 fc62df0bf353
child 14598 7009f59711e3
permissions -rw-r--r--
* raw control symbols are of the form \<^raw:...> now.
* again allowing symbols to begin with "\\" instead of "\" for
compatibility with ML-strings of old style theory and ML-files and
isa-ProofGeneral.
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@11860
    14
  val flag_markup: bool -> string
wenzelm@9707
    15
  val tex_trailer: string
wenzelm@9916
    16
  val isabelle_file: string -> string -> string
wenzelm@8499
    17
  val old_symbol_source: string -> Symbol.symbol list -> string
wenzelm@7726
    18
  val theory_entry: string -> string
wenzelm@9135
    19
  val modes: string list
wenzelm@8965
    20
  val setup: (theory -> theory) list
wenzelm@7726
    21
end;
wenzelm@7726
    22
wenzelm@7726
    23
structure Latex: LATEX =
wenzelm@7726
    24
struct
wenzelm@7726
    25
wenzelm@7726
    26
wenzelm@7726
    27
(* symbol output *)
wenzelm@7726
    28
wenzelm@7900
    29
local
wenzelm@7900
    30
wenzelm@7726
    31
val output_chr = fn
wenzelm@9505
    32
  " " => "\\ " |
wenzelm@7726
    33
  "\n" => "\\isanewline\n" |
wenzelm@9668
    34
  "!" => "{\\isacharbang}" |
wenzelm@9668
    35
  "\"" => "{\\isachardoublequote}" |
wenzelm@9663
    36
  "#" => "{\\isacharhash}" |
wenzelm@9663
    37
  "$" => "{\\isachardollar}" |
wenzelm@9663
    38
  "%" => "{\\isacharpercent}" |
wenzelm@9663
    39
  "&" => "{\\isacharampersand}" |
wenzelm@9663
    40
  "'" => "{\\isacharprime}" |
wenzelm@9663
    41
  "(" => "{\\isacharparenleft}" |
wenzelm@9663
    42
  ")" => "{\\isacharparenright}" |
wenzelm@9663
    43
  "*" => "{\\isacharasterisk}" |
wenzelm@9668
    44
  "+" => "{\\isacharplus}" |
wenzelm@9668
    45
  "," => "{\\isacharcomma}" |
wenzelm@9663
    46
  "-" => "{\\isacharminus}" |
wenzelm@9668
    47
  "." => "{\\isachardot}" |
wenzelm@9668
    48
  "/" => "{\\isacharslash}" |
wenzelm@9668
    49
  ":" => "{\\isacharcolon}" |
wenzelm@9668
    50
  ";" => "{\\isacharsemicolon}" |
wenzelm@9663
    51
  "<" => "{\\isacharless}" |
wenzelm@9668
    52
  "=" => "{\\isacharequal}" |
wenzelm@9663
    53
  ">" => "{\\isachargreater}" |
wenzelm@9668
    54
  "?" => "{\\isacharquery}" |
wenzelm@9668
    55
  "@" => "{\\isacharat}" |
wenzelm@9663
    56
  "[" => "{\\isacharbrackleft}" |
wenzelm@9663
    57
  "\\" => "{\\isacharbackslash}" |
wenzelm@9663
    58
  "]" => "{\\isacharbrackright}" |
wenzelm@9663
    59
  "^" => "{\\isacharcircum}" |
wenzelm@9663
    60
  "_" => "{\\isacharunderscore}" |
wenzelm@9668
    61
  "`" => "{\\isacharbackquote}" |
wenzelm@9663
    62
  "{" => "{\\isacharbraceleft}" |
wenzelm@9663
    63
  "|" => "{\\isacharbar}" |
wenzelm@9663
    64
  "}" => "{\\isacharbraceright}" |
wenzelm@9663
    65
  "~" => "{\\isachartilde}" |
wenzelm@10184
    66
  c => if Symbol.is_digit c then enclose "{\\isadigit{" "}}" c else c;
wenzelm@7726
    67
wenzelm@7900
    68
fun output_sym s =
wenzelm@7900
    69
  if size s = 1 then output_chr s
wenzelm@7900
    70
  else
wenzelm@7900
    71
    (case explode s of
schirmer@14561
    72
      "\\" :: "<" :: "^" :: "r"::"a"::"w"::":"::cs => implode (#1 (Library.split_last cs)) 
schirmer@14361
    73
    | "\\" :: "<" :: "^" :: cs => "\\isactrl" ^ implode (#1 (Library.split_last cs)) ^ " "
wenzelm@7973
    74
    | "\\" :: "<" :: cs => "{\\isasym" ^ implode (#1 (Library.split_last cs)) ^ "}"
wenzelm@7900
    75
    | _ => sys_error "output_sym");
wenzelm@7900
    76
wenzelm@7900
    77
in
wenzelm@7900
    78
wenzelm@8192
    79
val output_symbols = implode o map output_sym;
wenzelm@8192
    80
val output_syms = output_symbols o Symbol.explode;
wenzelm@7900
    81
wenzelm@7900
    82
end;
wenzelm@7726
    83
wenzelm@7726
    84
wenzelm@7726
    85
(* token output *)
wenzelm@7726
    86
wenzelm@7726
    87
structure T = OuterLex;
wenzelm@7726
    88
wenzelm@8660
    89
datatype token =
wenzelm@8660
    90
  Basic of T.token |
wenzelm@9135
    91
  Markup of string |
wenzelm@9135
    92
  MarkupEnv of string |
wenzelm@9135
    93
  Verbatim;
wenzelm@7756
    94
wenzelm@7756
    95
wenzelm@7756
    96
val invisible_token = T.keyword_with (equal ";") orf T.is_kind T.Comment;
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@11012
   109
  | output_tok (Markup cmd, txt) =
wenzelm@11012
   110
      "%\n\\isamarkup" ^ cmd ^ "{" ^ Symbol.strip_blanks txt ^ "%\n}\n"
wenzelm@9135
   111
  | output_tok (MarkupEnv cmd, txt) = "%\n\\begin{isamarkup" ^ cmd ^ "}%\n" ^
wenzelm@11012
   112
      Symbol.strip_blanks txt ^ "%\n\\end{isamarkup" ^ cmd ^ "}%\n"
wenzelm@11012
   113
  | output_tok (Verbatim, txt) = "%\n" ^ Symbol.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@11860
   119
fun flag_markup true = "\\isamarkuptrue%\n"
wenzelm@11860
   120
  | flag_markup false = "\\isamarkupfalse%\n";
wenzelm@11860
   121
wenzelm@11860
   122
wenzelm@7726
   123
(* theory presentation *)
wenzelm@7726
   124
wenzelm@9707
   125
val tex_trailer =
wenzelm@9707
   126
  "%%% Local Variables:\n\
wenzelm@9135
   127
  \%%% mode: latex\n\
wenzelm@9135
   128
  \%%% TeX-master: \"root\"\n\
wenzelm@9144
   129
  \%%% End:\n";
wenzelm@8192
   130
wenzelm@9916
   131
fun isabelle_file name txt =
wenzelm@9916
   132
  "%\n\\begin{isabellebody}%\n\
wenzelm@10247
   133
  \\\def\\isabellecontext{" ^ output_syms name ^ "}%\n" ^ txt ^
wenzelm@9916
   134
  "\\end{isabellebody}%\n" ^ tex_trailer;
wenzelm@9707
   135
wenzelm@8499
   136
fun old_symbol_source name syms =
wenzelm@9916
   137
  isabelle_file name ("\\isamarkupheader{" ^ output_syms name ^ "}%\n" ^ output_symbols syms);
wenzelm@7726
   138
wenzelm@9038
   139
fun theory_entry name = "\\input{" ^ name ^ ".tex}\n\n";
wenzelm@7726
   140
wenzelm@7726
   141
wenzelm@8460
   142
(* print mode *)
wenzelm@8460
   143
wenzelm@8965
   144
val latexN = "latex";
wenzelm@9135
   145
val modes = [latexN, Symbol.xsymbolsN, Symbol.symbolsN];
wenzelm@8965
   146
wenzelm@8460
   147
fun latex_output str =
wenzelm@8460
   148
  let val syms = Symbol.explode str
wenzelm@8460
   149
  in (output_symbols syms, real (Symbol.length syms)) end;
wenzelm@8460
   150
wenzelm@10955
   151
fun latex_indent "" = ""
wenzelm@10955
   152
  | latex_indent s = enclose "\\isaindent{" "}" s;
wenzelm@10955
   153
wenzelm@9558
   154
val token_translation =
wenzelm@9749
   155
  map (fn s => (latexN, s, latex_output)) Syntax.standard_token_classes;
wenzelm@8965
   156
wenzelm@10955
   157
val _ = Symbol.add_mode latexN (latex_output, latex_indent o #1);
wenzelm@8965
   158
val setup = [Theory.add_tokentrfuns token_translation];
wenzelm@8460
   159
wenzelm@8460
   160
wenzelm@7726
   161
end;