This works but I suspect you'll not like it... Should I combine it with escape(string const &) ?
Angus string const latexify(string const str) { ostringstream out; string::const_iterator it = str.begin(); string::const_iterator end = str.end(); for (; it != end; ++it) { switch (*it) { case ('$'): case ('&'): case ('%'): case ('#'): case ('_'): case ('{'): case ('}'): out << '\\' << *it; break; case ('~'): case ('^'): out << '\\' << *it << "{}"; break; case ('\\'): out << "$\backslash$"; break; default: out << *it; break; } } return out.str().c_str(); }