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