前端转换表达式 [英] Conversion of expressions by the FrontEnd
问题描述
最近我学习时, Mathematica 中有一些类型的表达式会由前端.
As I learned recently there are some types of expressions in Mathematica which are automatically parsed by the FrontEnd.
例如,如果我们评估HoldComplete[Rotate[Style[expr, Red], 0.5]]
,我们将看到FrontEnd不显示原始表达式:
For example if we evaluate HoldComplete[Rotate[Style[expr, Red], 0.5]]
we see that the FrontEnd does not display the original expression:
是否可以控制FrontEnd的这种行为?
Is it possible to control such behavior of the FrontEnd?
是否可以获取由FrontEnd自动解析的表达式的完整列表?
And is it possible to get complete list of expressions those are parsed by the FrontEnd automatically?
使用Print
时,我们可以看到对MakeBoxes
的调用:
We can see calls to MakeBoxes
when using Print
:
On[MakeBoxes]; Print[HoldComplete@Rotate["text", Pi/2]]
但是复制粘贴打印输出将得到更改的表达式:HoldComplete[Rotate["text", 1.5707963267948966]]
.它表明Print
不尊重HoldComplete
.
But copy-pasting the printed output gives changed expression: HoldComplete[Rotate["text", 1.5707963267948966]]
. It shows that Print
does not respect HoldComplete
.
创建输出Cell
时,也应该调用MakeBoxes
.有办法看到他们吗?
When creating output Cell
there should be calls for MakeBoxes
too. Is there a way to see them?
推荐答案
I have found a post by John Fultz with pretty clear explanation of how graphics functionality works:
在版本6中,内核具有 绝对没有任何参与 在生成渲染图像中. 显示广告的步骤 版本6中的图形非常多 像展示中使用的那些 非图形输出.它作为 如下:
In version 6, the kernel has absolutely no involvement whatsoever in generating the rendered image. The steps taken in displaying a graphic in version 6 are very much like those used in displaying non-graphical output. It works as follows:
1)计算表达式,然后
最终产生一些东西
头Graphics[]
或Graphics3D[]
.
1) The expression is evaluated, and
ultimately produces something with
head Graphics[]
or Graphics3D[]
.
2)传递结果表达式
通过MakeBoxes
. MakeBoxes
有一个
一组规则,这些规则可以转换图形
表达成方格的语言
前端用来表示
图形.例如,
2) The resulting expression is passed
through MakeBoxes
. MakeBoxes
has a
set of rules which turns the graphics
expression into the box language which
the front end uses to represent
graphics. E.g.,
In[9]:= MakeBoxes[Graphics[{Point[{0, 0}]}], StandardForm]
Out[9]= GraphicsBox[{PointBox[{0, 0}]}]
在内部,我们将此称为排版" 表达.可能有点奇怪 将图形视为 排版",但从根本上讲 发生的相同操作 排版(以这种方式工作 为期11年),因此我将使用该术语.
Internally, we call this the "typeset" expression. It may be a little weird thinking of graphics as being "typeset", but it's fundamentally the same operation which happens for typesetting (which has worked this way for 11 years), so I'll use the term.
3)所得的排版表达式为 通过MathLink发送到前端.
3) The resulting typeset expression is sent via MathLink to the front end.
4)前端解析排版 表达并创造内部 通常具有 一对一对应 排版表达式.
4) The front end parses the typeset expression and creates internal objects which generally have a one-to-one correspondence to the typeset expression.
5)前端渲染内部 对象.
5) The front end renders the internal objects.
这意味着通过调用MakeBoxes
在内核中执行转换.
This means that the conversion is performed in the Kernel by a call to MakeBoxes
.
可以通过高级代码拦截此呼叫:
This call can be intercepted through high-level code:
list = {};
MakeBoxes[expr_, form_] /; (AppendTo[list, HoldComplete[expr]];
True) := Null;
HoldComplete[Rotate[Style[expr, Red], 0.5]]
ClearAll[MakeBoxes];
list
这是我们得到的输出:
可以看到MakeBoxes
不尊重HoldAllComplete
属性.
在发送到FrontEnd之前自动转换的符号列表可以从FormatValues
获取:
The list of symbols which are auto-converted before sending to the FrontEnd one can get from FormatValues
:
In[1]:= list =
Select[Names["*"],
ToExpression[#, InputForm,
Function[symbol, Length[FormatValues@symbol] > 0, HoldAll]] &];
list // Length
During evaluation of In[1]:= General::readp: Symbol I is read-protected. >>
Out[2]= 162
这篇关于前端转换表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!