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