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