5
$\begingroup$

I'm trying to create a notebook where a user can enter a Mathematica expression, and its syntax is analyzed (number of tokens, compressed size, etc.).

Trying this answer,

CellPrint@TextCell[
  Row[{"This is an inline cell with automatic syntax highlighting: ", 
    ExpressionCell[Dynamic@input, "Notebook", "Input", 
     CellFrame -> True]}], "Text"]
Dynamic@input

the Dynamic doesn't update input. If I make an InputField inside the ExpressionCell the syntax highlighting doesn't apply, even if I change the BaseStyle. How can I get both dynamic behavior and syntax highlighting?

$\endgroup$

1 Answer 1

7
$\begingroup$

Here is what you can do:

x = ToBoxes@Defer@Plot[x, {x, 0, 1}];

InputField[Dynamic[x], Boxes, 
 BaseStyle -> {"Notebook", "Input", ShowCodeAssist -> True, ShowSyntaxStyles -> True}
]

Dynamic@x
Dynamic@MakeExpression[StripBoxes[x], StandardForm]

enter image description here

$\endgroup$
2
  • $\begingroup$ If I add whitespace inside that field, MakeExpression fails and generates ErrorBoxes; this is frustrating because the user cannot paste in input or add whitespace. Is there a way around that? $\endgroup$
    – lirtosiast
    Commented Jun 8, 2019 at 5:54
  • $\begingroup$ @lirtosiast I added StripBoxes to be less strict about the content. $\endgroup$
    – Kuba
    Commented Jun 8, 2019 at 6:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.