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