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