The following is a minimal working example of a problem that I met when I was programmatically constructing codefiles. I use Mathematica under Windows.
I have 5 strings, all containing an arrow character that is entered in the usual way esc - > esc. Two of the strings contain a quotation mark as well.
teststrings = {"a→b", "a\"c→b", "a→b", "a\"c→b", "a→b" }
(* {a→b,a"c→b,a→b,a"c→b,a→b} *)
When printed as output in the notebook, all arrows turn up as a single character. Now I write these strings to a file:
file = OpenWrite[FileNameJoin[{NotebookDirectory[], "test.m"}]];
Do[ WriteLine[file, x], {x, teststrings}];
Close[file];
When I inspect the file, it looks like so:
a→b
a"c->b
a->b
a"c→b
a→b
After the first quotation mark has been written to the file, the following arrows are written incorrectly as two characters, until the second quotation mark has been written to the file. After that, the arrows appear to be correct again.
I fail to see an explanation for this behaviour and I fail to see a workaround. My setting of $CharacterEncoding
is WindowsANSI. Is there a better way of doing this?
Edit
When I open the file in a text editor such as Notepad++, I indeed see 5 double arrows. So now I realize that I should have been more explicit. As I wrote, I want to have a Mathematica code file, and I inspected the file with Mathematica itself. Thanks for all comments mentioning that the file itself does not contain any single character arrow, only -> for an arrow.
So the problem is not the writing of the file, but the reading of the .m-file by Mathematica. It seems that when Mathematica reads a .m-file, the two characters -> are replaced with → until an escaped quotation mark is read, and then does not replace until the next reading of an escaped quotation mark. Strange. Is this as intended?
->
in the file for all the arrows in 13.0.1 on Win 10. $\endgroup$AutoOperatorRenderings
. $\endgroup$