src/Pure/Thy/latex.ML
author wenzelm
Tue Jun 06 20:31:22 2000 +0200 (2000-06-06)
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);
     1 (*  Title:      Pure/Thy/latex.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4     License:    GPL (GNU GENERAL PUBLIC LICENSE)
     5 
     6 LaTeX presentation primitives (based on outer lexical syntax).
     7 *)
     8 
     9 signature LATEX =
    10 sig
    11   datatype token = Basic of OuterLex.token | Markup of string * string |
    12     MarkupEnv of string * string | Verbatim of string
    13   val old_symbol_source: string -> Symbol.symbol list -> string
    14   val token_source: token list -> string
    15   val theory_entry: string -> string
    16   val setup: (theory -> theory) list
    17 end;
    18 
    19 structure Latex: LATEX =
    20 struct
    21 
    22 
    23 (* symbol output *)
    24 
    25 local
    26 
    27 val output_chr = fn
    28   " " => "~" |
    29   "\n" => "\\isanewline\n" |
    30   "$" => "\\$" |
    31   "&" => "\\&" |
    32   "%" => "\\%" |
    33   "#" => "\\#" |
    34   "_" => "\\_" |
    35   "{" => "{\\isabraceleft}" |
    36   "}" => "{\\isabraceright}" |
    37   "~" => "{\\isatilde}" |
    38   "^" => "{\\isacircum}" |
    39   "\"" => "{\"}" |
    40   "\\" => "{\\isabackslash}" |
    41   c => c;
    42 
    43 fun output_sym s =
    44   if size s = 1 then output_chr s
    45   else
    46     (case explode s of
    47       cs as "\\" :: "<" :: "^" :: _ => implode (map output_chr cs)
    48     | "\\" :: "<" :: cs => "{\\isasym" ^ implode (#1 (Library.split_last cs)) ^ "}"
    49     | _ => sys_error "output_sym");
    50 
    51 in
    52 
    53 val output_symbols = implode o map output_sym;
    54 val output_syms = output_symbols o Symbol.explode;
    55 
    56 end;
    57 
    58 
    59 (* token output *)
    60 
    61 structure T = OuterLex;
    62 
    63 datatype token =
    64   Basic of T.token |
    65   Markup of string * string |
    66   MarkupEnv of string * string |
    67   Verbatim of string;
    68 
    69 
    70 val invisible_token = T.keyword_with (equal ";") orf T.is_kind T.Comment;
    71 
    72 fun strip_blanks s =
    73   implode (#1 (Library.take_suffix Symbol.is_blank
    74     (#2 (Library.take_prefix Symbol.is_blank (explode s)))));
    75 
    76 fun output_tok (Basic tok) =
    77       let val s = T.val_of tok in
    78         if invisible_token tok then ""
    79         else if T.is_kind T.Command tok then
    80           if s = "{" then "{\\isabeginblock}"
    81           else if s = "}" then "{\\isaendblock}"
    82           else "\\isacommand{" ^ output_syms s ^ "}"
    83         else if T.is_kind T.Keyword tok andalso Syntax.is_identifier s then
    84           "\\isakeyword{" ^ output_syms s ^ "}"
    85         else if T.is_kind T.String tok then output_syms (quote s)
    86         else if T.is_kind T.Verbatim tok then output_syms (enclose "{*" "*}" s)
    87         else output_syms s
    88       end
    89   | output_tok (Markup (cmd, txt)) = "%\n\\isamarkup" ^ cmd ^ "{" ^ strip_blanks txt ^ "}\n"
    90   | output_tok (MarkupEnv (cmd, txt)) = "%\n\\begin{isamarkup" ^ cmd ^ "}%\n" ^
    91       strip_blanks txt ^ "%\n\\end{isamarkup" ^ cmd ^ "}%\n"
    92   | output_tok (Verbatim txt) = "%\n" ^ strip_blanks txt ^ "\n";
    93 
    94 
    95 val output_tokens = implode o map output_tok;
    96 
    97 
    98 (* theory presentation *)
    99 
   100 val isabelle_env = enclose "\\begin{isabelle}%\n" "\\end{isabelle}%\n";
   101 
   102 fun old_symbol_source name syms =
   103   isabelle_env ("\\isamarkupheader{" ^ output_syms name ^ "}%\n" ^ output_symbols syms);
   104 
   105 val token_source = isabelle_env o output_tokens;
   106 
   107 fun theory_entry name = "\\input{" ^ name ^ ".tex}\n\n";
   108 
   109 
   110 (* print mode *)
   111 
   112 val latexN = "latex";
   113 
   114 fun latex_output str =
   115   let val syms = Symbol.explode str
   116   in (output_symbols syms, real (Symbol.length syms)) end;
   117 
   118 val token_translation = map (fn s => (latexN, s, latex_output)) Syntax.standard_token_classes;
   119 
   120 val _ = Symbol.add_mode latexN latex_output;
   121 val setup = [Theory.add_tokentrfuns token_translation];
   122 
   123 
   124 end;