I am attempting to send text from a WSTP C++
backend to the Mathematica kernel which should be parsed and rendered as boxes.
The goal is for the user to see the outputs of things like Echo
as:
Frustratingly, when text like this is sent as a packet through WSTP, it does not render, and hides anything else in the string:
I construct the string representation of the boxes in C++
through raw string literals (having also tried explicit character escaping):
std::string x = R"delimiter(\!\(\*SubscriptBox[A,0]\))delimiter"
which, by writing to file, I have confirmed produces precisely the input to the Echo
in my first example.
Yet when I send Echo[x]
to the kernel through
WSPutFunction(stdlink, "EvaluatePacket", 1);
WSPutFunction(stdlink, "Echo", 1);
WSPutString(stdlink, x.c_str());
WSEndPacket(stdlink);
it doesn't render. What am I doing wrong?
Note that I must embed the boxes into larger strings. Ergo something like this workaround, letting the kernel generate the box strings, is not acceptable:
std::string expr = "Subscript[A,0]";
WSPutFunction(stdlink, "EvaluatePacket", 1);
WSPutFunction(stdlink, "Echo", 1);
// send StringJoin[..., ToString @ StandardForm @ ToExpression @ expr]
WSPutFunction(stdlink, "StringJoin", 2);
WSPutString(stdlink, "message prefix or something: ");
WSPutFunction(stdlink, "ToString", 1);
WSPutFunction(stdlink, "StandardForm", 1);
WSPutFunction(stdlink, "ToExpression", 1);
WSPutString(stdlink, expr.c_str());