# 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, y' == 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, y' == 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.

• An undocumented way of doing it (which I learned from this answer) is: MapThread[Set, {Table[SystemConvertTeXFormDumpmaketex[FromCharacterCode[63488 + i]], {i, 0, 25 + 26}], Table[FromCharacterCode[97 + i], {i, 0, 25}]~Join~Table[FromCharacterCode[65 + i], {i, 0, 25}]}]; It seems to work from v8.0.4 all the way through 11.0.0. Sep 22, 2016 at 1:49

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, y' == 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,(b^\[Prime])==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]}


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 Jul 22, 2014 at 9:36
• @Nasser My mistake: that is \[Function]. Please try: HoldForm @@ {expr} /. Thread[old -> new] // StandardForm // TeXForm Jul 22, 2014 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 Jul 22, 2014 at 9:44
• 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. Jul 22, 2014 at 9:50