src/Pure/Thy/latex.ML
author wenzelm
Wed, 31 Jan 2001 22:16:22 +0100
changeset 11012 8eb472444705
parent 10955 36741b4fe109
child 11719 49c14348a42b
permissions -rw-r--r--
strip_blanks moved to General/symbol.ML;
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
8808
wenzelm
parents: 8660
diff changeset
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
     5
8460
274426d1adbc tuned comments;
wenzelm
parents: 8192
diff changeset
     6
LaTeX presentation primitives (based on outer lexical syntax).
7726
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
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
     9
signature LATEX =
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    10
sig
9135
3aa95ab3f02d adapted to improved presentation;
wenzelm
parents: 9038
diff changeset
    11
  datatype token = Basic of OuterLex.token | Markup of string | MarkupEnv of string | Verbatim
3aa95ab3f02d adapted to improved presentation;
wenzelm
parents: 9038
diff changeset
    12
  val output_tokens: (token * string) list -> string
9707
067c25edd1bd added tex_trailer;
wenzelm
parents: 9668
diff changeset
    13
  val tex_trailer: string
9916
4a703366494b define \isabellecontext;
wenzelm
parents: 9749
diff changeset
    14
  val isabelle_file: string -> string -> string
8499
8958ece3bbdf old_symbol_source: include header;
wenzelm
parents: 8460
diff changeset
    15
  val old_symbol_source: string -> Symbol.symbol list -> string
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    16
  val theory_entry: string -> string
9135
3aa95ab3f02d adapted to improved presentation;
wenzelm
parents: 9038
diff changeset
    17
  val modes: string list
8965
d46b36785c70 proper token_translation for latex mode;
wenzelm
parents: 8896
diff changeset
    18
  val setup: (theory -> theory) list
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    19
end;
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    20
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    21
structure Latex: LATEX =
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    22
struct
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
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    25
(* symbol output *)
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    26
7900
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
    27
local
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
    28
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    29
val output_chr = fn
9505
09c75c801dde improved output of space symbol;
wenzelm
parents: 9144
diff changeset
    30
  " " => "\\ " |
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    31
  "\n" => "\\isanewline\n" |
9668
b5e709fd1e42 more \isachars;
wenzelm
parents: 9663
diff changeset
    32
  "!" => "{\\isacharbang}" |
b5e709fd1e42 more \isachars;
wenzelm
parents: 9663
diff changeset
    33
  "\"" => "{\\isachardoublequote}" |
9663
e4d58f1be05b output \isachar;
wenzelm
parents: 9558
diff changeset
    34
  "#" => "{\\isacharhash}" |
e4d58f1be05b output \isachar;
wenzelm
parents: 9558
diff changeset
    35
  "$" => "{\\isachardollar}" |
e4d58f1be05b output \isachar;
wenzelm
parents: 9558
diff changeset
    36
  "%" => "{\\isacharpercent}" |
e4d58f1be05b output \isachar;
wenzelm
parents: 9558
diff changeset
    37
  "&" => "{\\isacharampersand}" |
e4d58f1be05b output \isachar;
wenzelm
parents: 9558
diff changeset
    38
  "'" => "{\\isacharprime}" |
e4d58f1be05b output \isachar;
wenzelm
parents: 9558
diff changeset
    39
  "(" => "{\\isacharparenleft}" |
e4d58f1be05b output \isachar;
wenzelm
parents: 9558
diff changeset
    40
  ")" => "{\\isacharparenright}" |
e4d58f1be05b output \isachar;
wenzelm
parents: 9558
diff changeset
    41
  "*" => "{\\isacharasterisk}" |
9668
b5e709fd1e42 more \isachars;
wenzelm
parents: 9663
diff changeset
    42
  "+" => "{\\isacharplus}" |
b5e709fd1e42 more \isachars;
wenzelm
parents: 9663
diff changeset
    43
  "," => "{\\isacharcomma}" |
9663
e4d58f1be05b output \isachar;
wenzelm
parents: 9558
diff changeset
    44
  "-" => "{\\isacharminus}" |
9668
b5e709fd1e42 more \isachars;
wenzelm
parents: 9663
diff changeset
    45
  "." => "{\\isachardot}" |
b5e709fd1e42 more \isachars;
wenzelm
parents: 9663
diff changeset
    46
  "/" => "{\\isacharslash}" |
b5e709fd1e42 more \isachars;
wenzelm
parents: 9663
diff changeset
    47
  ":" => "{\\isacharcolon}" |
b5e709fd1e42 more \isachars;
wenzelm
parents: 9663
diff changeset
    48
  ";" => "{\\isacharsemicolon}" |
9663
e4d58f1be05b output \isachar;
wenzelm
parents: 9558
diff changeset
    49
  "<" => "{\\isacharless}" |
9668
b5e709fd1e42 more \isachars;
wenzelm
parents: 9663
diff changeset
    50
  "=" => "{\\isacharequal}" |
9663
e4d58f1be05b output \isachar;
wenzelm
parents: 9558
diff changeset
    51
  ">" => "{\\isachargreater}" |
9668
b5e709fd1e42 more \isachars;
wenzelm
parents: 9663
diff changeset
    52
  "?" => "{\\isacharquery}" |
b5e709fd1e42 more \isachars;
wenzelm
parents: 9663
diff changeset
    53
  "@" => "{\\isacharat}" |
9663
e4d58f1be05b output \isachar;
wenzelm
parents: 9558
diff changeset
    54
  "[" => "{\\isacharbrackleft}" |
e4d58f1be05b output \isachar;
wenzelm
parents: 9558
diff changeset
    55
  "\\" => "{\\isacharbackslash}" |
e4d58f1be05b output \isachar;
wenzelm
parents: 9558
diff changeset
    56
  "]" => "{\\isacharbrackright}" |
e4d58f1be05b output \isachar;
wenzelm
parents: 9558
diff changeset
    57
  "^" => "{\\isacharcircum}" |
e4d58f1be05b output \isachar;
wenzelm
parents: 9558
diff changeset
    58
  "_" => "{\\isacharunderscore}" |
9668
b5e709fd1e42 more \isachars;
wenzelm
parents: 9663
diff changeset
    59
  "`" => "{\\isacharbackquote}" |
9663
e4d58f1be05b output \isachar;
wenzelm
parents: 9558
diff changeset
    60
  "{" => "{\\isacharbraceleft}" |
e4d58f1be05b output \isachar;
wenzelm
parents: 9558
diff changeset
    61
  "|" => "{\\isacharbar}" |
e4d58f1be05b output \isachar;
wenzelm
parents: 9558
diff changeset
    62
  "}" => "{\\isacharbraceright}" |
e4d58f1be05b output \isachar;
wenzelm
parents: 9558
diff changeset
    63
  "~" => "{\\isachartilde}" |
10184
4a7a1091cf65 fully enclose "\isadigit{...}";
wenzelm
parents: 9916
diff changeset
    64
  c => if Symbol.is_digit c then enclose "{\\isadigit{" "}}" c else c;
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    65
7900
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
    66
fun output_sym s =
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
    67
  if size s = 1 then output_chr s
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
    68
  else
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
    69
    (case explode s of
10184
4a7a1091cf65 fully enclose "\isadigit{...}";
wenzelm
parents: 9916
diff changeset
    70
      "\\" :: "<" :: "^" :: cs => "\\isactrl" ^ implode (#1 (Library.split_last cs)) ^ " "
7973
0d801c6e4dc0 \isasymbol renamed to \isasym;
wenzelm
parents: 7900
diff changeset
    71
    | "\\" :: "<" :: cs => "{\\isasym" ^ implode (#1 (Library.split_last cs)) ^ "}"
7900
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
    72
    | _ => sys_error "output_sym");
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
    73
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
    74
in
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
    75
8192
45a7027136e3 added old_symbol_source;
wenzelm
parents: 8117
diff changeset
    76
val output_symbols = implode o map output_sym;
45a7027136e3 added old_symbol_source;
wenzelm
parents: 8117
diff changeset
    77
val output_syms = output_symbols o Symbol.explode;
7900
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
    78
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
    79
end;
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    80
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    81
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    82
(* token output *)
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    83
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    84
structure T = OuterLex;
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    85
8660
e5048a26e1d8 support markup environments;
wenzelm
parents: 8499
diff changeset
    86
datatype token =
e5048a26e1d8 support markup environments;
wenzelm
parents: 8499
diff changeset
    87
  Basic of T.token |
9135
3aa95ab3f02d adapted to improved presentation;
wenzelm
parents: 9038
diff changeset
    88
  Markup of string |
3aa95ab3f02d adapted to improved presentation;
wenzelm
parents: 9038
diff changeset
    89
  MarkupEnv of string |
3aa95ab3f02d adapted to improved presentation;
wenzelm
parents: 9038
diff changeset
    90
  Verbatim;
7756
f9f8660de23f improved presentation;
wenzelm
parents: 7752
diff changeset
    91
f9f8660de23f improved presentation;
wenzelm
parents: 7752
diff changeset
    92
f9f8660de23f improved presentation;
wenzelm
parents: 7752
diff changeset
    93
val invisible_token = T.keyword_with (equal ";") orf T.is_kind T.Comment;
f9f8660de23f improved presentation;
wenzelm
parents: 7752
diff changeset
    94
9135
3aa95ab3f02d adapted to improved presentation;
wenzelm
parents: 9038
diff changeset
    95
fun output_tok (Basic tok, _) =
7752
7ee322caf59c accomodate markup commands;
wenzelm
parents: 7745
diff changeset
    96
      let val s = T.val_of tok in
7756
f9f8660de23f improved presentation;
wenzelm
parents: 7752
diff changeset
    97
        if invisible_token tok then ""
7800
8ee919e42174 improved presentation;
wenzelm
parents: 7789
diff changeset
    98
        else if T.is_kind T.Command tok then
9668
b5e709fd1e42 more \isachars;
wenzelm
parents: 9663
diff changeset
    99
          "\\isacommand{" ^ output_syms s ^ "}"
7800
8ee919e42174 improved presentation;
wenzelm
parents: 7789
diff changeset
   100
        else if T.is_kind T.Keyword tok andalso Syntax.is_identifier s then
7900
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
   101
          "\\isakeyword{" ^ output_syms s ^ "}"
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
   102
        else if T.is_kind T.String tok then output_syms (quote s)
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
   103
        else if T.is_kind T.Verbatim tok then output_syms (enclose "{*" "*}" s)
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
   104
        else output_syms s
7756
f9f8660de23f improved presentation;
wenzelm
parents: 7752
diff changeset
   105
      end
11012
8eb472444705 strip_blanks moved to General/symbol.ML;
wenzelm
parents: 10955
diff changeset
   106
  | output_tok (Markup cmd, txt) =
8eb472444705 strip_blanks moved to General/symbol.ML;
wenzelm
parents: 10955
diff changeset
   107
      "%\n\\isamarkup" ^ cmd ^ "{" ^ Symbol.strip_blanks txt ^ "%\n}\n"
9135
3aa95ab3f02d adapted to improved presentation;
wenzelm
parents: 9038
diff changeset
   108
  | output_tok (MarkupEnv cmd, txt) = "%\n\\begin{isamarkup" ^ cmd ^ "}%\n" ^
11012
8eb472444705 strip_blanks moved to General/symbol.ML;
wenzelm
parents: 10955
diff changeset
   109
      Symbol.strip_blanks txt ^ "%\n\\end{isamarkup" ^ cmd ^ "}%\n"
8eb472444705 strip_blanks moved to General/symbol.ML;
wenzelm
parents: 10955
diff changeset
   110
  | output_tok (Verbatim, txt) = "%\n" ^ Symbol.strip_blanks txt ^ "\n";
7745
131005d3a62d strip_blanks;
wenzelm
parents: 7726
diff changeset
   111
7756
f9f8660de23f improved presentation;
wenzelm
parents: 7752
diff changeset
   112
f9f8660de23f improved presentation;
wenzelm
parents: 7752
diff changeset
   113
val output_tokens = implode o map output_tok;
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
   114
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
   115
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
   116
(* theory presentation *)
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
   117
9707
067c25edd1bd added tex_trailer;
wenzelm
parents: 9668
diff changeset
   118
val tex_trailer =
067c25edd1bd added tex_trailer;
wenzelm
parents: 9668
diff changeset
   119
  "%%% Local Variables:\n\
9135
3aa95ab3f02d adapted to improved presentation;
wenzelm
parents: 9038
diff changeset
   120
  \%%% mode: latex\n\
3aa95ab3f02d adapted to improved presentation;
wenzelm
parents: 9038
diff changeset
   121
  \%%% TeX-master: \"root\"\n\
9144
wenzelm
parents: 9135
diff changeset
   122
  \%%% End:\n";
8192
45a7027136e3 added old_symbol_source;
wenzelm
parents: 8117
diff changeset
   123
9916
4a703366494b define \isabellecontext;
wenzelm
parents: 9749
diff changeset
   124
fun isabelle_file name txt =
4a703366494b define \isabellecontext;
wenzelm
parents: 9749
diff changeset
   125
  "%\n\\begin{isabellebody}%\n\
10247
33e542b4934c \isabellecontext: output_syms;
wenzelm
parents: 10184
diff changeset
   126
  \\\def\\isabellecontext{" ^ output_syms name ^ "}%\n" ^ txt ^
9916
4a703366494b define \isabellecontext;
wenzelm
parents: 9749
diff changeset
   127
  "\\end{isabellebody}%\n" ^ tex_trailer;
9707
067c25edd1bd added tex_trailer;
wenzelm
parents: 9668
diff changeset
   128
8499
8958ece3bbdf old_symbol_source: include header;
wenzelm
parents: 8460
diff changeset
   129
fun old_symbol_source name syms =
9916
4a703366494b define \isabellecontext;
wenzelm
parents: 9749
diff changeset
   130
  isabelle_file name ("\\isamarkupheader{" ^ output_syms name ^ "}%\n" ^ output_symbols syms);
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
   131
9038
63d20536971f session.tex: nsert blank lines in order to guarantee new paragraphs
wenzelm
parents: 8965
diff changeset
   132
fun theory_entry name = "\\input{" ^ name ^ ".tex}\n\n";
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
   133
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
   134
8460
274426d1adbc tuned comments;
wenzelm
parents: 8192
diff changeset
   135
(* print mode *)
274426d1adbc tuned comments;
wenzelm
parents: 8192
diff changeset
   136
8965
d46b36785c70 proper token_translation for latex mode;
wenzelm
parents: 8896
diff changeset
   137
val latexN = "latex";
9135
3aa95ab3f02d adapted to improved presentation;
wenzelm
parents: 9038
diff changeset
   138
val modes = [latexN, Symbol.xsymbolsN, Symbol.symbolsN];
8965
d46b36785c70 proper token_translation for latex mode;
wenzelm
parents: 8896
diff changeset
   139
8460
274426d1adbc tuned comments;
wenzelm
parents: 8192
diff changeset
   140
fun latex_output str =
274426d1adbc tuned comments;
wenzelm
parents: 8192
diff changeset
   141
  let val syms = Symbol.explode str
274426d1adbc tuned comments;
wenzelm
parents: 8192
diff changeset
   142
  in (output_symbols syms, real (Symbol.length syms)) end;
274426d1adbc tuned comments;
wenzelm
parents: 8192
diff changeset
   143
10955
36741b4fe109 setuo indent: \isaindent;
wenzelm
parents: 10393
diff changeset
   144
fun latex_indent "" = ""
36741b4fe109 setuo indent: \isaindent;
wenzelm
parents: 10393
diff changeset
   145
  | latex_indent s = enclose "\\isaindent{" "}" s;
36741b4fe109 setuo indent: \isaindent;
wenzelm
parents: 10393
diff changeset
   146
9558
8d5221bf765b token translation: enclose "\\mbox{" "}";
wenzelm
parents: 9505
diff changeset
   147
val token_translation =
9749
36ddd544a18d token trans: removed \mbox to achieve proper italic correction;
wenzelm
parents: 9707
diff changeset
   148
  map (fn s => (latexN, s, latex_output)) Syntax.standard_token_classes;
8965
d46b36785c70 proper token_translation for latex mode;
wenzelm
parents: 8896
diff changeset
   149
10955
36741b4fe109 setuo indent: \isaindent;
wenzelm
parents: 10393
diff changeset
   150
val _ = Symbol.add_mode latexN (latex_output, latex_indent o #1);
8965
d46b36785c70 proper token_translation for latex mode;
wenzelm
parents: 8896
diff changeset
   151
val setup = [Theory.add_tokentrfuns token_translation];
8460
274426d1adbc tuned comments;
wenzelm
parents: 8192
diff changeset
   152
274426d1adbc tuned comments;
wenzelm
parents: 8192
diff changeset
   153
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
   154
end;