Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6535513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:22:56+00:00 2026-05-25T10:22:56+00:00

I have a Mathematica expression generated by another program, which I would like to

  • 0

I have a Mathematica expression generated by another program, which I would like to open in a notebook, properly formatted. For instance, the other program generates this:

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

The text is written into a file, crudely suffixed “.nb”, and launched, and the expression opens in a notebook without formatting. To achieve formatting, writing a file manually with BoxData seems impractical.

The file is actually being launched from .Net using Process.Start(“filename.nb”), but a command line launch seems equally problematic.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:22:57+00:00Added an answer on May 25, 2026 at 10:22 am

    Here is the solution I adopted. Thanks for all the help.

    The main step of the solution is to format the command via the kernel:-

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

    Then the formatted data is encapsulated to create a notebook:-

    Notebook[{Cell[BoxData[
    
    ... ( inserted box-formatted output ) ...
    
    ], "Input"]
    },
    WindowSize->{615, 750},
    WindowMargins->{{328, Automatic}, {Automatic, 76}},
    StyleDefinitions->"Default.nb"
    ]
    

    This is written to a file, suffixed “.nb”. All fine and dandy.

    This approach works well for multi-statement blocks of code, but some additional processing was included to format a single function call of the form Function[expression, options] to add a line-break before each option. Here is the C# code used to produce both types of output:-

    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();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several expressions I have generated in Mathematica that I would like to
I have a very complicated mathematica expression that I'd like to simplify by using
I would like to plot a decomposition tree in Mathematica. I have a function
I have a long expression that I would like to split into a collection
I have a binary tree based mathematical expression parser I built, which works great
I have a regular expression in which I'm trying to extract every group of
Does mathematica have something like select any that gets any element of a list
I have some expressions in Mathematica that are defined in terms of other expressions.
I would like to be able to convert a python mathematical expression into content
I have an issue concerning constraints which should be generated dynamically from a list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.