src/Pure/Thy/latex.ML
author wenzelm
Mon, 11 Dec 2017 17:52:05 +0100
changeset 67184 ecc786cb3b7b
parent 67175 4e5ba4b23731
child 67188 bc7a6455e12a
permissions -rw-r--r--
more robust range on preceding comment-line; no range for blank lines; avoid recursive output_text/mark_range; clarified Latex.output_token (no range) vs. Thy_Output.present_token (with range);
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
    Author:     Markus Wenzel, TU Muenchen
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
     3
15801
d2f5ca3c048d superceded by Pure.thy and CPure.thy;
wenzelm
parents: 14992
diff changeset
     4
LaTeX presentation elements -- based on outer lexical syntax.
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
     5
*)
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
signature LATEX =
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
     8
sig
67145
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
     9
  val output_name: string -> string
58716
23a380cc45f4 official support for "tt" style variants, avoid fragile \verb in LaTeX;
wenzelm
parents: 55828
diff changeset
    10
  val output_ascii: string -> string
63935
aa1fe1103ab8 raw control symbols are superseded by Latex.embed_raw;
wenzelm
parents: 63590
diff changeset
    11
  val latex_control: Symbol.symbol
aa1fe1103ab8 raw control symbols are superseded by Latex.embed_raw;
wenzelm
parents: 63590
diff changeset
    12
  val is_latex_control: Symbol.symbol -> bool
aa1fe1103ab8 raw control symbols are superseded by Latex.embed_raw;
wenzelm
parents: 63590
diff changeset
    13
  val embed_raw: string -> string
14924
2be4cbe27a27 added output_known_symbols; tuned;
wenzelm
parents: 14879
diff changeset
    14
  val output_known_symbols: (string -> bool) * (string -> bool) ->
2be4cbe27a27 added output_known_symbols; tuned;
wenzelm
parents: 14879
diff changeset
    15
    Symbol.symbol list -> string
11719
49c14348a42b Exported output_symbols.
berghofe
parents: 11012
diff changeset
    16
  val output_symbols: Symbol.symbol list -> string
67145
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    17
  val output_syms: string -> string
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
    18
  val range_output: Position.T -> string -> string
61455
0e4c257358cf clarified modules;
wenzelm
parents: 61435
diff changeset
    19
  val output_token: Token.T -> string
17081
e19963723262 eliminated datatype token;
wenzelm
parents: 15801
diff changeset
    20
  val begin_delim: string -> string
e19963723262 eliminated datatype token;
wenzelm
parents: 15801
diff changeset
    21
  val end_delim: string -> string
e19963723262 eliminated datatype token;
wenzelm
parents: 15801
diff changeset
    22
  val begin_tag: string -> string
e19963723262 eliminated datatype token;
wenzelm
parents: 15801
diff changeset
    23
  val end_tag: string -> string
61462
e16649b70107 clarified Latex.environment;
wenzelm
parents: 61455
diff changeset
    24
  val environment: string -> string -> string
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
    25
  val isabelle_theory: Position.T -> string -> string -> string
14924
2be4cbe27a27 added output_known_symbols; tuned;
wenzelm
parents: 14879
diff changeset
    26
  val symbol_source: (string -> bool) * (string -> bool) ->
2be4cbe27a27 added output_known_symbols; tuned;
wenzelm
parents: 14879
diff changeset
    27
    string -> Symbol.symbol list -> string
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    28
  val theory_entry: string -> string
66021
08ab52fb9db5 tuned signature;
wenzelm
parents: 66020
diff changeset
    29
  val latexN: string
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    30
end;
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
structure Latex: LATEX =
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    33
struct
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    34
67145
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    35
(* output name for LaTeX macros *)
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    36
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    37
val output_name =
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    38
  translate_string
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    39
    (fn "_" => "UNDERSCORE"
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    40
      | "'" => "PRIME"
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    41
      | "0" => "ZERO"
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    42
      | "1" => "ONE"
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    43
      | "2" => "TWO"
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    44
      | "3" => "THREE"
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    45
      | "4" => "FOUR"
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    46
      | "5" => "FIVE"
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    47
      | "6" => "SIX"
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    48
      | "7" => "SEVEN"
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    49
      | "8" => "EIGHT"
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    50
      | "9" => "NINE"
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    51
      | s => s);
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    52
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    53
fun enclose_name bg en = enclose bg en o output_name;
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    54
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    55
61455
0e4c257358cf clarified modules;
wenzelm
parents: 61435
diff changeset
    56
(* output verbatim ASCII *)
58716
23a380cc45f4 official support for "tt" style variants, avoid fragile \verb in LaTeX;
wenzelm
parents: 55828
diff changeset
    57
23a380cc45f4 official support for "tt" style variants, avoid fragile \verb in LaTeX;
wenzelm
parents: 55828
diff changeset
    58
val output_ascii =
23a380cc45f4 official support for "tt" style variants, avoid fragile \verb in LaTeX;
wenzelm
parents: 55828
diff changeset
    59
  translate_string
23a380cc45f4 official support for "tt" style variants, avoid fragile \verb in LaTeX;
wenzelm
parents: 55828
diff changeset
    60
    (fn " " => "\\ "
23a380cc45f4 official support for "tt" style variants, avoid fragile \verb in LaTeX;
wenzelm
parents: 55828
diff changeset
    61
      | "\t" => "\\ "
23a380cc45f4 official support for "tt" style variants, avoid fragile \verb in LaTeX;
wenzelm
parents: 55828
diff changeset
    62
      | "\n" => "\\isanewline\n"
23a380cc45f4 official support for "tt" style variants, avoid fragile \verb in LaTeX;
wenzelm
parents: 55828
diff changeset
    63
      | s =>
61578
6623c81cb15a avoid ligatures;
wenzelm
parents: 61516
diff changeset
    64
          if exists_string (fn s' => s = s') "\"#$%&',-<>\\^_`{}~"
58716
23a380cc45f4 official support for "tt" style variants, avoid fragile \verb in LaTeX;
wenzelm
parents: 55828
diff changeset
    65
          then enclose "{\\char`\\" "}" s else s);
23a380cc45f4 official support for "tt" style variants, avoid fragile \verb in LaTeX;
wenzelm
parents: 55828
diff changeset
    66
23a380cc45f4 official support for "tt" style variants, avoid fragile \verb in LaTeX;
wenzelm
parents: 55828
diff changeset
    67
61455
0e4c257358cf clarified modules;
wenzelm
parents: 61435
diff changeset
    68
(* output symbols *)
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    69
63935
aa1fe1103ab8 raw control symbols are superseded by Latex.embed_raw;
wenzelm
parents: 63590
diff changeset
    70
val latex_control = "\<^latex>";
aa1fe1103ab8 raw control symbols are superseded by Latex.embed_raw;
wenzelm
parents: 63590
diff changeset
    71
fun is_latex_control s = s = latex_control;
aa1fe1103ab8 raw control symbols are superseded by Latex.embed_raw;
wenzelm
parents: 63590
diff changeset
    72
aa1fe1103ab8 raw control symbols are superseded by Latex.embed_raw;
wenzelm
parents: 63590
diff changeset
    73
val embed_raw = prefix latex_control o cartouche;
aa1fe1103ab8 raw control symbols are superseded by Latex.embed_raw;
wenzelm
parents: 63590
diff changeset
    74
7900
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
    75
local
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
    76
40402
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
    77
val char_table =
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
    78
  Symtab.make
63590
4854f7ee0987 proper latex rendering of abbrevs templates (e.g. src/HOL/Nonstandard_Analysis/HLim.thy);
wenzelm
parents: 61595
diff changeset
    79
   [("\007", "{\\isacharbell}"),
4854f7ee0987 proper latex rendering of abbrevs templates (e.g. src/HOL/Nonstandard_Analysis/HLim.thy);
wenzelm
parents: 61595
diff changeset
    80
    ("!", "{\\isacharbang}"),
40402
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
    81
    ("\"", "{\\isachardoublequote}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
    82
    ("#", "{\\isacharhash}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
    83
    ("$", "{\\isachardollar}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
    84
    ("%", "{\\isacharpercent}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
    85
    ("&", "{\\isacharampersand}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
    86
    ("'", "{\\isacharprime}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
    87
    ("(", "{\\isacharparenleft}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
    88
    (")", "{\\isacharparenright}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
    89
    ("*", "{\\isacharasterisk}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
    90
    ("+", "{\\isacharplus}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
    91
    (",", "{\\isacharcomma}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
    92
    ("-", "{\\isacharminus}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
    93
    (".", "{\\isachardot}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
    94
    ("/", "{\\isacharslash}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
    95
    (":", "{\\isacharcolon}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
    96
    (";", "{\\isacharsemicolon}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
    97
    ("<", "{\\isacharless}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
    98
    ("=", "{\\isacharequal}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
    99
    (">", "{\\isachargreater}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   100
    ("?", "{\\isacharquery}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   101
    ("@", "{\\isacharat}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   102
    ("[", "{\\isacharbrackleft}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   103
    ("\\", "{\\isacharbackslash}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   104
    ("]", "{\\isacharbrackright}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   105
    ("^", "{\\isacharcircum}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   106
    ("_", "{\\isacharunderscore}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   107
    ("`", "{\\isacharbackquote}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   108
    ("{", "{\\isacharbraceleft}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   109
    ("|", "{\\isacharbar}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   110
    ("}", "{\\isacharbraceright}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   111
    ("~", "{\\isachartilde}")];
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   112
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   113
fun output_chr " " = "\\ "
43709
717e96cf9527 discontinued special treatment of hard tabulators;
wenzelm
parents: 43485
diff changeset
   114
  | output_chr "\t" = "\\ "
40402
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   115
  | output_chr "\n" = "\\isanewline\n"
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   116
  | output_chr c =
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   117
      (case Symtab.lookup char_table c of
49320
94bd2fb83d11 discontinued experiment with literal replacement text in PDF (cf. b646316f8b3c, 2ff10e613689);
wenzelm
parents: 48628
diff changeset
   118
        SOME s => s
40402
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   119
      | NONE => 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
   120
14924
2be4cbe27a27 added output_known_symbols; tuned;
wenzelm
parents: 14879
diff changeset
   121
val output_chrs = translate_string output_chr;
2be4cbe27a27 added output_known_symbols; tuned;
wenzelm
parents: 14879
diff changeset
   122
2be4cbe27a27 added output_known_symbols; tuned;
wenzelm
parents: 14879
diff changeset
   123
fun output_known_sym (known_sym, known_ctrl) sym =
14874
23c73484312f Symbol.decode;
wenzelm
parents: 14840
diff changeset
   124
  (case Symbol.decode sym of
23c73484312f Symbol.decode;
wenzelm
parents: 14840
diff changeset
   125
    Symbol.Char s => output_chr s
37533
d775bd70f571 explicit treatment of UTF8 character sequences as Isabelle symbols;
wenzelm
parents: 36959
diff changeset
   126
  | Symbol.UTF8 s => s
67145
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
   127
  | Symbol.Sym s => if known_sym s then enclose_name "{\\isasym" "}" s else output_chrs sym
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
   128
  | Symbol.Control s => if known_ctrl s then enclose_name "\\isactrl" " " s else output_chrs sym
43485
33a24212a72d more tolerant Symbol.decode;
wenzelm
parents: 42290
diff changeset
   129
  | Symbol.Malformed s => error (Symbol.malformed_msg s)
33a24212a72d more tolerant Symbol.decode;
wenzelm
parents: 42290
diff changeset
   130
  | Symbol.EOF => error "Bad EOF symbol");
7900
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
   131
64526
01920e390645 embedded latex has 0 length -- imitating \<^raw> before aa1fe1103ab8;
wenzelm
parents: 63936
diff changeset
   132
val scan_latex_length =
01920e390645 embedded latex has 0 length -- imitating \<^raw> before aa1fe1103ab8;
wenzelm
parents: 63936
diff changeset
   133
  Scan.many1 (fn (s, _) => Symbol.not_eof s andalso not (is_latex_control s))
01920e390645 embedded latex has 0 length -- imitating \<^raw> before aa1fe1103ab8;
wenzelm
parents: 63936
diff changeset
   134
    >> (Symbol.length o map Symbol_Pos.symbol) ||
01920e390645 embedded latex has 0 length -- imitating \<^raw> before aa1fe1103ab8;
wenzelm
parents: 63936
diff changeset
   135
  Scan.one (is_latex_control o Symbol_Pos.symbol) --
01920e390645 embedded latex has 0 length -- imitating \<^raw> before aa1fe1103ab8;
wenzelm
parents: 63936
diff changeset
   136
    Scan.option (Scan.permissive Symbol_Pos.scan_cartouche "") >> K 0;
01920e390645 embedded latex has 0 length -- imitating \<^raw> before aa1fe1103ab8;
wenzelm
parents: 63936
diff changeset
   137
63935
aa1fe1103ab8 raw control symbols are superseded by Latex.embed_raw;
wenzelm
parents: 63590
diff changeset
   138
fun scan_latex known =
aa1fe1103ab8 raw control symbols are superseded by Latex.embed_raw;
wenzelm
parents: 63590
diff changeset
   139
  Scan.one (is_latex_control o Symbol_Pos.symbol) |--
aa1fe1103ab8 raw control symbols are superseded by Latex.embed_raw;
wenzelm
parents: 63590
diff changeset
   140
    Symbol_Pos.scan_cartouche_content "Embedded LaTeX: " >> (implode o map Symbol_Pos.symbol) ||
aa1fe1103ab8 raw control symbols are superseded by Latex.embed_raw;
wenzelm
parents: 63590
diff changeset
   141
  Scan.one (Symbol.not_eof o Symbol_Pos.symbol) >> (output_known_sym known o Symbol_Pos.symbol);
aa1fe1103ab8 raw control symbols are superseded by Latex.embed_raw;
wenzelm
parents: 63590
diff changeset
   142
64526
01920e390645 embedded latex has 0 length -- imitating \<^raw> before aa1fe1103ab8;
wenzelm
parents: 63936
diff changeset
   143
fun read scan syms =
01920e390645 embedded latex has 0 length -- imitating \<^raw> before aa1fe1103ab8;
wenzelm
parents: 63936
diff changeset
   144
  Scan.read Symbol_Pos.stopper (Scan.repeat scan) (map (rpair Position.none) syms);
63935
aa1fe1103ab8 raw control symbols are superseded by Latex.embed_raw;
wenzelm
parents: 63590
diff changeset
   145
7900
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
   146
in
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
   147
64526
01920e390645 embedded latex has 0 length -- imitating \<^raw> before aa1fe1103ab8;
wenzelm
parents: 63936
diff changeset
   148
fun length_symbols syms =
01920e390645 embedded latex has 0 length -- imitating \<^raw> before aa1fe1103ab8;
wenzelm
parents: 63936
diff changeset
   149
  fold Integer.add (these (read scan_latex_length syms)) 0;
01920e390645 embedded latex has 0 length -- imitating \<^raw> before aa1fe1103ab8;
wenzelm
parents: 63936
diff changeset
   150
63935
aa1fe1103ab8 raw control symbols are superseded by Latex.embed_raw;
wenzelm
parents: 63590
diff changeset
   151
fun output_known_symbols known syms =
64526
01920e390645 embedded latex has 0 length -- imitating \<^raw> before aa1fe1103ab8;
wenzelm
parents: 63936
diff changeset
   152
  if exists is_latex_control syms then
01920e390645 embedded latex has 0 length -- imitating \<^raw> before aa1fe1103ab8;
wenzelm
parents: 63936
diff changeset
   153
    (case read (scan_latex known) syms of
01920e390645 embedded latex has 0 length -- imitating \<^raw> before aa1fe1103ab8;
wenzelm
parents: 63936
diff changeset
   154
      SOME ss => implode ss
01920e390645 embedded latex has 0 length -- imitating \<^raw> before aa1fe1103ab8;
wenzelm
parents: 63936
diff changeset
   155
    | NONE => error ("Malformed embedded LaTeX: " ^ quote (Symbol.beginning 10 syms)))
63935
aa1fe1103ab8 raw control symbols are superseded by Latex.embed_raw;
wenzelm
parents: 63590
diff changeset
   156
  else implode (map (output_known_sym known) syms);
aa1fe1103ab8 raw control symbols are superseded by Latex.embed_raw;
wenzelm
parents: 63590
diff changeset
   157
14924
2be4cbe27a27 added output_known_symbols; tuned;
wenzelm
parents: 14879
diff changeset
   158
val output_symbols = output_known_symbols (K true, K true);
8192
45a7027136e3 added old_symbol_source;
wenzelm
parents: 8117
diff changeset
   159
val output_syms = output_symbols o Symbol.explode;
7900
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
   160
27874
f0364f1c0ecf antiquotes: proper SymbolPos decoding, adapted Antiquote.read/Antiq;
wenzelm
parents: 27809
diff changeset
   161
val output_syms_antiq =
30589
cbe27c4ef417 Antiquote.Text: keep full position information;
wenzelm
parents: 30573
diff changeset
   162
  (fn Antiquote.Text ss => output_symbols (map Symbol_Pos.symbol ss)
61473
34d1913f0b20 clarified control antiquotations: decode control symbol to get name;
wenzelm
parents: 61471
diff changeset
   163
    | Antiquote.Control {name = (name, _), body, ...} =>
34d1913f0b20 clarified control antiquotations: decode control symbol to get name;
wenzelm
parents: 61471
diff changeset
   164
        "\\isaantiqcontrol{" ^ output_symbols (Symbol.explode name) ^ "}" ^
34d1913f0b20 clarified control antiquotations: decode control symbol to get name;
wenzelm
parents: 61471
diff changeset
   165
        output_symbols (map Symbol_Pos.symbol body)
34d1913f0b20 clarified control antiquotations: decode control symbol to get name;
wenzelm
parents: 61471
diff changeset
   166
    | Antiquote.Antiq {body, ...} =>
40402
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   167
        enclose "%\n\\isaantiq\n" "{}%\n\\endisaantiq\n"
61473
34d1913f0b20 clarified control antiquotations: decode control symbol to get name;
wenzelm
parents: 61471
diff changeset
   168
          (output_symbols (map Symbol_Pos.symbol body)));
22648
8c6b4f7548e3 output_basic: added isaantiq markup (only inside verbatim tokens);
wenzelm
parents: 19265
diff changeset
   169
7900
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
   170
end;
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
   171
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
   172
67184
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67175
diff changeset
   173
(* range marker *)
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
   174
67184
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67175
diff changeset
   175
local
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
   176
67184
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67175
diff changeset
   177
val marker = "%:%";
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67175
diff changeset
   178
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67175
diff changeset
   179
fun detect_marker s =
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67175
diff changeset
   180
  size s > 6 andalso String.isPrefix marker s andalso String.isSuffix marker s;
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67175
diff changeset
   181
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67175
diff changeset
   182
fun output_prop (a, b) = enclose marker marker (a ^ "=" ^ b);
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
   183
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
   184
fun output_range pos =
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
   185
  (case (Position.offset_of pos, Position.end_offset_of pos) of
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
   186
    (NONE, _) => ""
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
   187
  | (SOME i, NONE) => output_prop (Markup.rangeN, Value.print_int i)
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
   188
  | (SOME i, SOME j) => output_prop (Markup.rangeN, Value.print_int i ^ ":" ^ Value.print_int j));
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
   189
67184
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67175
diff changeset
   190
fun range_line head range line =
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67175
diff changeset
   191
  if detect_marker line orelse forall_string (fn s => s = " " orelse s = "\t") line then line
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67175
diff changeset
   192
  else if head then "%\n" ^ range ^ "\n" ^ line
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67175
diff changeset
   193
  else range ^ "\n" ^ line;
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67175
diff changeset
   194
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67175
diff changeset
   195
in
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67175
diff changeset
   196
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67175
diff changeset
   197
fun range_output pos output =
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67175
diff changeset
   198
  (case (output_range pos, split_lines output) of
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67175
diff changeset
   199
    (_, []) => ""
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67175
diff changeset
   200
  | ("", _ :: _) => output
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67175
diff changeset
   201
  | (r, l :: ls) => cat_lines (range_line true r l :: map (range_line false r) ls));
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67175
diff changeset
   202
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67175
diff changeset
   203
fun output_file pos =
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67175
diff changeset
   204
  (case Position.file_of pos of
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67175
diff changeset
   205
    NONE => ""
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67175
diff changeset
   206
  | SOME file => output_prop (Markup.fileN, file));
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67175
diff changeset
   207
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67175
diff changeset
   208
end;
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
   209
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
   210
61455
0e4c257358cf clarified modules;
wenzelm
parents: 61435
diff changeset
   211
(* output token *)
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
   212
61455
0e4c257358cf clarified modules;
wenzelm
parents: 61435
diff changeset
   213
fun output_token tok =
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
   214
  let
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
   215
    val s = Token.content_of tok;
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
   216
    val output =
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
   217
      if Token.is_kind Token.Comment tok then ""
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
   218
      else if Token.is_command tok then
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
   219
        "\\isacommand{" ^ output_syms s ^ "}"
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
   220
      else if Token.is_kind Token.Keyword tok andalso Symbol.is_ascii_identifier s then
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
   221
        "\\isakeyword{" ^ output_syms s ^ "}"
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
   222
      else if Token.is_kind Token.String tok then
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
   223
        enclose "{\\isachardoublequoteopen}" "{\\isachardoublequoteclose}" (output_syms s)
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
   224
      else if Token.is_kind Token.Alt_String tok then
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
   225
        enclose "{\\isacharbackquoteopen}" "{\\isacharbackquoteclose}" (output_syms s)
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
   226
      else if Token.is_kind Token.Verbatim tok then
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
   227
        let
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
   228
          val ants = Antiquote.read (Token.input_of tok);
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
   229
          val out = implode (map output_syms_antiq ants);
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
   230
        in enclose "{\\isacharverbatimopen}" "{\\isacharverbatimclose}" out end
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
   231
      else if Token.is_kind Token.Cartouche tok then
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
   232
        enclose "{\\isacartoucheopen}" "{\\isacartoucheclose}" (output_syms s)
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
   233
      else output_syms s;
67184
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67175
diff changeset
   234
  in output end
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
   235
  handle ERROR msg => error (msg ^ Position.here (Token.pos_of tok));
17081
e19963723262 eliminated datatype token;
wenzelm
parents: 15801
diff changeset
   236
61404
a433fecc5ce2 allow control symbols within markup body;
wenzelm
parents: 59809
diff changeset
   237
61455
0e4c257358cf clarified modules;
wenzelm
parents: 61435
diff changeset
   238
(* tags *)
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
   239
67145
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
   240
val begin_delim = enclose_name "%\n\\isadelim" "\n";
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
   241
val end_delim = enclose_name "%\n\\endisadelim" "\n";
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
   242
val begin_tag = enclose_name "%\n\\isatag" "\n";
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
   243
fun end_tag tg = enclose_name "%\n\\endisatag" "\n" tg ^ enclose "{\\isafold" "}%\n" tg;
11860
36ba0d4a836c flag_markup;
wenzelm
parents: 11719
diff changeset
   244
36ba0d4a836c flag_markup;
wenzelm
parents: 11719
diff changeset
   245
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
   246
(* theory presentation *)
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
   247
61462
e16649b70107 clarified Latex.environment;
wenzelm
parents: 61455
diff changeset
   248
fun environment name =
67145
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
   249
  enclose ("%\n\\begin{" ^ output_name name ^ "}%\n") ("%\n\\end{" ^ output_name name ^ "}");
61462
e16649b70107 clarified Latex.environment;
wenzelm
parents: 61455
diff changeset
   250
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
   251
fun isabelle_theory pos name txt =
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
   252
  output_file pos ^
67184
ecc786cb3b7b more robust range on preceding comment-line;
wenzelm
parents: 67175
diff changeset
   253
  "\n\\begin{isabellebody}%\n\
58870
e2c0d8ef29cb more flexibile \setisabellecontext, independently of header;
wenzelm
parents: 58861
diff changeset
   254
  \\\setisabellecontext{" ^ output_syms name ^ "}%\n" ^ txt ^
67175
4e5ba4b23731 removed Emacs legacy;
wenzelm
parents: 67173
diff changeset
   255
  "%\n\\end{isabellebody}%\n";
9707
067c25edd1bd added tex_trailer;
wenzelm
parents: 9668
diff changeset
   256
58870
e2c0d8ef29cb more flexibile \setisabellecontext, independently of header;
wenzelm
parents: 58861
diff changeset
   257
fun symbol_source known name syms =
67173
e746db6db903 more explicit latex errors;
wenzelm
parents: 67145
diff changeset
   258
  isabelle_theory Position.none name
58870
e2c0d8ef29cb more flexibile \setisabellecontext, independently of header;
wenzelm
parents: 58861
diff changeset
   259
    ("\\isamarkupfile{" ^ output_known_symbols known (Symbol.explode name) ^ "}%\n" ^
e2c0d8ef29cb more flexibile \setisabellecontext, independently of header;
wenzelm
parents: 58861
diff changeset
   260
      output_known_symbols known syms);
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
   261
9038
63d20536971f session.tex: nsert blank lines in order to guarantee new paragraphs
wenzelm
parents: 8965
diff changeset
   262
fun theory_entry name = "\\input{" ^ name ^ ".tex}\n\n";
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
   263
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
   264
8460
274426d1adbc tuned comments;
wenzelm
parents: 8192
diff changeset
   265
(* print mode *)
274426d1adbc tuned comments;
wenzelm
parents: 8192
diff changeset
   266
8965
d46b36785c70 proper token_translation for latex mode;
wenzelm
parents: 8896
diff changeset
   267
val latexN = "latex";
d46b36785c70 proper token_translation for latex mode;
wenzelm
parents: 8896
diff changeset
   268
8460
274426d1adbc tuned comments;
wenzelm
parents: 8192
diff changeset
   269
fun latex_output str =
274426d1adbc tuned comments;
wenzelm
parents: 8192
diff changeset
   270
  let val syms = Symbol.explode str
64526
01920e390645 embedded latex has 0 length -- imitating \<^raw> before aa1fe1103ab8;
wenzelm
parents: 63936
diff changeset
   271
  in (output_symbols syms, length_symbols syms) end;
19265
cae36e16f3c0 Output.add_mode: keyword component;
wenzelm
parents: 17218
diff changeset
   272
23621
e070a6ab1891 simplified pretty token metric: type int;
wenzelm
parents: 22648
diff changeset
   273
fun latex_markup (s, _) =
59123
e68e44836d04 imitate command markup and rendering of Isabelle/jEdit in HTML output;
wenzelm
parents: 59081
diff changeset
   274
  if s = Markup.commandN orelse s = Markup.keyword1N orelse s = Markup.keyword3N
e68e44836d04 imitate command markup and rendering of Isabelle/jEdit in HTML output;
wenzelm
parents: 59081
diff changeset
   275
  then ("\\isacommand{", "}")
e68e44836d04 imitate command markup and rendering of Isabelle/jEdit in HTML output;
wenzelm
parents: 59081
diff changeset
   276
  else if s = Markup.keyword2N
e68e44836d04 imitate command markup and rendering of Isabelle/jEdit in HTML output;
wenzelm
parents: 59081
diff changeset
   277
  then ("\\isakeyword{", "}")
29325
a205acc94356 Markup.no_output;
wenzelm
parents: 28375
diff changeset
   278
  else Markup.no_output;
10955
36741b4fe109 setuo indent: \isaindent;
wenzelm
parents: 10393
diff changeset
   279
23621
e070a6ab1891 simplified pretty token metric: type int;
wenzelm
parents: 22648
diff changeset
   280
fun latex_indent "" _ = ""
e070a6ab1891 simplified pretty token metric: type int;
wenzelm
parents: 22648
diff changeset
   281
  | latex_indent s _ = enclose "\\isaindent{" "}" s;
e070a6ab1891 simplified pretty token metric: type int;
wenzelm
parents: 22648
diff changeset
   282
63935
aa1fe1103ab8 raw control symbols are superseded by Latex.embed_raw;
wenzelm
parents: 63590
diff changeset
   283
val _ = Output.add_mode latexN latex_output embed_raw;
23703
1b6a2c119151 Markup.add_mode;
wenzelm
parents: 23621
diff changeset
   284
val _ = Markup.add_mode latexN latex_markup;
1b6a2c119151 Markup.add_mode;
wenzelm
parents: 23621
diff changeset
   285
val _ = Pretty.add_mode latexN latex_indent;
8460
274426d1adbc tuned comments;
wenzelm
parents: 8192
diff changeset
   286
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
   287
end;