7

A pet peeve of mine is the use of double square brackets for Part rather than the single character \[LeftDoubleBracket] and \[RightDoubleBracket]. I would like to have these automatically replaced when pasting plain-text code (from StackOverflow for example) into a Mathematica Notebook. I have been unable to configure this.

Can it be done with ImportAutoReplacements or another automatic method (preferred), or will I need use a method like the "Paste Tabular Data Palette" referenced here?

Either way, I am not good with string parsing, and I want to learn the best way to handle bracket counting.


Sjoerd gave Defer and Simon gave Ctrl+Shift+N which both cause Mathematica to auto-format code. These are fine options.

I am still interested in a method that is automatic and/or preserves as much of the original code as possible. For example, maintaining prefix f@1, infix 1 ~f~ 2, and postfix 1 // f functions in their original forms.


A subsection of this question was reposted as Matching brackets in a string and received several good answers.

Community
  • 1
  • 1
Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125

2 Answers2

2

Not really an answer, but a thread on entering the double [[ ]] pair (with the cursor between both pairs) using a single keystroke occurred a couple of weeks ago on the mathgroup. It didn't help me, but for others this was a solution apparently.

EDIT
to make good on my slightly off-topic first response here's a pattern replacement that seems to do the job (although I have difficulties myself to understand why it should be b and not b_; the latter doesn't work):

Defer[f[g[h[[i[[j[2], k[[1, m[[1, n[2]]]]]]]]]]]] /. 
 HoldPattern[Part[b, a_]] -> HoldPattern[b\[LeftDoubleBracket]a\[RightDoubleBracket]]

enter image description here

I leave the automation part to you.

EDIT 2
I discovered that if you add the above rule to ImportAutoReplacements and paste your SO code in a notebook in a Defer[] and evaluate this, you end up with a usable form with double brackets which can be used as input somewhere else.

EDIT 3
As remarked by Mr.Wizard invisibly below in the comments, the replacement rule isn't necessary. Defer does it on its own! Scientific progress goes "Boink", to cite Bill Watterson.

Boink

EDIT 4
The jury is still out on Defer. It has some peculiar side effects, and doesn't work well on all expressions. try the "Paste Tabular Data Palette" in the toolbag question for instance. Pasting this block of code in Defer and executing gives me this:
palette

It worked much better in another code snippet from the same thread:

enter image description here

The second part is how it looks after turning it in to input by editing the output of the first block (basically, I inserted a couple of returns to restore the format). This turns it into Input. Please notice that all double brackets turned into the correct corresponding symbol, but notice also the changing position of ReleaseHold.

Community
  • 1
  • 1
Sjoerd C. de Vries
  • 16,122
  • 3
  • 42
  • 94
  • Yes, I know about that. I am the one that gave the solution. (insert cheesy grin) – Mr.Wizard Mar 28 '11 at 18:40
  • More seriously, that is essentially unrelated to my question. The problem is not entering the symbols, it's automatically converting brackets into them. I think bracket counting is the only way. – Mr.Wizard Mar 28 '11 at 18:42
  • By the way, you never wrote back regarding the keyboard commands. Did the version I emailed you work? – Mr.Wizard Mar 28 '11 at 18:47
  • @Mr.Wizard I saw a couple of answers there. Which one was yours? As to your second remark: I plead guilty. – Sjoerd C. de Vries Mar 28 '11 at 18:53
  • It's one that starts out "Hello Barrie, What you want is not difficult." and suggests a modification to `KeyEventTranslations.tr` Did my email ever reach you? – Mr.Wizard Mar 28 '11 at 18:58
  • @Mr.Wizard I have been looking in my email, but haven't found anything on this issue. Could you resend it? – Sjoerd C. de Vries Mar 28 '11 at 19:00
  • I just resent it. The subject line is "Re: adding a keyboard shortcut for double brackets" -- edit -- at the time of the MathGroup post I couldn't remember where I learned that from, but it was from http://web.ift.uib.no/~szhorvat/mmatricks.php – Mr.Wizard Mar 28 '11 at 19:02
  • @Mr.Wizard I updated my answer. This is the closest I could get for the moment – Sjoerd C. de Vries Mar 28 '11 at 21:41
  • Sjoerd, thank you, `Defer` seems to be the magic function! If you evaluate `Defer[f[g[h[[i[[j[2], k[[1, m[[1, n[2]]]]]]]]]]]]` by itself (though the stacked brackets make my head swim) Mathematica does the interpretation automatically. I did not think to try anything like that. At the moment I cannot see any problem with it. Now I just need to package it. – Mr.Wizard Mar 28 '11 at 23:03
  • I have not found a way to "package" it so I am still looking for a way to make this work. :-/ – Mr.Wizard Mar 29 '11 at 04:08
  • @Mr.Wizard Indeed, the replacement part is superfluous. Funny, this. – Sjoerd C. de Vries Mar 29 '11 at 07:20
0

Simon wrote in a comment, but declined to post as an answer, something fairly similar to what I requested, though it is not automatic on paste, and is not in isolation from other formatting.

(One can) select the text and press Ctrl+Shift+N to translate to StandardForm

Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125