src/Pure/Thy/latex.ML
author wenzelm
Sun Jan 21 19:55:25 2001 +0100 (2001-01-21)
changeset 10955 36741b4fe109
parent 10393 b2a212304fb4
child 11012 8eb472444705
permissions -rw-r--r--
setuo indent: \isaindent;
     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 strip_blanks s =
    96   implode (#1 (Library.take_suffix Symbol.is_blank
    97     (#2 (Library.take_prefix Symbol.is_blank (explode s)))));
    98 
    99 fun output_tok (Basic tok, _) =
   100       let val s = T.val_of tok in
   101         if invisible_token tok then ""
   102         else if T.is_kind T.Command tok then
   103           "\\isacommand{" ^ output_syms s ^ "}"
   104         else if T.is_kind T.Keyword tok andalso Syntax.is_identifier s then
   105           "\\isakeyword{" ^ output_syms s ^ "}"
   106         else if T.is_kind T.String tok then output_syms (quote s)
   107         else if T.is_kind T.Verbatim tok then output_syms (enclose "{*" "*}" s)
   108         else output_syms s
   109       end
   110   | output_tok (Markup cmd, txt) = "%\n\\isamarkup" ^ cmd ^ "{" ^ strip_blanks txt ^ "%\n}\n"
   111   | output_tok (MarkupEnv cmd, txt) = "%\n\\begin{isamarkup" ^ cmd ^ "}%\n" ^
   112       strip_blanks txt ^ "%\n\\end{isamarkup" ^ cmd ^ "}%\n"
   113   | output_tok (Verbatim, txt) = "%\n" ^ strip_blanks txt ^ "\n";
   114 
   115 
   116 val output_tokens = implode o map output_tok;
   117 
   118 
   119 (* theory presentation *)
   120 
   121 val tex_trailer =
   122   "%%% Local Variables:\n\
   123   \%%% mode: latex\n\
   124   \%%% TeX-master: \"root\"\n\
   125   \%%% End:\n";
   126 
   127 fun isabelle_file name txt =
   128   "%\n\\begin{isabellebody}%\n\
   129   \\\def\\isabellecontext{" ^ output_syms name ^ "}%\n" ^ txt ^
   130   "\\end{isabellebody}%\n" ^ tex_trailer;
   131 
   132 fun old_symbol_source name syms =
   133   isabelle_file name ("\\isamarkupheader{" ^ output_syms name ^ "}%\n" ^ output_symbols syms);
   134 
   135 fun theory_entry name = "\\input{" ^ name ^ ".tex}\n\n";
   136 
   137 
   138 (* print mode *)
   139 
   140 val latexN = "latex";
   141 val modes = [latexN, Symbol.xsymbolsN, Symbol.symbolsN];
   142 
   143 fun latex_output str =
   144   let val syms = Symbol.explode str
   145   in (output_symbols syms, real (Symbol.length syms)) end;
   146 
   147 fun latex_indent "" = ""
   148   | latex_indent s = enclose "\\isaindent{" "}" s;
   149 
   150 val token_translation =
   151   map (fn s => (latexN, s, latex_output)) Syntax.standard_token_classes;
   152 
   153 val _ = Symbol.add_mode latexN (latex_output, latex_indent o #1);
   154 val setup = [Theory.add_tokentrfuns token_translation];
   155 
   156 
   157 end;