質問

別のプログラムによって生成された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"]]
     }]]

次に、NotebooksaveとNotebookcloseを使用して、保存して閉じることができます;)

作成しない限り 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 オプション。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top