Episode #125 of the Stack Overflow podcast is here. We talk Tilde Club and mechanical keyboards. Listen now

Hot answers tagged

36

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 appearance ...


29

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 ...


21

This syntax was deprecated in the version 6.0 era. According to the legacy documentation, For example, in version 5.2, the following strings are interpreted differently string1 = "first line second line" string2 = "\<first line second line\>"


17

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 ...


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"], $NewMessage[...


15

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 get ...


15

Assignments to MakeBoxes and Format get special treatment. The definitions are stored as (under-documented) FormatValues: FormatValues[baseForm] // InputForm (* {HoldPattern[MakeBoxes[baseForm[a_, b_], fmt_]] :> ToBoxes[BaseForm[a, b]]} *) Note that if we omit InputForm from the expression above, then the system will attempt to apply the formatting ...


14

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


13

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 ;;-2]]...


13

Verbeia is right. An alternative notation is to use escpdesc which gives a partial derivative; thus, typing escpdesc ctrl-t followed by f[x,t] will give the derivative of f with respect to its second argument. For instance, this is a valid way to specify a differential equation: This is closer to what you're after than D[f[x,t],t], for instance.


13

I am a developer at Wolfram Research and I am trying to share some of the work I have been doing with parsing WL code. I have written a package for parsing WL code and retaining interesting metadata, such as file and line information. I also expose a tokenization function. The paclet is available on the public paclet server: In[1]:= PacletUpdate["AST","...


13

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


13

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 : ...


12

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 ...


12

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. ...


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 of ...


11

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[ ToExpression[...


11

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 /: MakeBoxes[...


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

For a start, f[x,y]^(0,1) isn't the same as f^(0,1)[x,y]. But the real reason is that these expressions are very different in meaning, as revealed by their FullForm: D[f[x, y], y] // FullForm Derivative[0,1][f][x,y] versus (and I had to use a simple symbolic expression as the exponent to show what was going on: f[x,y]^z//FullForm Power[f[x,y],z] ...


10

Thanks to andre's comment (where this link is provided), I now see the effect of those delimiters (I tested it in Mathematica 11 and also some earlier versions). When I add 2 newlines to the box representation of the cell: Cell[BoxData["\"\<a bc\>\""], "Input", CellChangeTimes->{{3.662918813714031*^9, 3.6629188530623317`*^9}}] and switch back ...


10

Summary of the all available information In the hoarier days space-like characters (spaces, newlines, tabs) inside strings were interpreted on input in an odd way: for example single newlines followed by spaces or tabs were converted to a single space. The \<\> syntax was introduced as a way to avoid this: between \< and \> the space-like ...


10

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 ...


9

What internally makes the superscript behave as a Derivative seems to be implemented with TagBox, this is what the output looks like: SuperscriptBox["f", TagBox[ RowBox[{"(", RowBox[{"0", ",", "1"}], ")"}], Derivative], MultilineFunction -> None] If you show this with DisplayForm you will get something that looks like a superscript but ...


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

Point of conversion A large and perhaps key difference is that MakeBoxes (foo) only transforms the expression into the expanded form when it is converted to Box form. It's FullForm remains unchanged. foo[1, 0.3] // InputForm foo[1, 0.3`] This means that you can operate upon the expression in every standard way without thought to a hidden internal format....


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

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

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 ...


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