src/Pure/Thy/latex.ML
author wenzelm
Sat, 13 Nov 2021 17:26:35 +0100
changeset 74779 5fca489a6ac1
parent 73780 466fae6bf22e
child 74780 6504e9b09926
permissions -rw-r--r--
tuned output --- less redundancy;
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
73754
cd7eb3cdab4c support for index entries;
wenzelm
parents: 72315
diff changeset
     2
    Author:     Makarius
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
     3
73754
cd7eb3cdab4c support for index entries;
wenzelm
parents: 72315
diff changeset
     4
LaTeX output of theory sources.
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
73780
466fae6bf22e compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents: 73779
diff changeset
     9
  type text = XML.body
67194
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67189
diff changeset
    10
  val text: string * Position.T -> text
73780
466fae6bf22e compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents: 73779
diff changeset
    11
  val string: string -> text
466fae6bf22e compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents: 73779
diff changeset
    12
  val block: text -> XML.tree
466fae6bf22e compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents: 73779
diff changeset
    13
  val enclose_text: string -> string -> text -> text
466fae6bf22e compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents: 73779
diff changeset
    14
  val latex_text: text -> text
466fae6bf22e compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents: 73779
diff changeset
    15
  val output_text: text -> string
67145
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    16
  val output_name: string -> string
58716
23a380cc45f4 official support for "tt" style variants, avoid fragile \verb in LaTeX;
wenzelm
parents: 55828
diff changeset
    17
  val output_ascii: string -> string
71899
9a12eb655f67 tuned signature;
wenzelm
parents: 69346
diff changeset
    18
  val output_ascii_breakable: string -> string -> string
11719
49c14348a42b Exported output_symbols.
berghofe
parents: 11012
diff changeset
    19
  val output_symbols: Symbol.symbol list -> string
67145
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    20
  val output_syms: string -> string
67461
aad5c0982c3d tuned signature;
wenzelm
parents: 67428
diff changeset
    21
  val symbols: Symbol_Pos.T list -> text
aad5c0982c3d tuned signature;
wenzelm
parents: 67428
diff changeset
    22
  val symbols_output: Symbol_Pos.T list -> text
17081
e19963723262 eliminated datatype token;
wenzelm
parents: 15801
diff changeset
    23
  val begin_delim: string -> string
e19963723262 eliminated datatype token;
wenzelm
parents: 15801
diff changeset
    24
  val end_delim: string -> string
e19963723262 eliminated datatype token;
wenzelm
parents: 15801
diff changeset
    25
  val begin_tag: string -> string
e19963723262 eliminated datatype token;
wenzelm
parents: 15801
diff changeset
    26
  val end_tag: string -> string
73780
466fae6bf22e compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents: 73779
diff changeset
    27
  val environment_text: string -> text -> text
61462
e16649b70107 clarified Latex.environment;
wenzelm
parents: 61455
diff changeset
    28
  val environment: string -> string -> string
73780
466fae6bf22e compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents: 73779
diff changeset
    29
  val isabelle_body: string -> text -> text
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    30
  val theory_entry: string -> string
73754
cd7eb3cdab4c support for index entries;
wenzelm
parents: 72315
diff changeset
    31
  val index_escape: string -> string
cd7eb3cdab4c support for index entries;
wenzelm
parents: 72315
diff changeset
    32
  type index_item = {text: text, like: string}
cd7eb3cdab4c support for index entries;
wenzelm
parents: 72315
diff changeset
    33
  type index_entry = {items: index_item list, def: bool}
cd7eb3cdab4c support for index entries;
wenzelm
parents: 72315
diff changeset
    34
  val index_entry: index_entry -> text
73763
eccc4a13216d clarified index antiquotation for ML: more ambitious type-setting, more accurate syntax;
wenzelm
parents: 73754
diff changeset
    35
  val index_variants: (binding -> bool option -> 'a -> 'a) -> binding -> 'a -> 'a
66021
08ab52fb9db5 tuned signature;
wenzelm
parents: 66020
diff changeset
    36
  val latexN: string
69346
3c29edccf739 expose latex mode operations, to facilitate adhoc changes to it;
wenzelm
parents: 67462
diff changeset
    37
  val latex_output: string -> string * int
3c29edccf739 expose latex mode operations, to facilitate adhoc changes to it;
wenzelm
parents: 67462
diff changeset
    38
  val latex_markup: string * Properties.T -> Markup.output
3c29edccf739 expose latex mode operations, to facilitate adhoc changes to it;
wenzelm
parents: 67462
diff changeset
    39
  val latex_indent: string -> int -> string
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    40
end;
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    41
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    42
structure Latex: LATEX =
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    43
struct
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
    44
67194
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67189
diff changeset
    45
(* text with positions *)
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67189
diff changeset
    46
73780
466fae6bf22e compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents: 73779
diff changeset
    47
type text = XML.body;
73754
cd7eb3cdab4c support for index entries;
wenzelm
parents: 72315
diff changeset
    48
74779
5fca489a6ac1 tuned output --- less redundancy;
wenzelm
parents: 73780
diff changeset
    49
fun text (s, pos) =
5fca489a6ac1 tuned output --- less redundancy;
wenzelm
parents: 73780
diff changeset
    50
  if s = "" then []
5fca489a6ac1 tuned output --- less redundancy;
wenzelm
parents: 73780
diff changeset
    51
  else if pos = Position.none then [XML.Text s]
5fca489a6ac1 tuned output --- less redundancy;
wenzelm
parents: 73780
diff changeset
    52
  else [XML.Elem (Position.markup pos Markup.document_latex, [XML.Text s])];
67194
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67189
diff changeset
    53
73780
466fae6bf22e compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents: 73779
diff changeset
    54
fun string s = text (s, Position.none);
466fae6bf22e compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents: 73779
diff changeset
    55
466fae6bf22e compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents: 73779
diff changeset
    56
fun block body = XML.Elem (Markup.document_latex, body);
67195
6be90977f882 avoid redundant positions;
wenzelm
parents: 67194
diff changeset
    57
73780
466fae6bf22e compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents: 73779
diff changeset
    58
fun latex_text text =
466fae6bf22e compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents: 73779
diff changeset
    59
  text |> maps
466fae6bf22e compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents: 73779
diff changeset
    60
    (fn XML.Elem ((name, _), body) =>
466fae6bf22e compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents: 73779
diff changeset
    61
          if name = Markup.document_latexN then latex_text body else []
466fae6bf22e compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents: 73779
diff changeset
    62
      | t => [t]);
67194
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67189
diff changeset
    63
73780
466fae6bf22e compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents: 73779
diff changeset
    64
val output_text = XML.content_of o latex_text;
67194
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67189
diff changeset
    65
73780
466fae6bf22e compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents: 73779
diff changeset
    66
fun enclose_text bg en body = string bg @ body @ string en;
67462
c23d9375e661 more operations;
wenzelm
parents: 67461
diff changeset
    67
67194
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67189
diff changeset
    68
67145
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    69
(* output name for LaTeX macros *)
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    70
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    71
val output_name =
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    72
  translate_string
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    73
    (fn "_" => "UNDERSCORE"
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    74
      | "'" => "PRIME"
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    75
      | "0" => "ZERO"
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    76
      | "1" => "ONE"
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    77
      | "2" => "TWO"
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    78
      | "3" => "THREE"
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    79
      | "4" => "FOUR"
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    80
      | "5" => "FIVE"
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    81
      | "6" => "SIX"
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    82
      | "7" => "SEVEN"
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    83
      | "8" => "EIGHT"
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    84
      | "9" => "NINE"
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    85
      | s => s);
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    86
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    87
fun enclose_name bg en = enclose bg en o output_name;
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    88
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
    89
61455
0e4c257358cf clarified modules;
wenzelm
parents: 61435
diff changeset
    90
(* output verbatim ASCII *)
58716
23a380cc45f4 official support for "tt" style variants, avoid fragile \verb in LaTeX;
wenzelm
parents: 55828
diff changeset
    91
23a380cc45f4 official support for "tt" style variants, avoid fragile \verb in LaTeX;
wenzelm
parents: 55828
diff changeset
    92
val output_ascii =
23a380cc45f4 official support for "tt" style variants, avoid fragile \verb in LaTeX;
wenzelm
parents: 55828
diff changeset
    93
  translate_string
23a380cc45f4 official support for "tt" style variants, avoid fragile \verb in LaTeX;
wenzelm
parents: 55828
diff changeset
    94
    (fn " " => "\\ "
23a380cc45f4 official support for "tt" style variants, avoid fragile \verb in LaTeX;
wenzelm
parents: 55828
diff changeset
    95
      | "\t" => "\\ "
23a380cc45f4 official support for "tt" style variants, avoid fragile \verb in LaTeX;
wenzelm
parents: 55828
diff changeset
    96
      | "\n" => "\\isanewline\n"
23a380cc45f4 official support for "tt" style variants, avoid fragile \verb in LaTeX;
wenzelm
parents: 55828
diff changeset
    97
      | s =>
72315
8162ca81ea8a suppress ligatures more robustly, notably for lualatex;
wenzelm
parents: 71899
diff changeset
    98
          s
8162ca81ea8a suppress ligatures more robustly, notably for lualatex;
wenzelm
parents: 71899
diff changeset
    99
          |> exists_string (fn s' => s = s') "\"#$%&',-<>\\^_`{}~" ? enclose "{\\char`\\" "}"
8162ca81ea8a suppress ligatures more robustly, notably for lualatex;
wenzelm
parents: 71899
diff changeset
   100
          |> suffix "{\\kern0pt}");
58716
23a380cc45f4 official support for "tt" style variants, avoid fragile \verb in LaTeX;
wenzelm
parents: 55828
diff changeset
   101
71899
9a12eb655f67 tuned signature;
wenzelm
parents: 69346
diff changeset
   102
fun output_ascii_breakable sep =
9a12eb655f67 tuned signature;
wenzelm
parents: 69346
diff changeset
   103
  space_explode sep
9a12eb655f67 tuned signature;
wenzelm
parents: 69346
diff changeset
   104
  #> map output_ascii
9a12eb655f67 tuned signature;
wenzelm
parents: 69346
diff changeset
   105
  #> space_implode (output_ascii sep ^ "\\discretionary{}{}{}");
9a12eb655f67 tuned signature;
wenzelm
parents: 69346
diff changeset
   106
58716
23a380cc45f4 official support for "tt" style variants, avoid fragile \verb in LaTeX;
wenzelm
parents: 55828
diff changeset
   107
61455
0e4c257358cf clarified modules;
wenzelm
parents: 61435
diff changeset
   108
(* output symbols *)
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
   109
7900
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
   110
local
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
   111
40402
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   112
val char_table =
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   113
  Symtab.make
63590
4854f7ee0987 proper latex rendering of abbrevs templates (e.g. src/HOL/Nonstandard_Analysis/HLim.thy);
wenzelm
parents: 61595
diff changeset
   114
   [("\007", "{\\isacharbell}"),
4854f7ee0987 proper latex rendering of abbrevs templates (e.g. src/HOL/Nonstandard_Analysis/HLim.thy);
wenzelm
parents: 61595
diff changeset
   115
    ("!", "{\\isacharbang}"),
40402
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   116
    ("\"", "{\\isachardoublequote}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   117
    ("#", "{\\isacharhash}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   118
    ("$", "{\\isachardollar}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   119
    ("%", "{\\isacharpercent}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   120
    ("&", "{\\isacharampersand}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   121
    ("'", "{\\isacharprime}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   122
    ("(", "{\\isacharparenleft}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   123
    (")", "{\\isacharparenright}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   124
    ("*", "{\\isacharasterisk}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   125
    ("+", "{\\isacharplus}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   126
    (",", "{\\isacharcomma}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   127
    ("-", "{\\isacharminus}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   128
    (".", "{\\isachardot}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   129
    ("/", "{\\isacharslash}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   130
    (":", "{\\isacharcolon}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   131
    (";", "{\\isacharsemicolon}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   132
    ("<", "{\\isacharless}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   133
    ("=", "{\\isacharequal}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   134
    (">", "{\\isachargreater}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   135
    ("?", "{\\isacharquery}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   136
    ("@", "{\\isacharat}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   137
    ("[", "{\\isacharbrackleft}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   138
    ("\\", "{\\isacharbackslash}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   139
    ("]", "{\\isacharbrackright}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   140
    ("^", "{\\isacharcircum}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   141
    ("_", "{\\isacharunderscore}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   142
    ("`", "{\\isacharbackquote}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   143
    ("{", "{\\isacharbraceleft}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   144
    ("|", "{\\isacharbar}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   145
    ("}", "{\\isacharbraceright}"),
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   146
    ("~", "{\\isachartilde}")];
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   147
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   148
fun output_chr " " = "\\ "
43709
717e96cf9527 discontinued special treatment of hard tabulators;
wenzelm
parents: 43485
diff changeset
   149
  | 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
   150
  | 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
   151
  | output_chr c =
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   152
      (case Symtab.lookup char_table c of
72315
8162ca81ea8a suppress ligatures more robustly, notably for lualatex;
wenzelm
parents: 71899
diff changeset
   153
        SOME s => s ^ "{\\kern0pt}"
40402
b646316f8b3c basic setup for literal replacement text in PDF, to support copy/paste of Isabelle symbols;
wenzelm
parents: 39666
diff changeset
   154
      | 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
   155
67263
449a989f42cd discontinued 'display_drafts' command;
wenzelm
parents: 67197
diff changeset
   156
fun output_sym sym =
14874
23c73484312f Symbol.decode;
wenzelm
parents: 14840
diff changeset
   157
  (case Symbol.decode sym of
23c73484312f Symbol.decode;
wenzelm
parents: 14840
diff changeset
   158
    Symbol.Char s => output_chr s
37533
d775bd70f571 explicit treatment of UTF8 character sequences as Isabelle symbols;
wenzelm
parents: 36959
diff changeset
   159
  | Symbol.UTF8 s => s
67263
449a989f42cd discontinued 'display_drafts' command;
wenzelm
parents: 67197
diff changeset
   160
  | Symbol.Sym s => enclose_name "{\\isasym" "}" s
449a989f42cd discontinued 'display_drafts' command;
wenzelm
parents: 67197
diff changeset
   161
  | Symbol.Control s => enclose_name "\\isactrl" " " s
43485
33a24212a72d more tolerant Symbol.decode;
wenzelm
parents: 42290
diff changeset
   162
  | Symbol.Malformed s => error (Symbol.malformed_msg s)
33a24212a72d more tolerant Symbol.decode;
wenzelm
parents: 42290
diff changeset
   163
  | Symbol.EOF => error "Bad EOF symbol");
7900
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
   164
67428
dd8e40f46962 clarified signature;
wenzelm
parents: 67358
diff changeset
   165
open Basic_Symbol_Pos;
dd8e40f46962 clarified signature;
wenzelm
parents: 67358
diff changeset
   166
64526
01920e390645 embedded latex has 0 length -- imitating \<^raw> before aa1fe1103ab8;
wenzelm
parents: 63936
diff changeset
   167
val scan_latex_length =
67428
dd8e40f46962 clarified signature;
wenzelm
parents: 67358
diff changeset
   168
  Scan.many1 (fn (s, _) => s <> Symbol.latex andalso Symbol.not_eof s)
64526
01920e390645 embedded latex has 0 length -- imitating \<^raw> before aa1fe1103ab8;
wenzelm
parents: 63936
diff changeset
   169
    >> (Symbol.length o map Symbol_Pos.symbol) ||
67428
dd8e40f46962 clarified signature;
wenzelm
parents: 67358
diff changeset
   170
  $$ Symbol.latex -- Scan.option (Scan.permissive Symbol_Pos.scan_cartouche "") >> K 0;
64526
01920e390645 embedded latex has 0 length -- imitating \<^raw> before aa1fe1103ab8;
wenzelm
parents: 63936
diff changeset
   171
67263
449a989f42cd discontinued 'display_drafts' command;
wenzelm
parents: 67197
diff changeset
   172
val scan_latex =
67428
dd8e40f46962 clarified signature;
wenzelm
parents: 67358
diff changeset
   173
  $$ Symbol.latex |-- Symbol_Pos.scan_cartouche_content "Embedded LaTeX: "
dd8e40f46962 clarified signature;
wenzelm
parents: 67358
diff changeset
   174
    >> (implode o map Symbol_Pos.symbol) ||
67263
449a989f42cd discontinued 'display_drafts' command;
wenzelm
parents: 67197
diff changeset
   175
  Scan.one (Symbol.not_eof o Symbol_Pos.symbol) >> (output_sym o Symbol_Pos.symbol);
63935
aa1fe1103ab8 raw control symbols are superseded by Latex.embed_raw;
wenzelm
parents: 63590
diff changeset
   176
64526
01920e390645 embedded latex has 0 length -- imitating \<^raw> before aa1fe1103ab8;
wenzelm
parents: 63936
diff changeset
   177
fun read scan syms =
01920e390645 embedded latex has 0 length -- imitating \<^raw> before aa1fe1103ab8;
wenzelm
parents: 63936
diff changeset
   178
  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
   179
7900
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
   180
in
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
   181
64526
01920e390645 embedded latex has 0 length -- imitating \<^raw> before aa1fe1103ab8;
wenzelm
parents: 63936
diff changeset
   182
fun length_symbols syms =
01920e390645 embedded latex has 0 length -- imitating \<^raw> before aa1fe1103ab8;
wenzelm
parents: 63936
diff changeset
   183
  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
   184
67263
449a989f42cd discontinued 'display_drafts' command;
wenzelm
parents: 67197
diff changeset
   185
fun output_symbols syms =
67428
dd8e40f46962 clarified signature;
wenzelm
parents: 67358
diff changeset
   186
  if member (op =) syms Symbol.latex then
67263
449a989f42cd discontinued 'display_drafts' command;
wenzelm
parents: 67197
diff changeset
   187
    (case read scan_latex syms of
64526
01920e390645 embedded latex has 0 length -- imitating \<^raw> before aa1fe1103ab8;
wenzelm
parents: 63936
diff changeset
   188
      SOME ss => implode ss
01920e390645 embedded latex has 0 length -- imitating \<^raw> before aa1fe1103ab8;
wenzelm
parents: 63936
diff changeset
   189
    | NONE => error ("Malformed embedded LaTeX: " ^ quote (Symbol.beginning 10 syms)))
67263
449a989f42cd discontinued 'display_drafts' command;
wenzelm
parents: 67197
diff changeset
   190
  else implode (map output_sym syms);
63935
aa1fe1103ab8 raw control symbols are superseded by Latex.embed_raw;
wenzelm
parents: 63590
diff changeset
   191
8192
45a7027136e3 added old_symbol_source;
wenzelm
parents: 8117
diff changeset
   192
val output_syms = output_symbols o Symbol.explode;
7900
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
   193
e62973fccc97 output \isasymbols;
wenzelm
parents: 7852
diff changeset
   194
end;
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
   195
67461
aad5c0982c3d tuned signature;
wenzelm
parents: 67428
diff changeset
   196
fun symbols syms = text (Symbol_Pos.content syms, #1 (Symbol_Pos.range syms));
aad5c0982c3d tuned signature;
wenzelm
parents: 67428
diff changeset
   197
fun symbols_output syms =
aad5c0982c3d tuned signature;
wenzelm
parents: 67428
diff changeset
   198
  text (output_symbols (map Symbol_Pos.symbol syms), #1 (Symbol_Pos.range syms));
aad5c0982c3d tuned signature;
wenzelm
parents: 67428
diff changeset
   199
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
   200
61455
0e4c257358cf clarified modules;
wenzelm
parents: 61435
diff changeset
   201
(* tags *)
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
   202
67145
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
   203
val begin_delim = enclose_name "%\n\\isadelim" "\n";
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
   204
val end_delim = enclose_name "%\n\\endisadelim" "\n";
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
   205
val begin_tag = enclose_name "%\n\\isatag" "\n";
e77c5bfca9aa name mangling for Latex macros;
wenzelm
parents: 66021
diff changeset
   206
fun end_tag tg = enclose_name "%\n\\endisatag" "\n" tg ^ enclose "{\\isafold" "}%\n" tg;
11860
36ba0d4a836c flag_markup;
wenzelm
parents: 11719
diff changeset
   207
36ba0d4a836c flag_markup;
wenzelm
parents: 11719
diff changeset
   208
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
   209
(* theory presentation *)
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
   210
67194
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67189
diff changeset
   211
fun environment_delim name =
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67189
diff changeset
   212
 ("%\n\\begin{" ^ output_name name ^ "}%\n",
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67189
diff changeset
   213
  "%\n\\end{" ^ output_name name ^ "}");
61462
e16649b70107 clarified Latex.environment;
wenzelm
parents: 61455
diff changeset
   214
73780
466fae6bf22e compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents: 73779
diff changeset
   215
fun environment_text name = environment_delim name |-> enclose_text;
67194
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67189
diff changeset
   216
fun environment name = environment_delim name |-> enclose;
1c0a6a957114 positions as postlude: avoid intrusion of odd %-forms into main tex source;
wenzelm
parents: 67189
diff changeset
   217
67263
449a989f42cd discontinued 'display_drafts' command;
wenzelm
parents: 67197
diff changeset
   218
fun isabelle_body name =
73780
466fae6bf22e compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents: 73779
diff changeset
   219
  enclose_text
67263
449a989f42cd discontinued 'display_drafts' command;
wenzelm
parents: 67197
diff changeset
   220
   ("%\n\\begin{isabellebody}%\n\\setisabellecontext{" ^ output_syms name ^ "}%\n")
449a989f42cd discontinued 'display_drafts' command;
wenzelm
parents: 67197
diff changeset
   221
   "%\n\\end{isabellebody}%\n";
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
   222
9038
63d20536971f session.tex: nsert blank lines in order to guarantee new paragraphs
wenzelm
parents: 8965
diff changeset
   223
fun theory_entry name = "\\input{" ^ name ^ ".tex}\n\n";
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
   224
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
   225
73754
cd7eb3cdab4c support for index entries;
wenzelm
parents: 72315
diff changeset
   226
(* index entries *)
cd7eb3cdab4c support for index entries;
wenzelm
parents: 72315
diff changeset
   227
cd7eb3cdab4c support for index entries;
wenzelm
parents: 72315
diff changeset
   228
type index_item = {text: text, like: string};
cd7eb3cdab4c support for index entries;
wenzelm
parents: 72315
diff changeset
   229
type index_entry = {items: index_item list, def: bool};
cd7eb3cdab4c support for index entries;
wenzelm
parents: 72315
diff changeset
   230
cd7eb3cdab4c support for index entries;
wenzelm
parents: 72315
diff changeset
   231
val index_escape =
cd7eb3cdab4c support for index entries;
wenzelm
parents: 72315
diff changeset
   232
  translate_string (fn s =>
73763
eccc4a13216d clarified index antiquotation for ML: more ambitious type-setting, more accurate syntax;
wenzelm
parents: 73754
diff changeset
   233
    if member_string "!\"@|" s then "\\char" ^ string_of_int (ord s)
eccc4a13216d clarified index antiquotation for ML: more ambitious type-setting, more accurate syntax;
wenzelm
parents: 73754
diff changeset
   234
    else if member_string "\\{}#" s then "\"" ^ s else s);
73754
cd7eb3cdab4c support for index entries;
wenzelm
parents: 72315
diff changeset
   235
cd7eb3cdab4c support for index entries;
wenzelm
parents: 72315
diff changeset
   236
fun index_item (item: index_item) =
cd7eb3cdab4c support for index entries;
wenzelm
parents: 72315
diff changeset
   237
  let
cd7eb3cdab4c support for index entries;
wenzelm
parents: 72315
diff changeset
   238
    val like_text =
73779
546e1e591635 more direct index_entry: no positions required -- text is eventually moved to .ind file;
wenzelm
parents: 73763
diff changeset
   239
      if #like item = "" then ""
546e1e591635 more direct index_entry: no positions required -- text is eventually moved to .ind file;
wenzelm
parents: 73763
diff changeset
   240
      else index_escape (#like item) ^ "@";
73780
466fae6bf22e compose Latex text as XML, output exported YXML in Isabelle/Scala;
wenzelm
parents: 73779
diff changeset
   241
    val item_text = index_escape (output_text (#text item));
73779
546e1e591635 more direct index_entry: no positions required -- text is eventually moved to .ind file;
wenzelm
parents: 73763
diff changeset
   242
  in like_text ^ item_text end;
73754
cd7eb3cdab4c support for index entries;
wenzelm
parents: 72315
diff changeset
   243
cd7eb3cdab4c support for index entries;
wenzelm
parents: 72315
diff changeset
   244
fun index_entry (entry: index_entry) =
73779
546e1e591635 more direct index_entry: no positions required -- text is eventually moved to .ind file;
wenzelm
parents: 73763
diff changeset
   245
  (space_implode "!" (map index_item (#items entry)) ^
546e1e591635 more direct index_entry: no positions required -- text is eventually moved to .ind file;
wenzelm
parents: 73763
diff changeset
   246
    "|" ^ index_escape (if #def entry then "isaindexdef" else "isaindexref"))
546e1e591635 more direct index_entry: no positions required -- text is eventually moved to .ind file;
wenzelm
parents: 73763
diff changeset
   247
  |> enclose "\\index{" "}"
546e1e591635 more direct index_entry: no positions required -- text is eventually moved to .ind file;
wenzelm
parents: 73763
diff changeset
   248
  |> string;
73754
cd7eb3cdab4c support for index entries;
wenzelm
parents: 72315
diff changeset
   249
73763
eccc4a13216d clarified index antiquotation for ML: more ambitious type-setting, more accurate syntax;
wenzelm
parents: 73754
diff changeset
   250
fun index_binding NONE = I
eccc4a13216d clarified index antiquotation for ML: more ambitious type-setting, more accurate syntax;
wenzelm
parents: 73754
diff changeset
   251
  | index_binding (SOME def) = Binding.map_name (suffix (if def then "_def" else "_ref"));
eccc4a13216d clarified index antiquotation for ML: more ambitious type-setting, more accurate syntax;
wenzelm
parents: 73754
diff changeset
   252
eccc4a13216d clarified index antiquotation for ML: more ambitious type-setting, more accurate syntax;
wenzelm
parents: 73754
diff changeset
   253
fun index_variants setup binding =
eccc4a13216d clarified index antiquotation for ML: more ambitious type-setting, more accurate syntax;
wenzelm
parents: 73754
diff changeset
   254
  fold (fn index => setup (index_binding index binding) index) [NONE, SOME true, SOME false];
eccc4a13216d clarified index antiquotation for ML: more ambitious type-setting, more accurate syntax;
wenzelm
parents: 73754
diff changeset
   255
eccc4a13216d clarified index antiquotation for ML: more ambitious type-setting, more accurate syntax;
wenzelm
parents: 73754
diff changeset
   256
8460
274426d1adbc tuned comments;
wenzelm
parents: 8192
diff changeset
   257
(* print mode *)
274426d1adbc tuned comments;
wenzelm
parents: 8192
diff changeset
   258
8965
d46b36785c70 proper token_translation for latex mode;
wenzelm
parents: 8896
diff changeset
   259
val latexN = "latex";
d46b36785c70 proper token_translation for latex mode;
wenzelm
parents: 8896
diff changeset
   260
8460
274426d1adbc tuned comments;
wenzelm
parents: 8192
diff changeset
   261
fun latex_output str =
274426d1adbc tuned comments;
wenzelm
parents: 8192
diff changeset
   262
  let val syms = Symbol.explode str
64526
01920e390645 embedded latex has 0 length -- imitating \<^raw> before aa1fe1103ab8;
wenzelm
parents: 63936
diff changeset
   263
  in (output_symbols syms, length_symbols syms) end;
19265
cae36e16f3c0 Output.add_mode: keyword component;
wenzelm
parents: 17218
diff changeset
   264
69346
3c29edccf739 expose latex mode operations, to facilitate adhoc changes to it;
wenzelm
parents: 67462
diff changeset
   265
fun latex_markup (s, _: Properties.T) =
59123
e68e44836d04 imitate command markup and rendering of Isabelle/jEdit in HTML output;
wenzelm
parents: 59081
diff changeset
   266
  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
   267
  then ("\\isacommand{", "}")
e68e44836d04 imitate command markup and rendering of Isabelle/jEdit in HTML output;
wenzelm
parents: 59081
diff changeset
   268
  else if s = Markup.keyword2N
e68e44836d04 imitate command markup and rendering of Isabelle/jEdit in HTML output;
wenzelm
parents: 59081
diff changeset
   269
  then ("\\isakeyword{", "}")
29325
a205acc94356 Markup.no_output;
wenzelm
parents: 28375
diff changeset
   270
  else Markup.no_output;
10955
36741b4fe109 setuo indent: \isaindent;
wenzelm
parents: 10393
diff changeset
   271
23621
e070a6ab1891 simplified pretty token metric: type int;
wenzelm
parents: 22648
diff changeset
   272
fun latex_indent "" _ = ""
e070a6ab1891 simplified pretty token metric: type int;
wenzelm
parents: 22648
diff changeset
   273
  | latex_indent s _ = enclose "\\isaindent{" "}" s;
e070a6ab1891 simplified pretty token metric: type int;
wenzelm
parents: 22648
diff changeset
   274
67428
dd8e40f46962 clarified signature;
wenzelm
parents: 67358
diff changeset
   275
val _ = Output.add_mode latexN latex_output (prefix Symbol.latex o cartouche);
23703
1b6a2c119151 Markup.add_mode;
wenzelm
parents: 23621
diff changeset
   276
val _ = Markup.add_mode latexN latex_markup;
1b6a2c119151 Markup.add_mode;
wenzelm
parents: 23621
diff changeset
   277
val _ = Pretty.add_mode latexN latex_indent;
8460
274426d1adbc tuned comments;
wenzelm
parents: 8192
diff changeset
   278
7726
2c7fc0ba1e12 Simple LaTeX presentation primitives (based on outer lexical syntax).
wenzelm
parents:
diff changeset
   279
end;