我有一个由另一个程序生成的 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 手动写入文件似乎不切实际。

该文件实际上是使用 Process.Start("filename.nb") 从 .Net 启动的,但命令行启动似乎同样有问题。

有帮助吗?

解决方案 4

这是我采用的解决方案。感谢您的帮助。

解决方案的主要步骤是通过内核格式化命令: -

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”。一切都很好,花花公子。

此方法适用于多态代码块,但是包括一些其他处理来格式化表单函数的单个函数调用[表达式,选项],以在每个选项之前添加线路破坏。这是用于生成两种输出的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();
    }
}

其他提示

这个怎么样:

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

我对其进行了测试,并且似乎可以工作(从普通文件导入,导出到您将打开的文件)。这确实创建了明确的框,但是用户方面的努力很少。我没有测试,但是您应该能够从命令行以脚本模式运行此代码。

编辑

要在Mathematica中进行测试,您可以使用EG

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

在运行上面的代码之前。

您可以使用以下包装:

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

然后,命令笔记本电脑和笔记本电报可用于保存和关闭内容;)

除非你创建了 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 选项。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top