From b0474491d74a47ab675ed0cc4d67bd3e82889ec2 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Wed, 23 Mar 2022 16:09:16 +0100 Subject: [PATCH] add align, align*, and equation support --- lib/htmlgen.ml | 2 +- lib/mathgen.ml | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ test.txt | 5 ++++- 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 lib/mathgen.ml diff --git a/lib/htmlgen.ml b/lib/htmlgen.ml index e24963a..e1bd4d2 100644 --- a/lib/htmlgen.ml +++ b/lib/htmlgen.ml @@ -202,7 +202,7 @@ let prepare_body name str toc = in read_preamble p; let doc = separate_sections doc in let doc = calculate_environments doc - in + in let doc = Mathgen.re_calculate_env doc in (match (Hashtbl.find_opt preamble "glossary") with | Some s -> init_glossary s | None -> ();); diff --git a/lib/mathgen.ml b/lib/mathgen.ml new file mode 100644 index 0000000..4c86eb4 --- /dev/null +++ b/lib/mathgen.ml @@ -0,0 +1,51 @@ +open Parser + +let generate_latex_command s l = + let line = "\\"^s in + let args = String.concat "," l in + let line = if args="" then line else Printf.sprintf "%s[%s]" line args in + line;; + + + +let generate_latex l = + let rec unparse acc l = + match l with + | [] -> String.concat " " acc + | Line s::q -> unparse (s::acc) q + | AtomicCmd (s,l)::q -> + let line = generate_latex_command s l in + unparse (line::acc) q + | OneArgCmd (s,l,l2)::q -> + let line = generate_latex_command s l in + let line = Printf.sprintf "%s{%s}" line (unparse [] l2) in + unparse (line::acc) q + | MultipleArgCmd (s,l,l2)::q -> + let line = generate_latex_command s l in + let l = List.map (unparse []) l2 in + let line = Printf.sprintf "%s{%s}" line (String.concat "\n" l) in + unparse (line::acc) q + | _::q -> unparse acc q + in unparse [] l;; + +let env_de_latexer env = + match env with + | e -> e;; + +let re_calculate_env ast = + let rec aux acc ast = + match ast with + | [] -> acc + | Env (s,n)::q when s="align" + -> aux (Math(Printf.sprintf "\\begin{align}%s\\end{align}" (generate_latex n))::acc) q + | Env (s,n)::q when s="align*" + -> aux (Math(Printf.sprintf "\\begin{align*}%s\\end{align*}" (generate_latex n))::acc) q + | Env (s,n)::q when s="equation" + -> aux (Math(Printf.sprintf "\\begin{equation}%s\\end{equation}" (generate_latex n))::acc) q + | Env(s,n)::q + -> let ast = aux [] n in + let ast = List.rev ast + in let env = Env(s,ast) + in aux (env::acc) q + | e::q -> aux (e::acc) q + in List.rev (aux [] ast);; \ No newline at end of file diff --git a/test.txt b/test.txt index 2ad6311..24377ac 100644 --- a/test.txt +++ b/test.txt @@ -5,7 +5,10 @@ Hello tralalala \bigskip This is an integral -$\int^a_b f(x) \mathrm{d}x$ +\begin{align*} +2x &= 4 \\ +3y &= 3 +\end{align*} \begin{center} au center \end{center}