Hot answers tagged

28

Assuming nb is your notebook object, then this will do what you want without touching the clipboard: First[FrontEndExecute[ FrontEnd`ExportPacket[NotebookSelection[nb], "InputText"]]] Some notes about this solution: It preserves evaluation semantics precisely, regardless of typesetting. It does not dirty the clipboard If you prefer to get the ...


24

My other answer is a nice solution for interactively looking at boxes, but in the comments, Mr.Wizard seems to be indicating that he's more interested in programmatic usage, and that he's definitely interested in seeing the box form after the FE has stripped non-semantic boxes to send to the kernel. So here's a totally different method for doing this which ...


16

This took some digging but at least in Version 7 the FrontEnd command is FT, e.g.: FE`FT["Plot"] You can read the definition with Definition[FE`FT]. If you want only the Box form itself we can modify it accordingly (here for version 7): templateCell[name_String] := Module[{template}, If[! StringQ@ToExpression[name <> "::usage"], ...


13

Here's some code which produces an InputField and the box form of anything you type into the InputField as you type it: DynamicModule[{boxes = ""}, Column[{InputField[Dynamic[boxes], Boxes, ContinuousAction -> True], Dynamic[boxes, BaseStyle -> {ShowStringCharacters -> True}]}]] The critical idea here is using a Boxes style InputField to ...


13

You can use this to create a functionality which will fit your need the best. Here's how you can preview your input cell with c highlighted Red. CellPrint[ NotebookRead @ PreviousCell[] /. "c" -> InterpretationBox[ StyleBox["c", FontColor -> RGBColor[1, 0, 0]], c ] ] You can even evaluate such cell. General ...


12

You can put it to the last cell and evaluate, it will resize all images from InputCells to the width of 100px. You can of course change affected cell set and output parameters. Do[ With[{nr = NotebookRead[cell]}, If[! FreeQ[nr, "ImageGraphics"], SelectionMove[cell, Cell, All]; NotebookWrite[ EvaluationNotebook[], nr /. g : ...


11

You can convert any expression to string by using ToString. If you want to preserve the visual representation, you should use ToString[(*your expression*), StandardForm]. logo = Import["http://wolfram.com/favicon.ico", "Image"] logostr = ToString[logo, StandardForm] StringJoin["Mathematica", logostr] % // StringQ Edit: By checking the cell expression ...


10

This is a nice exercise on boxing: MakeBoxes[u[v_[r_[b_]]], TraditionalForm] := Module[{b1, b2, b3, t}, t = ToBoxes[#, TraditionalForm] &; {bl1, bl2, bl3} = StyleBox[#1, #2] & @@@ { {"{", {20, Orange}}, {"[", {15, Purple}}, {"(", {12, Blue}}}; {br1, br2, br3} = {bl1, bl2, bl3} /. {"[" -> "]", "{" -> "}", "(" -> ")"}; ...


10

tokenize[str_] := Module[{exp, nb = CreateDocument[{ExpressionCell@ InputForm@MakeExpression[str, StandardForm]}, Visible -> False]}, SelectionMove[nb, Next, Cell]; exp = Flatten[ NotebookRead[nb][[1, 1]] /. {RowBox -> List, i_String /; StringMatchQ[i, Whitespace ..] :> Sequence[]}]; NotebookClose[nb]; exp[[3 ...


10

The following functions will load the expressions and erroneous cells from a notebook: notebookExpressions[path_, pattern_:_] := Cases[Import[path, "Notebook"] // First , c:Cell[_, "Input"|"Output"|"Print", ___] :> Module[{v = eval[c]}, v /; MatchQ[v, _$Failed | Hold[pattern]]] , Infinity ] eval[cell_] := Quiet @ Check[ ...


10

This was an intentional change. It is no longer possible to get "short form linear syntax", as we call it, from the kernel. Instead, the kernel now emits only "long form linear syntax" for StandardForm/TraditionalForm, and the boxes themselves do not do anything special for InputForm. This is the first step in what will be a multistep process to replace ...


10

It is well-documented! According to the Documentation page for StandardForm, StandardForm generates output that gives a unique and unambiguous representation of Wolfram Language expressions, suitable for use as input. » StandardForm is the standard format type used for both input and output of Wolfram Language expressions in notebooks. ...


9

You can place your desired output in a Row and then put it into DisplayForm. Manipulate[ Switch[testStatChoice, 1, testText1], {{testStatChoice, 1, "Select Calculation"}, {1 -> "1: First Calculation Example"}, ControlType -> PopupMenu}, Initialization :> {testText1 := Row[{SuperscriptBox["R", "*"]// TraditionalForm, " = ", "Rate of star ...


9

Perhaps it's not obvious because this doesn't look like a box. But the way that DocumentNotebook, et al, get translated into Notebook is by going through MakeBoxes. Which means that RawBoxes saves the day by preserving its guts in MakeBoxes: DocumentNotebook@{TextCell["Hello world!", "Section"], RawBoxes@cell, TextCell["The solution is:", "Text"]}


8

The simple answer is, if you want a string converted to StandardForm, you could wrap BoxData around it. E.g., CellPrint[Cell[BoxData["myFunction::usage=\"myFunction does ...\";"], "Input"]] But, in general, I wouldn't structure this as a question of CellPrint vs. FrontEnd`CellPrint. FrontEnd`CellPrint is undocumented, and therefore there is no contract ...


8

My colleague John Fultz suggested the following answer. f /: MakeBoxes[dat : f[args_], fmt_] := TagBox[ToBoxes[Rasterize@RandomImage[1, {100, 100}]], InterpretTemplate[f[args] &], Editable -> False, Selectable -> True, SelectWithContents -> True, Tooltip -> "tooltip"] After a bit of exploring I realized that I should have checked ...


8

The problem here is independent of Manipulate or Dynamic. It is about how to display a Cell object without using a CellPrint statement. testText1 in itself is always displayed as Cell[...]. Therefore I suggest reconstructing the expression as something else, not wrapped in Cell. testText1 := TraditionalForm@Row[{Superscript[R, "*"], " = Rate of star ...


8

I believe this is the documentation you are looking for: String Representation of Boxes Notably: And:


8

Question 1: What is the typesetting in Mathematica? What procedures does it include? I think that this 2008 year MathGroup post by John Fultz completely answers this question, so I'll cite it here: In version 6, the kernel has absolutely no involvement whatsoever in generating the rendered image. The steps taken in displaying a graphic in ...


7

NOTE: This answer is provided for illustrative purposes only, since it shows some techniques of working with boxed data. While it illustrates how one could emulate the correct behavior in some cases, this code should NOT be used in practice (as a solution for this particular problem), because doing so may be both fragile and dangerous. Please read the ...


7

This is documented in Part >> More Information (Part >> Details in V9):


7

While I was working on alternative TeX export, I had similar requirement. I wanted to export annotated Mathematica code to TeX, with annotations reflecting FrontEnd's syntax highlighting. Since I couldn't find a way to use front end itself to do it, I decided to write my own package. My SyntaxAnnotations package is now available on GitHub. It works by ...


7

An good explanation can be found an old mathgroup archive thread which I have reconstructed: When you create a typeset form for a function or operator, you must write a MakeBoxes definition for that function. For example, if you want Transpose[A] to have the typeset form $A^T$ then you might, erroneously, write it this way: Transpose /: ...


6

Not quite tested embeddedNotebookForm/: MakeBoxes[embeddedNotebookForm[nb:Notebook[cells_List,___]],StandardForm]:= MakeBoxes@DocumentNotebook[{TextCell@"tag"}]/.{ {{Cell["tag"]}}:>Block[{},List/@ Replace[cells, Cell[CellGroupData[{cs__Cell},_]]:>cs, {1}] /;True], ...


6

This is caused by the AutoMultiplicationSymbol option in the front end. SetOptions[$FrontEnd, AutoMultiplicationSymbol -> False] Will make the space disappear.


6

Why not use ToBoxes (or MakeBoxes) to construct boxes, instead of doing it yourself? DisplayForm[ ToBoxes[Row[List[0, 1, Superscript[2, 3], Superscript[1, 2], a]]]]


6

You can check also the docs for PrecedenceForm for examples. Precedence can be used to force parenthesization. If you put a low-ish number, you will likely get a parenthesis. I am afraid I cannot help with the fourth argument (group).


5

One approach that may be familiar to more experienced users is based on the input syntax described in String Representation of Boxes. \(input\)             raw boxes This appears to work but closer inspection shows that it is not exact: "\({1*^4, 000123, a*b c}\)" // ToExpression ...


5

Interesting question. I don't have much experience with this sort of thing so I imagine there is a better way but this is what I could hack together at the moment: frac[lst_List] := Column[ Column /@ Partition[Riffle[lst, \[HorizontalLine]], 2, 2, -1, {}], Alignment -> Center, Spacings -> 0] {"x", 200, Integrate[Sin[x^2], x]} // frac Here ...


5

When I see this right, then the evil function is TraditionalFormDump`ordplus. This seems to change the order. The arguments can be extracted from a Trace TraditionalForm[a + b]; (* Dummy call *) TraditionalFormDump`ordplus[{{"+", "b"}, {"+", "a"}}, {}] (* {2,1} *) If we change this to give a sorted list, then your arguments are not reordered ...



Only top voted, non community-wiki answers of a minimum length are eligible