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