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