src/Pure/Thy/latex.ML
author wenzelm
Tue, 06 Jun 2000 20:31:22 +0200
changeset 9038 63d20536971f
parent 8965 d46b36785c70
child 9135 3aa95ab3f02d
permissions -rw-r--r--
session.tex: nsert blank lines in order to guarantee new paragraphs for each file (otherwise TeX would easily choke on large input);
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
8660
e5048a26e1d8 support markup environments;
wenzelm
parents: 8499
diff changeset
    11
  datatype token = Basic of OuterLex.token | Markup of string * string |
e5048a26e1d8 support markup environments;
wenzelm
parents: 8499
diff changeset
    12
    MarkupEnv of string * string | Verbatim of string
8499
8958ece3bbdf old_symbol_source: include header;
wenzelm
parents: 8460
diff changeset
    13
  val old_symbol_source: string -> Symbol.symbol list -> string
7756
f9f8660de23f improved presentation;
wenzelm
parents: 7752
diff changeset
    14
  val token_source: token list -> string
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    15
  val theory_entry: string -> string
8965
d46b36785c70 proper token_translation for latex mode;
wenzelm
parents: 8896
diff changeset
    16
  val setup: (theory -> theory) list
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    17
end;
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    18
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    19
structure Latex: LATEX =
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    20
struct
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    21
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    22
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    23
(* symbol output *)
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    24
7900
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
    25
local
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
    26
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    27
val output_chr = fn
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    28
  " " => "~" |
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    29
  "\n" => "\\isanewline\n" |
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    30
  "$" => "\\$" |
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    31
  "&" => "\\&" |
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    32
  "%" => "\\%" |
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    33
  "#" => "\\#" |
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    34
  "_" => "\\_" |
7800
8ee919e42174 improved presentation;
wenzelm
parents: 7789
diff changeset
    35
  "{" => "{\\isabraceleft}" |
8ee919e42174 improved presentation;
wenzelm
parents: 7789
diff changeset
    36
  "}" => "{\\isabraceright}" |
8ee919e42174 improved presentation;
wenzelm
parents: 7789
diff changeset
    37
  "~" => "{\\isatilde}" |
8ee919e42174 improved presentation;
wenzelm
parents: 7789
diff changeset
    38
  "^" => "{\\isacircum}" |
7752
7ee322caf59c accomodate markup commands;
wenzelm
parents: 7745
diff changeset
    39
  "\"" => "{\"}" |
7800
8ee919e42174 improved presentation;
wenzelm
parents: 7789
diff changeset
    40
  "\\" => "{\\isabackslash}" |
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    41
  c => c;
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    42
7900
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
    43
fun output_sym s =
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
    44
  if size s = 1 then output_chr s
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
    45
  else
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
    46
    (case explode s of
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
    47
      cs as "\\" :: "<" :: "^" :: _ => implode (map output_chr cs)
7973
0d801c6e4dc0 \isasymbol renamed to \isasym;
wenzelm
parents: 7900
diff changeset
    48
    | "\\" :: "<" :: cs => "{\\isasym" ^ implode (#1 (Library.split_last cs)) ^ "}"
7900
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
    49
    | _ => sys_error "output_sym");
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
    50
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
    51
in
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
    52
8192
45a7027136e3 added old_symbol_source;
wenzelm
parents: 8117
diff changeset
    53
val output_symbols = implode o map output_sym;
45a7027136e3 added old_symbol_source;
wenzelm
parents: 8117
diff changeset
    54
val output_syms = output_symbols o Symbol.explode;
7900
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
    55
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
    56
end;
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    57
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    58
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    59
(* token output *)
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    60
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    61
structure T = OuterLex;
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    62
8660
e5048a26e1d8 support markup environments;
wenzelm
parents: 8499
diff changeset
    63
datatype token =
e5048a26e1d8 support markup environments;
wenzelm
parents: 8499
diff changeset
    64
  Basic of T.token |
e5048a26e1d8 support markup environments;
wenzelm
parents: 8499
diff changeset
    65
  Markup of string * string |
e5048a26e1d8 support markup environments;
wenzelm
parents: 8499
diff changeset
    66
  MarkupEnv of string * string |
e5048a26e1d8 support markup environments;
wenzelm
parents: 8499
diff changeset
    67
  Verbatim of string;
7756
f9f8660de23f improved presentation;
wenzelm
parents: 7752
diff changeset
    68
f9f8660de23f improved presentation;
wenzelm
parents: 7752
diff changeset
    69
f9f8660de23f improved presentation;
wenzelm
parents: 7752
diff changeset
    70
val invisible_token = T.keyword_with (equal ";") orf T.is_kind T.Comment;
f9f8660de23f improved presentation;
wenzelm
parents: 7752
diff changeset
    71
f9f8660de23f improved presentation;
wenzelm
parents: 7752
diff changeset
    72
fun strip_blanks s =
f9f8660de23f improved presentation;
wenzelm
parents: 7752
diff changeset
    73
  implode (#1 (Library.take_suffix Symbol.is_blank
f9f8660de23f improved presentation;
wenzelm
parents: 7752
diff changeset
    74
    (#2 (Library.take_prefix Symbol.is_blank (explode s)))));
f9f8660de23f improved presentation;
wenzelm
parents: 7752
diff changeset
    75
f9f8660de23f improved presentation;
wenzelm
parents: 7752
diff changeset
    76
fun output_tok (Basic tok) =
7752
7ee322caf59c accomodate markup commands;
wenzelm
parents: 7745
diff changeset
    77
      let val s = T.val_of tok in
7756
f9f8660de23f improved presentation;
wenzelm
parents: 7752
diff changeset
    78
        if invisible_token tok then ""
7800
8ee919e42174 improved presentation;
wenzelm
parents: 7789
diff changeset
    79
        else if T.is_kind T.Command tok then
8896
c80aba8c1d5e replaced {{ }} by { };
wenzelm
parents: 8808
diff changeset
    80
          if s = "{" then "{\\isabeginblock}"
c80aba8c1d5e replaced {{ }} by { };
wenzelm
parents: 8808
diff changeset
    81
          else if s = "}" then "{\\isaendblock}"
7900
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
    82
          else "\\isacommand{" ^ output_syms s ^ "}"
7800
8ee919e42174 improved presentation;
wenzelm
parents: 7789
diff changeset
    83
        else if T.is_kind T.Keyword tok andalso Syntax.is_identifier s then
7900
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
    84
          "\\isakeyword{" ^ output_syms s ^ "}"
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
    85
        else if T.is_kind T.String tok then output_syms (quote s)
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
    86
        else if T.is_kind T.Verbatim tok then output_syms (enclose "{*" "*}" s)
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
    87
        else output_syms s
7756
f9f8660de23f improved presentation;
wenzelm
parents: 7752
diff changeset
    88
      end
7852
d28dff7ac48d markup / varbatim: comment out (%) newline char;
wenzelm
parents: 7800
diff changeset
    89
  | output_tok (Markup (cmd, txt)) = "%\n\\isamarkup" ^ cmd ^ "{" ^ strip_blanks txt ^ "}\n"
8660
e5048a26e1d8 support markup environments;
wenzelm
parents: 8499
diff changeset
    90
  | output_tok (MarkupEnv (cmd, txt)) = "%\n\\begin{isamarkup" ^ cmd ^ "}%\n" ^
e5048a26e1d8 support markup environments;
wenzelm
parents: 8499
diff changeset
    91
      strip_blanks txt ^ "%\n\\end{isamarkup" ^ cmd ^ "}%\n"
7852
d28dff7ac48d markup / varbatim: comment out (%) newline char;
wenzelm
parents: 7800
diff changeset
    92
  | output_tok (Verbatim txt) = "%\n" ^ strip_blanks txt ^ "\n";
7745
131005d3a62d strip_blanks;
wenzelm
parents: 7726
diff changeset
    93
7756
f9f8660de23f improved presentation;
wenzelm
parents: 7752
diff changeset
    94
f9f8660de23f improved presentation;
wenzelm
parents: 7752
diff changeset
    95
val output_tokens = implode o map output_tok;
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    96
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    97
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    98
(* theory presentation *)
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    99
8460
274426d1adbc tuned comments;
wenzelm
parents: 8192
diff changeset
   100
val isabelle_env = enclose "\\begin{isabelle}%\n" "\\end{isabelle}%\n";
8192
45a7027136e3 added old_symbol_source;
wenzelm
parents: 8117
diff changeset
   101
8499
8958ece3bbdf old_symbol_source: include header;
wenzelm
parents: 8460
diff changeset
   102
fun old_symbol_source name syms =
8958ece3bbdf old_symbol_source: include header;
wenzelm
parents: 8460
diff changeset
   103
  isabelle_env ("\\isamarkupheader{" ^ output_syms name ^ "}%\n" ^ output_symbols syms);
8958ece3bbdf old_symbol_source: include header;
wenzelm
parents: 8460
diff changeset
   104
8460
274426d1adbc tuned comments;
wenzelm
parents: 8192
diff changeset
   105
val token_source = isabelle_env o output_tokens;
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
   106
9038
63d20536971f session.tex: nsert blank lines in order to guarantee new paragraphs
wenzelm
parents: 8965
diff changeset
   107
fun theory_entry name = "\\input{" ^ name ^ ".tex}\n\n";
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
   108
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
   109
8460
274426d1adbc tuned comments;
wenzelm
parents: 8192
diff changeset
   110
(* print mode *)
274426d1adbc tuned comments;
wenzelm
parents: 8192
diff changeset
   111
8965
d46b36785c70 proper token_translation for latex mode;
wenzelm
parents: 8896
diff changeset
   112
val latexN = "latex";
d46b36785c70 proper token_translation for latex mode;
wenzelm
parents: 8896
diff changeset
   113
8460
274426d1adbc tuned comments;
wenzelm
parents: 8192
diff changeset
   114
fun latex_output str =
274426d1adbc tuned comments;
wenzelm
parents: 8192
diff changeset
   115
  let val syms = Symbol.explode str
274426d1adbc tuned comments;
wenzelm
parents: 8192
diff changeset
   116
  in (output_symbols syms, real (Symbol.length syms)) end;
274426d1adbc tuned comments;
wenzelm
parents: 8192
diff changeset
   117
8965
d46b36785c70 proper token_translation for latex mode;
wenzelm
parents: 8896
diff changeset
   118
val token_translation = map (fn s => (latexN, s, latex_output)) Syntax.standard_token_classes;
d46b36785c70 proper token_translation for latex mode;
wenzelm
parents: 8896
diff changeset
   119
d46b36785c70 proper token_translation for latex mode;
wenzelm
parents: 8896
diff changeset
   120
val _ = Symbol.add_mode latexN latex_output;
d46b36785c70 proper token_translation for latex mode;
wenzelm
parents: 8896
diff changeset
   121
val setup = [Theory.add_tokentrfuns token_translation];
8460
274426d1adbc tuned comments;
wenzelm
parents: 8192
diff changeset
   122
274426d1adbc tuned comments;
wenzelm
parents: 8192
diff changeset
   123
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
   124
end;