3

我有另一個程序生成的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,FontFamily->"Courier"]] 

文本被寫入到一個文件,後綴粗暴「.nb」,並推出,並表達在筆記本打開沒有格式化。要實現格式化,使用BoxData手動編寫文件似乎不切合實際。

該文件實際上是從.Net使用Process.Start(「filename.nb」)啓動的,但命令行啓動似乎同樣存在問題。

+0

只是好奇...你用什麼程序來生成? –

+0

@ belisarius - 使用.Net構建一個具有衆多參數和參數的函數。這實際上不是一個像例子那樣的劇情功能。 –

回答

1

這是我採用的解決方案。感謝所有的幫助。

的解決方案的主要步驟是通過內核格式化命令: -

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(); 
    } 
} 
2

您可以使用以下包裝:

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和NotebookClose可以用來保存和關閉的事情;)

+0

但這並沒有格式化Mathematica表達式 –

+0

這有點接近我在找的東西,但儘管我嘗試不了從命令行執行它:CreateDocument [ExpressionCell [Defer [Plot [{Exp [x] ,插值[表[{K/5,精通[(K-1/2)/ 5]},{K,0,5}],InterpolationOrder-> 0] [X]},{X,0,1} ,Filling - > {1 - > {{2},{Yellow,Orange}}},PlotLabel-> Style [「Formatting」,Blue,FontFamily - >「Courier」]]],「Input」]] –

+0

@Chris你需要將代碼封裝在Developer'UseFrontEnd中,我的意思是在'Developer'上下文中使用'UseFrontEnd'。 – Sasha

2

除非你創建BoxData表述明確,沒有辦法格式化您的表達式,而不實際調用至少Mathematica FrontEnd。

我能想到的最接近的是你添加如下內容:

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選項

+1

'ToBoxes'允許僅使用內核將表達式轉換爲'BoxForms'。 –

+0

@阿列克謝 - 謝謝。我得到這樣的內核輸出:ToBoxes [Defer [ Plot [{Exp [x],Interpolation [Table [{k/5,Exp [(k-1/2)/ 5]},{k 0,0}], InterpolationOrder-> 0] [x]},{x,0,1},填充 - > {1 - > {{2},{Yellow,Orange}}}, PlotLabel->樣式[「格式」,藍色,FontFamily - >「快遞」]] ]] –

4

這個怎麼樣:

Export["C:\\Temp\\formatTest1.nb", 
    ToExpression[Import["C:\\Temp\\formatTest.nb", "Text"], InputForm, MakeBoxes]] 

我測試了它,它似乎工作(從純文件導入,導出到你將打開的文件),這確實會創建顯式框,但在用戶方面很少費力,我沒有測試,但你應該能夠運行這個代碼在腳本模式下,從命令行。

EDIT

要從Mathematica內部進行測試,您可以使用例如

Export["C:\\Temp\\formatTest.nb", 
    [email protected]@[email protected] 
    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"] 

運行上面的代碼之前。

+0

謝謝。有趣的是看到使用MakeBoxes的ToExpression。我可以確認它是否有效,例如FullForm [ToExpression [ToString [HoldForm [FullForm [expression]]], InputForm,MakeBoxes]] == FullForm [ToBoxes [Defer [expression]]] –

+0

@Chris我想我下意識地不喜歡'Defer',不知道其他功能無法以更透明的方式實現的情況。 –