I'm constructing a function that gives me subindices in output.
The function is HAt.

For example:

HAt[u == u0, t -> 0]

enter image description here

The function is constructed following:

HAt/:MakeBoxes[HAt[eq_, x_-> n_], StandardForm]:=
      RowBox[{"", ToBoxes[eq[[1]]], ")"}], 
      RowBox[{ToBoxes[x], "=", ToBoxes[n]}]], 
      RowBox[{"(", ToBoxes[eq[[2]]], ")"}], 

So, the problema is when the right-hand (or left-hand) of my equation is a number. For example, if I put:

HAt[u == 0, t -> 0]

enter image description here

But I want some like this:

enter image description here

I think the ideia is put some condition at eq[[2]] when this term is a numerical term, some like that:

HAt/:MakeBoxes[HAt[eq_, x_ -> n_] /; eq[[2]]_Numeric, StandardForm] := 
       RowBox[{ToBoxes[x], "=", ToBoxes[n]}]], 

But the above doesn't work. How do I write the condition I want to enforce correctly?

  • 1
    $\begingroup$ Why not do this with PatternTest? Also Numeric isn't a thing... NumericQ is though. $\endgroup$
    – b3m2a1
    Jan 29 '19 at 21:01
  • $\begingroup$ Solved, thank you @b3m2a1 $\endgroup$
    – Mateus
    Jan 29 '19 at 21:20

When playing with typesetting the most important thing is to keep your input unevaluated. In cases like Hold @ HAt[...] MakeBoxes won't know that Hold is there.

There are several things that can cause evaluation leaks:

  • ToBoxes[x] will evaluate x, use MakeBoxes

  • eq[[1]] will evaluate eq, use lhs_ == rhs_ to match your equations

  • ?NumericQ will evaluate as well as opposed to e.g. _Real but the latter is not general enough. Fortunately there is System`Dump`HeldNumericQ :)

Here is how I'd do this:

HAt // ClearAll;
HAt /: MakeBoxes[HAt[lhs_ == rhs_, x_ -> n_], fmt : StandardForm] := With[
  { subscript = SubscriptBox[RowBox[{"(", #, ")"}], RowBox[{MakeBoxes[x, fmt], "=", MakeBoxes[n, fmt]}]] &
, RowBox[{
    subscript @ MakeBoxes[lhs, fmt]
  , "\[Equal]"
  , If[System`Dump`HeldNumericQ[rhs], Identity, subscript] @ MakeBoxes[rhs, fmt]

enter image description here

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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