From 4464417ec9ab3df071728295c9c29c7eede20055 Mon Sep 17 00:00:00 2001 From: Nicolas Chataing Date: Fri, 15 May 2020 11:27:38 +0200 Subject: [PATCH] Pretty print math symbols in html and latex outputs --- src/catala/literate/html.ml | 12 +++++++++++- src/catala/literate/latex.ml | 21 +++++++++++++++++---- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/catala/literate/html.ml b/src/catala/literate/html.ml index 0661f480..cdb297d2 100644 --- a/src/catala/literate/html.ml +++ b/src/catala/literate/html.ml @@ -153,11 +153,21 @@ let program_item_to_html (i : A.program_item) (custom_pygments : string option) | _ -> "#" ) (pre_html (Pos.unmark a.law_article_name)) | A.CodeBlock (_, c) | A.MetadataBlock (_, c) -> + let syms = R.regexp "!=|<=|>=|--|->" in + let syms_subst = function + | "!=" -> "≠" + | "<=" -> "≤" + | ">=" -> "≥" + | "--" -> "—" + | "->" -> "→" + | s -> s + in + let pprinted_c = R.substitute ~rex:syms ~subst:syms_subst (Pos.unmark c) in let formatted_original_code = P.sprintf "
\n
%s
\n%s\n
" (Pos.get_file (Pos.get_position c)) (pygmentize_code - (Pos.same_pos_as ("/*" ^ Pos.unmark c ^ "*/") c) + (Pos.same_pos_as ("/*" ^ pprinted_c ^ "*/") c) language custom_pygments) in formatted_original_code diff --git a/src/catala/literate/latex.ml b/src/catala/literate/latex.ml index b9e857b2..34555352 100644 --- a/src/catala/literate/latex.ml +++ b/src/catala/literate/latex.ml @@ -98,6 +98,18 @@ let wrap_latex (code : string) (source_files : string list) (custom_pygments : s source_files)) code +let math_syms_replace (c : string) : string = + let syms = R.regexp "!=|<=|>=|--|->" in + let syms2cmd = function + | "!=" -> "$\\neq$" + | "<=" -> "$\\leq$" + | ">=" -> "$\\geq$" + | "--" -> "\\textemdash" + | "->" -> "$\\rightarrow$" + | s -> s + in + R.substitute ~rex:syms ~subst:(fun s -> "|" ^ syms2cmd s ^ "|") c + let program_item_to_latex (i : A.program_item) (language : C.language_option) : string = match i with | A.LawHeading (title, precedence) -> @@ -108,26 +120,27 @@ let program_item_to_latex (i : A.program_item) (language : C.language_option) : | A.LawArticle a -> P.sprintf "\\paragraph{%s}" (pre_latexify (Pos.unmark a.law_article_name)) | A.CodeBlock (_, c) -> P.sprintf - "\\begin{minted}[label={\\hspace*{\\fill}\\texttt{%s}},firstnumber=%d]{%s}\n\ + "\\begin{minted}[escapeinside=||,mathescape=true,label={\\hspace*{\\fill}\\texttt{%s}},firstnumber=%d]{%s}\n\ /*%s*/\n\ \\end{minted}" (pre_latexify (Filename.basename (Pos.get_file (Pos.get_position c)))) (Pos.get_start_line (Pos.get_position c) + 1) (match language with C.Fr -> "catala_fr" | C.En -> "catala_en") - (Pos.unmark c) + (math_syms_replace (Pos.unmark c)) | A.MetadataBlock (_, c) -> P.sprintf "\\begin{tcolorbox}[colframe=OliveGreen, breakable, \ title=\\textcolor{black}{\\texttt{Métadonnées}},title after \ break=\\textcolor{black}{\\texttt{Métadonnées}},before skip=1em, after skip=1em]\n\ - \\begin{minted}[numbersep=9mm, firstnumber=%d, label={\\hspace*{\\fill}\\texttt{%s}}]{%s}\n\ + \\begin{minted}[escapeinside=||,mathescape=true,numbersep=9mm, firstnumber=%d, \ + label={\\hspace*{\\fill}\\texttt{%s}}]{%s}\n\ /*%s*/\n\ \\end{minted}\n\ \\end{tcolorbox}" (Pos.get_start_line (Pos.get_position c) + 1) (pre_latexify (Filename.basename (Pos.get_file (Pos.get_position c)))) (match language with C.Fr -> "catala_fr" | C.En -> "catala_en") - (Pos.unmark c) + (math_syms_replace (Pos.unmark c)) | A.LawInclude (A.PdfFile ((file, _), page)) -> let label = file ^ match page with None -> "" | Some p -> P.sprintf "_page_%d," p in P.sprintf