本文介绍了如何在粘贴时自动转换Part [[double-bracket]]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个小小的烦恼是使用双方括号 Part 而不是单个字符 \ [LeftDoubleBracket] \ [RightDoubleBracket] 。我希望在将纯文本代码(例如StackOverflow)粘贴到Mathematica Notebook时自动替换它们。我无法配置它。



是否可以使用 ImportAutoReplacements 或其他自动方法(首选)来完成,或者我是否需要使用像粘贴表格数据调色板这样的方法例如。在延迟和执行中粘贴这段代码给了我这个:



它在同一个线程的另一个代码片段中工作得更好:





第二部分是通过编辑将其输入到输入后的样子第一个块的输出(基本上,我插入了几个返回来恢复格式)。这将它变成输入。请注意,所有双括号都变成了正确的相应符号,但也请注意ReleaseHold的位置变化。


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 ++ 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.

解决方案

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]]

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.

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:

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

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.

这篇关于如何在粘贴时自动转换Part [[double-bracket]]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 08:37