4

考虑以下:

printMessage[cellexpr_]:=CellPrint@Cell[cellexpr,"Message",
                         CellLabel->"(slave Kernel)",ShowCellLabel->True,
                         CellFrameMargins->0,Background->LightBrown,
                         CellLabelAutoDelete->False];
printMessage[BoxData[RowBox[{RowBox[{"Sin", "::", "\"argx\""}], ": ",
  "\"\\!\\(Sin\\) called with \\!\\(2\\) arguments; 1 argument is expected.\""}],
  StandardForm]]
Sin[1,1];

-->

(slave Kernel) Sin::argx: Sin called with 2 arguments; 1 argument is expected.
During evaluation of In[1]:= Sin::argx: Sin called with 2 arguments;
1 argument is expected. >>

在此处输入图像描述

可以看到自动生成Message的继承了之前printed的样式Cell。为什么会发生这种情况?以及如何防止这种情况?

4

1 回答 1

1

似乎此错误的一种解决方法是移至选项"Message"中的另一个位置Cell

printMessage[cellexpr_]:=CellPrint@Cell[cellexpr,CellLabel->"(slave Kernel)",
                         "Message", ShowCellLabel->True,
                         CellFrameMargins->0,Background->LightBrown,
                         CellLabelAutoDelete->False];
printMessage[BoxData[RowBox[{RowBox[{"Sin", "::", "\"argx\""}], ": ",
  "\"\\!\\(Sin\\) called with \\!\\(2\\) arguments; 1 argument is expected.\""}],
  StandardForm]]
Sin[1,1];

编辑

但由于打印的结果外观发生了Cell变化:

截屏

print := printMessage[
  BoxData[RowBox[{RowBox[{"NIntegrate", "::", "\"slwcon\""}], ": ", 
     "\"Numerical integration converging too slowly; suspect one of \
the following: singularity, value of the integration being 0, \
oscillatory integrand, or insufficient WorkingPrecision. If your \
integrand is oscillatory try using the option Method->Oscillatory in \
NIntegrate.\""}], StandardForm]]

编辑 2

上述解决方法仅在第一个新会话期间有效。保存并重新打开笔记本后,问题再次出现。

目前唯一可行的解​​决方法是不使用样式,而是指定明确的选项集。

于 2011-04-17T03:26:12.017 回答