Skip to content

Commit

Permalink
move latex escaping into rope.hh
Browse files Browse the repository at this point in the history
  • Loading branch information
sjanssen2 committed Dec 22, 2023
1 parent 0731a21 commit 6291b21
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
7 changes: 7 additions & 0 deletions rtlib/rope.hh
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,13 @@ inline void append_latex(rope::Ref<X> &str, const Subsequence &s) {
append_latex(str, ichar);
}
}
template<typename X>
inline void append_latex(rope::Ref<X> &str, const char *s, int slen) {
for (int i = 0; i < slen; ++i) {
char ichar = (s[i]);
append_latex(str, ichar);
}
}

template<class T, typename X>
inline void append(rope::Ref<X> &str, char c, T i) {
Expand Down
27 changes: 15 additions & 12 deletions src/signature.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include <utility>
#include <list>
#include <vector>
#include <boost/algorithm/string/replace.hpp>

#include "signature.hh"
#include "input.hh"
Expand Down Expand Up @@ -587,17 +586,21 @@ struct Generate_TikZ_Stmts : public Generate_Stmts {
f->add_arg(*ret);
std::string *color = new std::string(COLOR_ALGFCT);

// escape potential _ characters in algebra function names for LaTeX
std::string fn_name = *fn.name;
boost::replace_all(fn_name, "_", "\\\\_");
// we need to substract the number of escaped \ when computing Rope length
std::string::difference_type n = std::count(
fn.name->begin(), fn.name->end(), '_');
std::string t = "node {\\\\color[HTML]{" + \
color->substr(1, color->size()-1) + "} " + fn_name + "} ";
f->add_arg(new Expr::Const(t));
f->add_arg(new Expr::Const(
static_cast<int>(t.size()) - 1 - static_cast<int>(n)));
f->add_arg(new Expr::Const("node {\\\\color[HTML]{" + \
color->substr(1, color->size()-1) + "} "));
f->add_arg(new Expr::Const(27));
fn.stmts.push_back(f);

f = new Statement::Fn_Call("append_latex");
f->add_arg(*ret);
f->add_arg(new Expr::Const(*fn.name));
f->add_arg(new Expr::Const(static_cast<int>(fn.name->size())));
fn.stmts.push_back(f);

f = new Statement::Fn_Call(Statement::Fn_Call::STR_APPEND);
f->add_arg(*ret);
f->add_arg(new Expr::Const("} "));
f->add_arg(new Expr::Const(2));
fn.stmts.push_back(f);

Statement::Var_Decl *cur = ret;
Expand Down

0 comments on commit 6291b21

Please sign in to comment.