Wolfram mathematica 如何使用格式正确的表达式创建笔记本
我有一个Mathematica表达式,它是由另一个程序生成的,我想在笔记本中打开,格式正确。例如,另一个程序生成:Wolfram mathematica 如何使用格式正确的表达式创建笔记本,wolfram-mathematica,mathematica-frontend,mathematical-expressions,Wolfram Mathematica,Mathematica Frontend,Mathematical Expressions,我有一个Mathematica表达式,它是由另一个程序生成的,我想在笔记本中打开,格式正确。例如,另一个程序生成: Plot[{Exp[x],Interpolation[Table[{k/5,Exp[(k-1/2)/5]},{k,0,5}], InterpolationOrder->0][x]},{x,0,1},Filling->{1->{{2},{Yellow,Orange}}}, PlotLabel->Style["Formatting",Blue,FontFamil
Plot[{Exp[x],Interpolation[Table[{k/5,Exp[(k-1/2)/5]},{k,0,5}],
InterpolationOrder->0][x]},{x,0,1},Filling->{1->{{2},{Yellow,Orange}}},
PlotLabel->Style["Formatting",Blue,FontFamily->"Courier"]]
文本被写入到一个文件中,粗略地加上“.nb”后缀并启动,表达式在笔记本中打开,没有格式。要实现格式化,使用BoxData手动写入文件似乎不切实际
该文件实际上是使用Process.Start(“filename.nb”)从.Net启动的,但命令行启动似乎同样有问题 您可以使用以下包装:
nb = CreateWindow[
DocumentNotebook[{
Plot[{Exp[x],
Interpolation[Table[{k/5, Exp[(k - 1/2)/5]}, {k, 0, 5}],
InterpolationOrder -> 0][x]}, {x, 0, 1},
Filling -> {1 -> {{2}, {Yellow, Orange}}},
PlotLabel ->
Style["Formatting", Blue, FontFamily -> "Courier"]]
}]]
然后可以使用NotebookSave和notebooksclose命令来保存和关闭对象;) 除非明确创建
BoxData
表达式,否则在不实际调用Mathematica前端的情况下,无法格式化表达式
我能想到的最接近的方法是添加以下内容:
SelectionMove[EvaluationNotebook[], Next, EvaluationCell];
FrontEndExecute[{FrontEndToken[FrontEnd`InputNotebook[],
"SelectionConvert", "StandardForm"]}];
Plot[{Exp[x], Interpolation[Table[{k/5, Exp[(1/5)*(k - 1/2)]}, {k, 0, 5}],
InterpolationOrder -> 0][x]}, {x, 0, 1},
Filling -> {1 -> {{2}, {Yellow, Orange}}},
PlotLabel -> Style["Formatting", Blue, FontFamily -> "Courier"],
Evaluated -> True]
SelectionMove[EvaluationNotebook[], After, GeneratedCell];
当计算单元格时,它会自动格式化Plot
命令。
(顺便说一句:您可能应该在列表前面添加Evaluate
,或者添加(没有很好的文档记录)Evaluate->True
选项。这样如何:
Export["C:\\Temp\\formatTest1.nb",
ToExpression[Import["C:\\Temp\\formatTest.nb", "Text"], InputForm, MakeBoxes]]
我对它进行了测试,它似乎可以工作(从普通文件导入,导出到您将要打开的文件)。这确实可以创建显式框,但用户只需付出很少的努力。我没有进行测试,但您应该能够在脚本模式下从命令行运行此代码
编辑
要从Mathematica内部进行测试,您可以使用
Export["C:\\Temp\\formatTest.nb",
ToString@HoldForm@FullForm@
Plot[{Exp[x],Interpolation[Table[{k/5, Exp[(k - 1/2)/5]}, {k, 0, 5}],
InterpolationOrder -> 0][x]}, {x, 0, 1},
Filling -> {1 -> {{2}, {Yellow, Orange}}},
PlotLabel -> Style["Formatting", Blue, FontFamily -> "Courier"]],
"Text"]
在运行上述代码之前。以下是我采用的解决方案。感谢所有帮助 解决方案的主要步骤是通过内核格式化命令:-
FullForm[ToBoxes[
Defer[Plot[{Exp[x],
Interpolation[Table[{k/5, Exp[(k - 1/2)/5]}, {k, 0, 5}],
InterpolationOrder -> 0][x]}, {x, 0, 1},
Filling -> {1 -> {{2}, {Yellow, Orange}}},
PlotLabel ->
Style["Formatting", Blue, FontFamily -> "Courier"]]]]]
然后,将格式化数据封装以创建笔记本:-
Notebook[{Cell[BoxData[
... ( inserted box-formatted output ) ...
], "Input"]
},
WindowSize->{615, 750},
WindowMargins->{{328, Automatic}, {Automatic, 76}},
StyleDefinitions->"Default.nb"
]
这是写入一个文件,后缀为“.nb”。一切都很好
这种方法适用于多语句代码块,但还包括了一些额外的处理,以格式化形式函数[expression,options]的单个函数调用,从而在每个选项之前添加换行符。以下是用于生成这两种类型输出的C#代码:-
public static class MathematicaHelpers
{
public static string CreateNotebook(string mathCommand, string fileLocation, MathKernel kernel, bool addNewLines)
{
if (addNewLines) {
mathCommand = string.Format("{0}{1}{2}", "Module[{boxoutput,b2},boxoutput=FullForm[ToBoxes[Defer[", mathCommand, "]]];b2=boxoutput[[1,1,3,1]];boxoutput[[1,1,3,1]]=Join[Flatten[Riffle[Partition[b2,2],\"\\[IndentingNewLine]\"],1],{\"\\[IndentingNewLine]\",Last[b2]}];boxoutput]");
} else {
mathCommand = string.Format("{0}{1}{2}", "FullForm[ToBoxes[Defer[", mathCommand, "]]]");
}
fileLocation = Path.ChangeExtension(fileLocation, ".nb");
mathCommand = ComputeMathCommand(mathCommand, kernel);
mathCommand = string.Format("{0}{1}{2}", "Notebook[{Cell[BoxData[", mathCommand,
"], \"Input\"]},WindowSize->{615, 750}, WindowMargins->{{328, Automatic}, {Automatic, 76}},StyleDefinitions->\"Default.nb\"]");
File.WriteAllText(fileLocation, mathCommand);
return fileLocation;
}
private static string ComputeMathCommand(string command, MathKernel kernel)
{
kernel.Compute(command);
return kernel.Result.ToString();
}
}
只是好奇…你用什么程序来生成它?@belisarius-使用.Net来构建一个包含大量参数和参数的函数。它实际上不像示例中那样是一个绘图函数。这与我要寻找的有点接近,但尽管尝试了,我还是无法从命令行执行它:CreateDocument[ExpressionCell[Defer[plot[{Exp[x] ,插值[表[{k/5,Exp[(k-1/2)/5]},{k,0,5}],插值顺序->0][x]},{x,0,1},填充->{1->{2},{黄色,橙色}}},绘图标签->样式[“格式化”,蓝色,FontFamily->Courier”]],“输入”]@Chris你需要在开发者
UseFrontEnd中包装你的代码,我的意思是在Developer
上下文中使用UseFrontEnd`。ToBox
只允许使用内核将表达式转换为BoxForms
。@Alexey-谢谢。我从这个内核输出中得到了一些东西:ToBoxes[Delay[Plot[{Exp[x],Interpolation[Table[{k/5,Exp[(k-1/2)/5]},{k,0,5}],InterpolationOrder->0][x]},{x,0,1},Filling->{1->{2},{Yellow,Orange}}},PlotLabel->Style[“Formatting”,Blue,fontf家族->“Courier”]]]谢谢。很有趣地看到ToExpression在Makebox中的使用。我可以确认它是有效的,例如:FullForm[ToExpression[ToString[HoldForm[FullForm[expression]],InputForm,makebox]]==FullForm[ToBoxes[Defer[expression]]]@Chris我想我下意识地不喜欢Defer
,可能是因为我不知道有哪种情况下它的功能无法通过其他更透明的方式实现。