Skip to content

Commit

Permalink
escaping algebra names
Browse files Browse the repository at this point in the history
  • Loading branch information
sjanssen2 committed Dec 22, 2023
1 parent 6291b21 commit ec18edc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
38 changes: 23 additions & 15 deletions rtlib/rope.hh
Original file line number Diff line number Diff line change
Expand Up @@ -756,25 +756,33 @@ inline void append(rope::Ref<X> &str, const T &x) {
str.append(x);
}

template<typename X>
inline void append_latex(rope::Ref<X> &str, char &c) {
std::string s(1, c);
/* accepts a string and escapes all containing characters to be compatible
* to LaTeX math mode. Used for tikZ generations.
*/
inline std::string latex(const std::string in) {
std::string out = std::string(in);

/* https://www.cespedes.org/blog/85/how-to-escape-latex-special-characters
* note that we are in math mode, which might use different rules than
* LaTeX's "normal" text mode. */
boost::replace_all(s, "\\", "\\backslash");
boost::replace_all(s, "#", "\\#");
boost::replace_all(s, "$", "\\$");
boost::replace_all(s, "%", "\\%");
boost::replace_all(s, "&", "\\&");
boost::replace_all(s, "_", "\\_");
boost::replace_all(s, "{", "\\{");
boost::replace_all(s, "}", "\\}");
boost::replace_all(s, "^", "\\hat{\\ }");
boost::replace_all(s, "~", "\\tilde{\\ }");

str.append(s.c_str(), s.size());
boost::replace_all(out, "\\", "\\backslash");
boost::replace_all(out, "#", "\\#");
boost::replace_all(out, "$", "\\$");
boost::replace_all(out, "%", "\\%");
boost::replace_all(out, "&", "\\&");
boost::replace_all(out, "_", "\\_");
boost::replace_all(out, "{", "\\{");
boost::replace_all(out, "}", "\\}");
boost::replace_all(out, "^", "\\hat{\\ }");
boost::replace_all(out, "~", "\\tilde{\\ }");

return out;
}
template<typename X>
inline void append_latex(rope::Ref<X> &str, char &c) {
std::string in(1, c);
std::string out = latex(in);
str.append(out.c_str(), out.size());
}
template<typename X>
inline void append_latex(rope::Ref<X> &str, const Subsequence &s) {
Expand Down
4 changes: 2 additions & 2 deletions src/cpp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2586,8 +2586,8 @@ void Printer::Cpp::print_tikz_singleAlgebraValue(Product::Base *product,
for (Product::iterator a = Product::begin(wrk_product);
a != Product::end(); ++a) {
if (((*a)->is(Product::SINGLE)) && (!(*a)->uses_tikz())) {
stream << indent() << "out << \""
<< *(*a)->algebra()->name << " & \\\\ \" << ";
stream << indent() << "out << latex(\""
<< *(*a)->algebra()->name << "\") << \" & \\\\ \" << ";
stream << candidate << *wrk_product->get_component_accessor(
*(*a)->algebra());
stream << " << \" \\\\\\\\ \";" << endl;
Expand Down

0 comments on commit ec18edc

Please sign in to comment.