Skip to content

Commit

Permalink
add align, align*, and equation support
Browse files Browse the repository at this point in the history
  • Loading branch information
vanilla-extracts committed Mar 23, 2022
1 parent 85f5295 commit b047449
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/htmlgen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> (););
Expand Down
51 changes: 51 additions & 0 deletions lib/mathgen.ml
Original file line number Diff line number Diff line change
@@ -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);;
5 changes: 4 additions & 1 deletion test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit b047449

Please sign in to comment.