Hello, Please note that attempting to change expressions in symtab vars you create new copies of them, which are not connected to symbols in the initial expression. To get the desired result you need to substitute original symbols in the expression with their alterations. Here is the code: int main(){ GiNaC::parser reader; GiNaC::ex e = reader("2*x_1+sin(y_2)"); GiNaC::symtab & vars = reader.get_syms(); std::cout << GiNaC::latex; exmap em; for (std::pair<std::string, GiNaC::ex> var : vars) { // loop through each symbol then texify and use "set_TeX_name()" em[var.second] = symbol(var.first, addTeXbrackets(var.first)); } //<- Not sure how to add latex to the output yet std::cout << e.subs(em) <<std::endl; // -> 2 x_{1}+\sin(y_{2}) } Best wishes, Vladimir -- Vladimir V. Kisil http://www1.maths.leeds.ac.uk/~kisilv/ Book: Geometry of Mobius Maps https://doi.org/10.1142/p835 Soft: Geometry of cycles http://moebinv.sourceforge.net/ Jupyter notebooks: https://github.com/vvkisil?tab=repositories
On Tue, 18 Apr 2023 10:38:48 -1000, Charles White <whitece6@hawaii.edu> said:
CW> Hi GiNaC! I love the library! Keep up the good work! CW> Currently though I am having trouble with LaTeX output; I would CW> like to parse an expression, then set the LaTeX name to the CW> symbols that were generated from the reader. I can do that, but CW> the expression doesn't change when I print it. Here is an CW> minimal example: CW> #include <ginac/ginac.h> std::string addTeXbrackets(std::string CW> const& in) { std::string ret = in; std::string::size_type pos = CW> 0; while((pos = ret.find('_', pos)) != std::string::npos) { CW> ret.replace(pos, 1, "_{"); pos += 1; // move past the dot (and CW> the extra '\n') ret.push_back('}'); } CW> return ret; } int main(){ GiNaC::parser reader; GiNaC::ex e = CW> reader("2*x_1+sin(y_2)"); GiNaC::symtab & vars = CW> reader.get_syms(); std::cout << GiNaC::latex; for CW> (std::pair<std::string, GiNaC::ex> var : vars) { // loop through CW> each symbol then texify and use "set_TeX_name()" CW> GiNaC::ex_to<GiNaC::symbol CW> &>(var.second).set_TeX_name(addTeXbrackets(var.first)); CW> std::cout<<var.second<<std::endl; } //<- Not sure how to add CW> latex to the output yet std::cout<<e<<std::endl; } //output is: CW> x_{1} y_{2} 2 x_1+\sin(y_2) CW> Thanks! Charles