3
$\begingroup$

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?

$\endgroup$
6
  • 1
    $\begingroup$ Which version are you using? I get -> in the file for all the arrows in 13.0.1 on Win 10. $\endgroup$
    – Edmund
    Commented Mar 17, 2022 at 13:24
  • 1
    $\begingroup$ I see this on Notepad++ and Notepad. [v12.2.0Win7-x64]. You have to zoom in. $\endgroup$
    – Syed
    Commented Mar 17, 2022 at 13:30
  • $\begingroup$ If you have Mathematica 12.3 or higher, read AutoOperatorRenderings. $\endgroup$
    – Ben Izd
    Commented Mar 17, 2022 at 13:31
  • $\begingroup$ @Edmund. Please see my edit. $\endgroup$ Commented Mar 17, 2022 at 13:59
  • $\begingroup$ @Syed. Please see my edit. $\endgroup$ Commented Mar 17, 2022 at 14:00

1 Answer 1

0
$\begingroup$

MMA version 13.0.1

If I write the string to a *.m file I get in Notepad++ the fullform of the arrow:

teststrings = {"a\[RightArrow]b", "a\"c\[RightArrow]b", 
   "a\[RightArrow]b", "a\"c\[RightArrow]b", "a\[RightArrow]b"};
Put[teststrings, "d:/tmp/test.m"]

enter image description here

Reading this back with MMA using Get produces the original string:

Get["d:/tmp/test.m"]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thanks for your interest in this problem. Unfortunately your solution is not really a solution for my problem, for you write the strings to the file including their quotationmarks and I need the strings without the quotation marks. I find your result when I would have used Write instead of WriteLine. $\endgroup$ Commented Mar 18, 2022 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.