src/Pure/Thy/latex.ML
author wenzelm
Thu Mar 19 15:44:14 2009 +0100 (2009-03-19)
changeset 30589 cbe27c4ef417
parent 30573 49899f26fbd1
child 30590 1d9c9fcf8513
permissions -rw-r--r--
Antiquote.Text: keep full position information;
wenzelm@7726
     1
(*  Title:      Pure/Thy/latex.ML
wenzelm@7726
     2
    Author:     Markus Wenzel, TU Muenchen
wenzelm@7726
     3
wenzelm@15801
     4
LaTeX presentation elements -- based on outer lexical syntax.
wenzelm@7726
     5
*)
wenzelm@7726
     6
wenzelm@7726
     7
signature LATEX =
wenzelm@7726
     8
sig
wenzelm@14924
     9
  val output_known_symbols: (string -> bool) * (string -> bool) ->
wenzelm@14924
    10
    Symbol.symbol list -> string
berghofe@11719
    11
  val output_symbols: Symbol.symbol list -> string
wenzelm@17081
    12
  val output_basic: OuterLex.token -> string
wenzelm@17081
    13
  val output_markup: string -> string -> string
wenzelm@17081
    14
  val output_markup_env: string -> string -> string
wenzelm@17081
    15
  val output_verbatim: string -> string
wenzelm@17081
    16
  val markup_true: string
wenzelm@17081
    17
  val markup_false: string
wenzelm@17081
    18
  val begin_delim: string -> string
wenzelm@17081
    19
  val end_delim: string -> string
wenzelm@17081
    20
  val begin_tag: string -> string
wenzelm@17081
    21
  val end_tag: string -> string
wenzelm@9707
    22
  val tex_trailer: string
wenzelm@9916
    23
  val isabelle_file: string -> string -> string
wenzelm@14924
    24
  val symbol_source: (string -> bool) * (string -> bool) ->
wenzelm@14924
    25
    string -> Symbol.symbol list -> string
wenzelm@7726
    26
  val theory_entry: string -> string
wenzelm@9135
    27
  val modes: string list
wenzelm@7726
    28
end;
wenzelm@7726
    29
wenzelm@7726
    30
structure Latex: LATEX =
wenzelm@7726
    31
struct
wenzelm@7726
    32
wenzelm@7726
    33
wenzelm@7726
    34
(* symbol output *)
wenzelm@7726
    35
wenzelm@7900
    36
local
wenzelm@7900
    37
wenzelm@7726
    38
val output_chr = fn
wenzelm@9505
    39
  " " => "\\ " |
wenzelm@7726
    40
  "\n" => "\\isanewline\n" |
wenzelm@9668
    41
  "!" => "{\\isacharbang}" |
wenzelm@9668
    42
  "\"" => "{\\isachardoublequote}" |
wenzelm@9663
    43
  "#" => "{\\isacharhash}" |
wenzelm@9663
    44
  "$" => "{\\isachardollar}" |
wenzelm@9663
    45
  "%" => "{\\isacharpercent}" |
wenzelm@9663
    46
  "&" => "{\\isacharampersand}" |
wenzelm@9663
    47
  "'" => "{\\isacharprime}" |
wenzelm@9663
    48
  "(" => "{\\isacharparenleft}" |
wenzelm@9663
    49
  ")" => "{\\isacharparenright}" |
wenzelm@9663
    50
  "*" => "{\\isacharasterisk}" |
wenzelm@9668
    51
  "+" => "{\\isacharplus}" |
wenzelm@9668
    52
  "," => "{\\isacharcomma}" |
wenzelm@9663
    53
  "-" => "{\\isacharminus}" |
wenzelm@9668
    54
  "." => "{\\isachardot}" |
wenzelm@9668
    55
  "/" => "{\\isacharslash}" |
wenzelm@9668
    56
  ":" => "{\\isacharcolon}" |
wenzelm@9668
    57
  ";" => "{\\isacharsemicolon}" |
wenzelm@9663
    58
  "<" => "{\\isacharless}" |
wenzelm@9668
    59
  "=" => "{\\isacharequal}" |
wenzelm@9663
    60
  ">" => "{\\isachargreater}" |
wenzelm@9668
    61
  "?" => "{\\isacharquery}" |
wenzelm@9668
    62
  "@" => "{\\isacharat}" |
wenzelm@9663
    63
  "[" => "{\\isacharbrackleft}" |
wenzelm@9663
    64
  "\\" => "{\\isacharbackslash}" |
wenzelm@9663
    65
  "]" => "{\\isacharbrackright}" |
wenzelm@9663
    66
  "^" => "{\\isacharcircum}" |
wenzelm@9663
    67
  "_" => "{\\isacharunderscore}" |
wenzelm@9668
    68
  "`" => "{\\isacharbackquote}" |
wenzelm@9663
    69
  "{" => "{\\isacharbraceleft}" |
wenzelm@9663
    70
  "|" => "{\\isacharbar}" |
wenzelm@9663
    71
  "}" => "{\\isacharbraceright}" |
wenzelm@9663
    72
  "~" => "{\\isachartilde}" |
wenzelm@14992
    73
  c => if Symbol.is_ascii_digit c then enclose "{\\isadigit{" "}}" c else c;
wenzelm@7726
    74
wenzelm@14924
    75
val output_chrs = translate_string output_chr;
wenzelm@14924
    76
wenzelm@14924
    77
fun output_known_sym (known_sym, known_ctrl) sym =
wenzelm@14874
    78
  (case Symbol.decode sym of
wenzelm@14874
    79
    Symbol.Char s => output_chr s
wenzelm@14924
    80
  | Symbol.Sym s => if known_sym s then enclose "{\\isasym" "}" s else output_chrs sym
wenzelm@14924
    81
  | Symbol.Ctrl s => if known_ctrl s then enclose "\\isactrl" " " s else output_chrs sym
wenzelm@14874
    82
  | Symbol.Raw s => s);
wenzelm@7900
    83
wenzelm@7900
    84
in
wenzelm@7900
    85
wenzelm@14924
    86
val output_known_symbols = implode oo (map o output_known_sym);
wenzelm@14924
    87
val output_symbols = output_known_symbols (K true, K true);
wenzelm@8192
    88
val output_syms = output_symbols o Symbol.explode;
wenzelm@7900
    89
wenzelm@27874
    90
val output_syms_antiq =
wenzelm@30589
    91
  (fn Antiquote.Text ss => output_symbols (map Symbol_Pos.symbol ss)
wenzelm@27874
    92
    | Antiquote.Antiq (ss, _) =>
wenzelm@30573
    93
        enclose "%\n\\isaantiq\n" "%\n\\endisaantiq\n" (output_symbols (map Symbol_Pos.symbol ss))
wenzelm@27344
    94
    | Antiquote.Open _ => "{\\isaantiqopen}"
wenzelm@27874
    95
    | Antiquote.Close _ => "{\\isaantiqclose}");
wenzelm@22648
    96
wenzelm@7900
    97
end;
wenzelm@7726
    98
wenzelm@7726
    99
wenzelm@7726
   100
(* token output *)
wenzelm@7726
   101
wenzelm@7726
   102
structure T = OuterLex;
wenzelm@7726
   103
wenzelm@28375
   104
val invisible_token = T.keyword_with (fn s => s = ";") orf T.is_kind T.Comment;
wenzelm@7756
   105
wenzelm@17081
   106
fun output_basic tok =
wenzelm@27809
   107
  let val s = T.content_of tok in
wenzelm@17081
   108
    if invisible_token tok then ""
wenzelm@17081
   109
    else if T.is_kind T.Command tok then
wenzelm@17081
   110
      "\\isacommand{" ^ output_syms s ^ "}"
wenzelm@17081
   111
    else if T.is_kind T.Keyword tok andalso Syntax.is_ascii_identifier s then
wenzelm@17081
   112
      "\\isakeyword{" ^ output_syms s ^ "}"
wenzelm@17169
   113
    else if T.is_kind T.String tok then
wenzelm@17169
   114
      enclose "{\\isachardoublequoteopen}" "{\\isachardoublequoteclose}" (output_syms s)
wenzelm@17169
   115
    else if T.is_kind T.AltString tok then
wenzelm@17169
   116
      enclose "{\\isacharbackquoteopen}" "{\\isacharbackquoteclose}" (output_syms s)
wenzelm@17186
   117
    else if T.is_kind T.Verbatim tok then
wenzelm@27874
   118
      let
wenzelm@27885
   119
        val (txt, pos) = T.source_position_of tok;
wenzelm@30573
   120
        val ants = Antiquote.read (Symbol_Pos.explode (txt, pos), pos);
wenzelm@27874
   121
        val out = implode (map output_syms_antiq ants);
wenzelm@27874
   122
      in enclose "{\\isacharverbatimopen}" "{\\isacharverbatimclose}" out end
wenzelm@17081
   123
    else output_syms s
wenzelm@17081
   124
  end;
wenzelm@17081
   125
wenzelm@17081
   126
fun output_markup cmd txt = "%\n\\isamarkup" ^ cmd ^ "{" ^ Symbol.strip_blanks txt ^ "%\n}\n";
wenzelm@7745
   127
wenzelm@17081
   128
fun output_markup_env cmd txt =
wenzelm@17081
   129
  "%\n\\begin{isamarkup" ^ cmd ^ "}%\n" ^
wenzelm@17081
   130
  Symbol.strip_blanks txt ^
wenzelm@17081
   131
  "%\n\\end{isamarkup" ^ cmd ^ "}%\n";
wenzelm@7756
   132
wenzelm@17081
   133
fun output_verbatim txt = "%\n" ^ Symbol.strip_blanks txt ^ "\n";
wenzelm@7726
   134
wenzelm@17081
   135
val markup_true = "\\isamarkuptrue%\n";
wenzelm@17081
   136
val markup_false = "\\isamarkupfalse%\n";
wenzelm@7726
   137
wenzelm@17081
   138
val begin_delim = enclose "%\n\\isadelim" "\n";
wenzelm@17081
   139
val end_delim = enclose "%\n\\endisadelim" "\n";
wenzelm@17081
   140
val begin_tag = enclose "%\n\\isatag" "\n";
wenzelm@17081
   141
fun end_tag tg = enclose "%\n\\endisatag" "\n" tg ^ enclose "{\\isafold" "}%\n" tg;
wenzelm@11860
   142
wenzelm@11860
   143
wenzelm@7726
   144
(* theory presentation *)
wenzelm@7726
   145
wenzelm@9707
   146
val tex_trailer =
wenzelm@9707
   147
  "%%% Local Variables:\n\
wenzelm@9135
   148
  \%%% mode: latex\n\
wenzelm@9135
   149
  \%%% TeX-master: \"root\"\n\
wenzelm@9144
   150
  \%%% End:\n";
wenzelm@8192
   151
wenzelm@9916
   152
fun isabelle_file name txt =
wenzelm@9916
   153
  "%\n\\begin{isabellebody}%\n\
wenzelm@10247
   154
  \\\def\\isabellecontext{" ^ output_syms name ^ "}%\n" ^ txt ^
wenzelm@9916
   155
  "\\end{isabellebody}%\n" ^ tex_trailer;
wenzelm@9707
   156
wenzelm@14924
   157
fun symbol_source known name syms = isabelle_file name
wenzelm@14924
   158
  ("\\isamarkupheader{" ^ output_known_symbols known (Symbol.explode name) ^ "}%\n" ^
wenzelm@14924
   159
    output_known_symbols known syms);
wenzelm@7726
   160
wenzelm@9038
   161
fun theory_entry name = "\\input{" ^ name ^ ".tex}\n\n";
wenzelm@7726
   162
wenzelm@7726
   163
wenzelm@8460
   164
(* print mode *)
wenzelm@8460
   165
wenzelm@8965
   166
val latexN = "latex";
wenzelm@17218
   167
val modes = [latexN, Symbol.xsymbolsN];
wenzelm@8965
   168
wenzelm@8460
   169
fun latex_output str =
wenzelm@8460
   170
  let val syms = Symbol.explode str
wenzelm@23621
   171
  in (output_symbols syms, Symbol.length syms) end;
wenzelm@19265
   172
wenzelm@23621
   173
fun latex_markup (s, _) =
wenzelm@23621
   174
  if s = Markup.keywordN then ("\\isakeyword{", "}")
wenzelm@23621
   175
  else if s = Markup.commandN then ("\\isacommand{", "}")
wenzelm@29325
   176
  else Markup.no_output;
wenzelm@10955
   177
wenzelm@23621
   178
fun latex_indent "" _ = ""
wenzelm@23621
   179
  | latex_indent s _ = enclose "\\isaindent{" "}" s;
wenzelm@23621
   180
wenzelm@23621
   181
val _ = Output.add_mode latexN latex_output Symbol.encode_raw;
wenzelm@23703
   182
val _ = Markup.add_mode latexN latex_markup;
wenzelm@23703
   183
val _ = Pretty.add_mode latexN latex_indent;
wenzelm@8460
   184
wenzelm@7726
   185
end;