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