Problem with Message styling

2019-06-27 13:55发布

问题:

Consider the following:

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

One can see that the auto-generated Message inherits the style of the previous printed Cell. Why this happens? And how to prevent this?

回答1:

It seems that one workaround for this bug is to move "Message" to another place in the Cell options:

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

EDIT

But as the result appearance of the printed Cell is changed:

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

EDIT 2

The above workaround works only during the first fresh session. After saving and reopening of the Notebook the problem appears again.

The only working workaround a this moment is do not use Styles but to specify explicit set of options.