Skip to content

Commit

Permalink
First attempt at fixing latex scientific notation
Browse files Browse the repository at this point in the history
Ref: #31
  • Loading branch information
rapgenic committed May 14, 2021
1 parent d647a38 commit 04029be
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/xoctave/display.cpp
Expand Up @@ -25,9 +25,10 @@
#include <octave/symtab.h>

#include <nlohmann/json.hpp>
#include <regex>

#include "xoctave_interpreter.hpp"
#include "xeus/xinterpreter.hpp"
#include "xoctave_interpreter.hpp"

namespace xoctave::display {

Expand Down Expand Up @@ -113,6 +114,22 @@ octave_value_list matrix_to_html(const octave_value_list& args, int /*nargout*/)
return ovl(t.str());
}

std::string latex_fix_sci_not(std::string text) {
text = std::regex_replace(text, std::regex("e\\+[0]*([0-9]+)"), "\\mathrm{ᴇ}{$1}");
text = std::regex_replace(text, std::regex("e\\-[0]*([0-9]+)"), "\\mathrm{ᴇ\\text{-}}{$1}");

return text;
}

octave_value_list latex_fix_sci_not(const octave_value_list& args, int /*nargout*/) {
if (args.length() != 1)
print_usage();

std::string text = args(0).xstring_value("IN must be a string");

return ovl(latex_fix_sci_not(text));
}

octave_value_list matrix_to_latex(const octave_value_list& args, int /*nargout*/) {
std::ostringstream l;

Expand Down Expand Up @@ -148,23 +165,25 @@ octave_value_list matrix_to_latex(const octave_value_list& args, int /*nargout*/

l << "$$";

return ovl(l.str());
return ovl(latex_fix_sci_not(l.str()));
}

} // namespace

void register_all(octave::interpreter& i) {
auto &s = i.get_symbol_table();
auto& s = i.get_symbol_table();

auto display_data_func = new octave_builtin(display_data, "display_data", __FILE__, "");
auto override_path_func = new octave_builtin(override_path, "XOCTAVE_OVERRIDE_PATH", __FILE__, "");
auto matrix_to_html_func = new octave_builtin(matrix_to_html, "__matrix_to_html__", __FILE__, "");
auto matrix_to_latex_func = new octave_builtin(matrix_to_latex, "__matrix_to_latex__", __FILE__, "");
auto latex_fix_sci_not_func = new octave_builtin(latex_fix_sci_not, "__latex_fix_sci_not__", __FILE__, "");

s.install_built_in_function("display_data", display_data_func);
s.install_built_in_function("XOCTAVE_OVERRIDE_PATH", override_path_func);
s.install_built_in_function("__matrix_to_html__", matrix_to_html_func);
s.install_built_in_function("__matrix_to_latex__", matrix_to_latex_func);
s.install_built_in_function("__latex_fix_sci_not__", latex_fix_sci_not_func);
}

} // namespace xoctave::display

0 comments on commit 04029be

Please sign in to comment.