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