這是我採用的解決方案。感謝所有的幫助。
的解決方案的主要步驟是通過內核格式化命令: -
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」。所有的罰款和花花公子。
此方法適用於多語句代碼塊,但包含一些額外的處理以格式化Function [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構建一個具有衆多參數和參數的函數。這實際上不是一個像例子那樣的劇情功能。 –