Mathematica 中的语法着色 [英] Syntax Coloring In Mathematica

查看:29
本文介绍了Mathematica 中的语法着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在

f[x_] = 2 x

或可变符号,例如

lotto = Table[2, {10}];

自动着色?

在 M8 的语法着色中,没有为此提供选项.

In Syntax coloring on M8 no option is offered for this.

只有没有赋值的局部变量或全局符号.

Only Local Variables or Global symbols that have no values assigned.

推荐答案

这不是您所要求的,但可能对您有用.

This is not exactly what you asked for, but it may be useful to you.

您可以使用以下方法按上下文突出显示符号:

You can highlight symbols by context, using this method:

SetOptions[$FrontEndSession, 
  AutoStyleOptions -> {"SymbolContextStyles" -> {"highlight`" -> Green}}
]

AppendTo[$ContextPath, "highlight`"];

现在,当你在上下文 highlight` 中创建一个符号时,它会自动变成绿色:

Now, when you create a symbol in the context highlight` it will automatically be colored green:

highlight`lotto ;

并且由于 highlight` 被附加到 $ContextPath 之后,该符号可以简单地使用:

and since highlight` was appended to $ContextPath, after that the symbol can be used plainly:

lotto

如果您希望自动突出显示您创建的所有符号,请设置:

If you want all the symbols you create to automatically be highlighted, then set:

$Context = "highlight`"

之后,您创建的所有 符号都将属于上下文 highlight` 并接收颜色.

After that, all new symbols you create will belong to the context highlight` and will receive the color.

新的意思是以前没有在会话中使用过的,或者已经被删除d.

New means ones that have not been previously used in the session, or have been Removed.

在我看来,实现此目的的更好方法(避免可能的阴影"问题)可能是为 Global` 上下文本身设置突出显示.我没有考虑过这样做的后果,但可以单独使用它:

It occurs to me that a better way to accomplish this, that avoids a possible "shadowing" problem, may be to set highlighting for the Global` context itself. I have not considered the ramifications of this, but it can be done with this alone:

SetOptions[$FrontEndSession, 
  AutoStyleOptions -> {"SymbolContextStyles" -> {"Global`" -> Green}}
]

<小时>

使用提示

  • 上下文highlight`是完全任意的,你可以用不同的颜色突出显示多个上下文.


    Tips on usage

    • The context highlight` is completely arbitrary, and you can have multiple contexts highlighted with different colors.

      您可以为包的上下文(例如 Units`)着色,以区分哪些符号属于该包.

      You can color the contexts of packages such as Units` to distinguish which symbols belong to that package.

      这篇关于Mathematica 中的语法着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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