如何在粘贴时自动转换 Part [[ 双括号 ]]? [英] How do I get auto-conversion of Part [[ double-brackets ]] on paste?

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

问题描述

我最讨厌的一点是对 Part 使用双方括号而不是单个字符 [LeftDoubleBracket][RightDoubleBracket].我希望在将纯文本代码(例如来自 StackOverflow)粘贴到 Mathematica Notebook 时自动替换这些代码.我一直无法配置它.

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.

是否可以使用 ImportAutoReplacements 或其他自动方法(首选)来完成,或者我是否需要使用引用 这里?

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 给了 Defer 和 Simon 给了 Ctrl+Shift+N 这都导致 Mathematica 自动格式化代码.这些都是不错的选择.

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

我仍然对自动和/或尽可能多地保留原始代码的方法感兴趣.例如,保持前缀f@1、中缀1 ~f~2和后缀1//f函数的原始形式.

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.

推荐答案

不是真正的答案,而是一个 thread 使用一次按键输入双 [[ ]] 对(光标位于两对之间)发生在几周前的数学组上.这对我没有帮助,但对于其他人来说,这显然是一个解决方案.

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.

编辑
为了更好地解决我稍微偏离主题的第一反应,这里有一个模式替换似乎可以完成这项工作(尽管我自己很难理解为什么它应该是 b 而不是 b_;后者不起作用):

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.

编辑 2
我发现如果您将上述规则添加到 ImportAutoReplacements 并将您的 SO 代码粘贴到笔记本中的 Defer[] 并对其进行评估,您最终会得到一个带有双括号的可用表单,该表单可以用作其他地方的输入.

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.

编辑 3
正如 Mr.Wizard 在下面的评论中无形地指出的那样,替换规则不是必需的.Defer 是自己做的!引用比尔沃特森的话说,科学进步是笨蛋".

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.

编辑 4
陪审团仍在 Defer 上.它有一些特殊的副作用,并且不适用于所有表达式.试试工具包问题 例如.在 Defer 中粘贴这段代码并执行给了我这个:

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:

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

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 [[ 双括号 ]]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆