src/Pure/Thy/latex.ML
author berghofe
Wed Oct 10 18:41:13 2001 +0200 (2001-10-10)
changeset 11719 49c14348a42b
parent 11012 8eb472444705
child 11860 36ba0d4a836c
permissions -rw-r--r--
Exported output_symbols.
     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   val output_symbols: Symbol.symbol list -> string
    12   datatype token = Basic of OuterLex.token | Markup of string | MarkupEnv of string | Verbatim
    13   val output_tokens: (token * string) list -> string
    14   val tex_trailer: string
    15   val isabelle_file: string -> string -> string
    16   val old_symbol_source: string -> Symbol.symbol list -> string
    17   val theory_entry: string -> string
    18   val modes: string list
    19   val setup: (theory -> theory) list
    20 end;
    21 
    22 structure Latex: LATEX =
    23 struct
    24 
    25 
    26 (* symbol output *)
    27 
    28 local
    29 
    30 val output_chr = fn
    31   " " => "\\ " |
    32   "\n" => "\\isanewline\n" |
    33   "!" => "{\\isacharbang}" |
    34   "\"" => "{\\isachardoublequote}" |
    35   "#" => "{\\isacharhash}" |
    36   "$" => "{\\isachardollar}" |
    37   "%" => "{\\isacharpercent}" |
    38   "&" => "{\\isacharampersand}" |
    39   "'" => "{\\isacharprime}" |
    40   "(" => "{\\isacharparenleft}" |
    41   ")" => "{\\isacharparenright}" |
    42   "*" => "{\\isacharasterisk}" |
    43   "+" => "{\\isacharplus}" |
    44   "," => "{\\isacharcomma}" |
    45   "-" => "{\\isacharminus}" |
    46   "." => "{\\isachardot}" |
    47   "/" => "{\\isacharslash}" |
    48   ":" => "{\\isacharcolon}" |
    49   ";" => "{\\isacharsemicolon}" |
    50   "<" => "{\\isacharless}" |
    51   "=" => "{\\isacharequal}" |
    52   ">" => "{\\isachargreater}" |
    53   "?" => "{\\isacharquery}" |
    54   "@" => "{\\isacharat}" |
    55   "[" => "{\\isacharbrackleft}" |
    56   "\\" => "{\\isacharbackslash}" |
    57   "]" => "{\\isacharbrackright}" |
    58   "^" => "{\\isacharcircum}" |
    59   "_" => "{\\isacharunderscore}" |
    60   "`" => "{\\isacharbackquote}" |
    61   "{" => "{\\isacharbraceleft}" |
    62   "|" => "{\\isacharbar}" |
    63   "}" => "{\\isacharbraceright}" |
    64   "~" => "{\\isachartilde}" |
    65   c => if Symbol.is_digit c then enclose "{\\isadigit{" "}}" c else c;
    66 
    67 fun output_sym s =
    68   if size s = 1 then output_chr s
    69   else
    70     (case explode s of
    71       "\\" :: "<" :: "^" :: cs => "\\isactrl" ^ implode (#1 (Library.split_last cs)) ^ " "
    72     | "\\" :: "<" :: cs => "{\\isasym" ^ implode (#1 (Library.split_last cs)) ^ "}"
    73     | _ => sys_error "output_sym");
    74 
    75 in
    76 
    77 val output_symbols = implode o map output_sym;
    78 val output_syms = output_symbols o Symbol.explode;
    79 
    80 end;
    81 
    82 
    83 (* token output *)
    84 
    85 structure T = OuterLex;
    86 
    87 datatype token =
    88   Basic of T.token |
    89   Markup of string |
    90   MarkupEnv of string |
    91   Verbatim;
    92 
    93 
    94 val invisible_token = T.keyword_with (equal ";") orf T.is_kind T.Comment;
    95 
    96 fun output_tok (Basic tok, _) =
    97       let val s = T.val_of tok in
    98         if invisible_token tok then ""
    99         else if T.is_kind T.Command tok then
   100           "\\isacommand{" ^ output_syms s ^ "}"
   101         else if T.is_kind T.Keyword tok andalso Syntax.is_identifier s then
   102           "\\isakeyword{" ^ output_syms s ^ "}"
   103         else if T.is_kind T.String tok then output_syms (quote s)
   104         else if T.is_kind T.Verbatim tok then output_syms (enclose "{*" "*}" s)
   105         else output_syms s
   106       end
   107   | output_tok (Markup cmd, txt) =
   108       "%\n\\isamarkup" ^ cmd ^ "{" ^ Symbol.strip_blanks txt ^ "%\n}\n"
   109   | output_tok (MarkupEnv cmd, txt) = "%\n\\begin{isamarkup" ^ cmd ^ "}%\n" ^
   110       Symbol.strip_blanks txt ^ "%\n\\end{isamarkup" ^ cmd ^ "}%\n"
   111   | output_tok (Verbatim, txt) = "%\n" ^ Symbol.strip_blanks txt ^ "\n";
   112 
   113 
   114 val output_tokens = implode o map output_tok;
   115 
   116 
   117 (* theory presentation *)
   118 
   119 val tex_trailer =
   120   "%%% Local Variables:\n\
   121   \%%% mode: latex\n\
   122   \%%% TeX-master: \"root\"\n\
   123   \%%% End:\n";
   124 
   125 fun isabelle_file name txt =
   126   "%\n\\begin{isabellebody}%\n\
   127   \\\def\\isabellecontext{" ^ output_syms name ^ "}%\n" ^ txt ^
   128   "\\end{isabellebody}%\n" ^ tex_trailer;
   129 
   130 fun old_symbol_source name syms =
   131   isabelle_file name ("\\isamarkupheader{" ^ output_syms name ^ "}%\n" ^ output_symbols syms);
   132 
   133 fun theory_entry name = "\\input{" ^ name ^ ".tex}\n\n";
   134 
   135 
   136 (* print mode *)
   137 
   138 val latexN = "latex";
   139 val modes = [latexN, Symbol.xsymbolsN, Symbol.symbolsN];
   140 
   141 fun latex_output str =
   142   let val syms = Symbol.explode str
   143   in (output_symbols syms, real (Symbol.length syms)) end;
   144 
   145 fun latex_indent "" = ""
   146   | latex_indent s = enclose "\\isaindent{" "}" s;
   147 
   148 val token_translation =
   149   map (fn s => (latexN, s, latex_output)) Syntax.standard_token_classes;
   150 
   151 val _ = Symbol.add_mode latexN (latex_output, latex_indent o #1);
   152 val setup = [Theory.add_tokentrfuns token_translation];
   153 
   154 
   155 end;