src/Pure/Thy/latex.ML
author wenzelm
Sat Jun 05 13:07:31 2004 +0200 (2004-06-05)
changeset 14874 23c73484312f
parent 14840 dc23ff2629e2
child 14879 8989eedf72a1
permissions -rw-r--r--
Symbol.decode;
     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 flag_markup: bool -> string
    15   val tex_trailer: string
    16   val isabelle_file: string -> string -> string
    17   val old_symbol_source: string -> Symbol.symbol list -> string
    18   val theory_entry: string -> string
    19   val modes: string list
    20   val setup: (theory -> theory) list
    21 end;
    22 
    23 structure Latex: LATEX =
    24 struct
    25 
    26 
    27 (* symbol output *)
    28 
    29 local
    30 
    31 val output_chr = fn
    32   " " => "\\ " |
    33   "\n" => "\\isanewline\n" |
    34   "!" => "{\\isacharbang}" |
    35   "\"" => "{\\isachardoublequote}" |
    36   "#" => "{\\isacharhash}" |
    37   "$" => "{\\isachardollar}" |
    38   "%" => "{\\isacharpercent}" |
    39   "&" => "{\\isacharampersand}" |
    40   "'" => "{\\isacharprime}" |
    41   "(" => "{\\isacharparenleft}" |
    42   ")" => "{\\isacharparenright}" |
    43   "*" => "{\\isacharasterisk}" |
    44   "+" => "{\\isacharplus}" |
    45   "," => "{\\isacharcomma}" |
    46   "-" => "{\\isacharminus}" |
    47   "." => "{\\isachardot}" |
    48   "/" => "{\\isacharslash}" |
    49   ":" => "{\\isacharcolon}" |
    50   ";" => "{\\isacharsemicolon}" |
    51   "<" => "{\\isacharless}" |
    52   "=" => "{\\isacharequal}" |
    53   ">" => "{\\isachargreater}" |
    54   "?" => "{\\isacharquery}" |
    55   "@" => "{\\isacharat}" |
    56   "[" => "{\\isacharbrackleft}" |
    57   "\\" => "{\\isacharbackslash}" |
    58   "]" => "{\\isacharbrackright}" |
    59   "^" => "{\\isacharcircum}" |
    60   "_" => "{\\isacharunderscore}" |
    61   "`" => "{\\isacharbackquote}" |
    62   "{" => "{\\isacharbraceleft}" |
    63   "|" => "{\\isacharbar}" |
    64   "}" => "{\\isacharbraceright}" |
    65   "~" => "{\\isachartilde}" |
    66   c => if Symbol.is_digit c then enclose "{\\isadigit{" "}}" c else c;
    67 
    68 fun output_sym sym =
    69   (case Symbol.decode sym of
    70     Symbol.Char s => output_chr s
    71   | Symbol.Sym s => enclose "{\\isasym" "}" s
    72   | Symbol.Ctrl s => enclose "\\isactrl" " " s
    73   | Symbol.Raw s => s);
    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_ascii_identifier s then
   102           "\\isakeyword{" ^ output_syms s ^ "}"
   103         else if T.is_kind T.String tok then output_syms (Library.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 fun flag_markup true = "\\isamarkuptrue%\n"
   118   | flag_markup false = "\\isamarkupfalse%\n";
   119 
   120 
   121 (* theory presentation *)
   122 
   123 val tex_trailer =
   124   "%%% Local Variables:\n\
   125   \%%% mode: latex\n\
   126   \%%% TeX-master: \"root\"\n\
   127   \%%% End:\n";
   128 
   129 fun isabelle_file name txt =
   130   "%\n\\begin{isabellebody}%\n\
   131   \\\def\\isabellecontext{" ^ output_syms name ^ "}%\n" ^ txt ^
   132   "\\end{isabellebody}%\n" ^ tex_trailer;
   133 
   134 fun old_symbol_source name syms =
   135   isabelle_file name ("\\isamarkupheader{" ^ output_syms name ^ "}%\n" ^ output_symbols syms);
   136 
   137 fun theory_entry name = "\\input{" ^ name ^ ".tex}\n\n";
   138 
   139 
   140 (* print mode *)
   141 
   142 val latexN = "latex";
   143 val modes = [latexN, Symbol.xsymbolsN, Symbol.symbolsN];
   144 
   145 fun latex_output str =
   146   let val syms = Symbol.explode str
   147   in (output_symbols syms, real (Symbol.length syms)) end;
   148 
   149 fun latex_indent "" = ""
   150   | latex_indent s = enclose "\\isaindent{" "}" s;
   151 
   152 val token_translation =
   153   map (fn s => (latexN, s, latex_output)) Syntax.standard_token_classes;
   154 
   155 val _ = Output.add_mode latexN (latex_output, latex_indent o #1, Symbol.default_raw);
   156 val setup = [Theory.add_tokentrfuns token_translation];
   157 
   158 
   159 end;