How to convert formal symbols to standard ones in an expression?

Mathematica generates an expression sometimes using \[Formal] symbols. This is causing a problem with the TeXForm code generated as $\LaTeX$ is not able to process it. To clarify: The generated TeXForm should not include Unicode. The problem is that these are causing problem. So the solution should be such that, the result of applying TeXForm to the expression, should not produce Unicode.

Is there a way to make these expressions use normal symbols (normal x and y) so that I can generate $\LaTeX$ code from them that will work in my $\LaTeX$ document

 f = DifferentialRoot[Function[{y, x}, {y''[x] + y[x] == 0, y[0] == 0, y'[0] == 1}]]


If I do TeXForm[f] on the above, the $\LaTeX$ code generated is not valid with TeXLive. I just need the expression f not to contain those dots above and below the symbols. Just normal symbols. One way is to use

f = DifferentialRoot[Function[{y, x}, {y''[x] + y[x] == 0, y[0] == 0, y'[0] == 1}]]
// OutputForm


which $\LaTeX$ can handle after TeXForm is applied to the above. But it will be nice to have x and y there instead.

-

1 Answer

Interesting question with more complications than I anticipated.

If it is not necessary to replace every formal Symbol with the equivalent plain version, but instead arbitrarily with specified Symbols, you can use something like this:

SetAttributes[formalQ, HoldFirst]
formalQ[s_Symbol] := StringMatchQ[ToString@HoldForm@FullForm@s, "\\[Formal*]"]

expr = DifferentialRoot[Function[{y, x}, {y''[x] + y[x] == 0, y[0] == 0, y'[0] == 1}]];

rep = {a, b, c, d, e};  (* new Symbols to use *)

old = Union @ Cases[expr, _Symbol?formalQ, {-1}]
new = Take[rep, Length @ old]
HoldForm @@ {expr} /. Thread[old -> new]

DifferentialRoot[Function[{b,a},{b[a]+(b^\[Prime]\[Prime])[a]==0,b[0]==0,(b^\[Prime])[0]==1}]]


Note that I had to wrap expr in HoldForm as otherwise it automatically puts the Formal Symbols right back!

I am still working on the larger problem of replacing every Formal Symbol with its plain equivalent.
If I cannot find an internal look-up table I may have to build my own.

If you are only dealing with automatically-generated Formal Symbols you may only have a few to replace:

Holonomic$DifferenceRootFormalSymbols Holonomic$DifferentialRootFormalSymbols

{\[FormalY], \[FormalN]}

{\[FormalY], \[FormalX]}


Therefore I would start with:

fix = {\[FormalY] :> y, \[FormalN] :> n, \[FormalX] :> x}

-
The generated code (from TeXForm) still contain those trouble invalid code) which are the \unicode. When I do TeXForm[%] on the result from your code, it still shows up with invlaid latex code that uses \unicode. The main reason I wanted to remove those \Formal and replace them with normal symbols is to avoid this unicode being generated. I'll update the question to make it more clear. You have removed all the other unicode, good, but one is still there. !Mathematica graphics – Nasser Jul 22 '14 at 9:36
@Nasser My mistake: that is \[Function]. Please try: HoldForm @@ {expr} /. Thread[old -> new] // StandardForm // TeXForm – Mr.Wizard Jul 22 '14 at 9:40
Thanks, yes, that is good. no unicode. It is not critical that x and y are being replaced by a and b. (x,y just happened to be more common to use) but will live with this solution. thank you – Nasser Jul 22 '14 at 9:44
@Nasser Are all the formal symbols you're dealing with automatically generated? If that's the case there may be only a few to replace, i.e. y, n, x. – Mr.Wizard Jul 22 '14 at 9:47
yes. These are solutions from a set of differential equations that I am processing. So it will really include x and y as these are the same symbols used for each equation. It is just that M sometimes returns solutions using \Formal which causes problem for Latex. I am not using the solution for anything other than converting it to Latex. – Nasser Jul 22 '14 at 9:50