How can I define a function with a variable with subscript?

I want to define a function in Mathematica:

$$h(\tau_x)=\tau_x$$

$$\tau_x$$ has no special meaning. To be honest, it's totally OK if I use the function $$h(x)=x$$ to replace $$h(\tau_x)=\tau_x$$. It doesn't influence the final value I want. I just want to make this function look better.

Here is what I tried

In[85]:= h[Subscript[\[Tau], x]_]:=Subscript[\[Tau], x]
In[88]:= h[4]
Out[88]= 1
In[89]:= h[x_]:=x
In[90]:= h[4]
Out[90]= 4
In[91]:= <<Notation
In[92]:= Symbolize[Subscript[\[Tau], x]]
In[93]:= h[Subscript[\[Tau], x]_]:=Subscript[\[Tau], x]
In[94]:= h[4]
Out[94]= Subscript[\[Tau], x]


Here is the image:

• I recommend you don't try this. Jun 12, 2023 at 16:21
• @lericr hhh, my sanity tells me so, too. But our foumulas does look better if such a thing is possible. And it would be more convenient if the variables in Mathematica corresponde to the original math form. Jun 12, 2023 at 16:26
• First, that's a subjective opinion, and one that I disagree with. I find all of the extra semantic-free decorations to be very distracting, less visually pleasing, and not at all convenient. But subjective opinions are subjective opinions. Jun 12, 2023 at 16:32
• Second, Mathematica suffers a bit from its own success. People expect things from it that would be so infeasibe in other languages as to not even come under consideration. Remember, at the end of the day Wolfram Language is a programming language (or a computational language if you prefer). It needs some constraints to be able to parse and compute deterministically. Thus the way pattern matching works for arguments. It has its own sort of elegance, and it's just inappropriate to compare it to free form mathematical notation that can be explained on-the-fly (and needn't even be unambiguous). Jun 12, 2023 at 16:36
• @lericr Thanks! I got your idea and I won't waste more time on this. Jun 12, 2023 at 16:43

Even though this is – as already mentioned by @lericr in the comments and many times across this SE – generally not advised, I still want you to explain why your code does not work.

After symbolizing Subscript[τ, x] and defining h, take a look at the InputForm (!) of downvalues assigned to h:

Clear[h];
h[Subscript[τ, x] _] := Subscript[τ, x]
DownValues[h] // InputForm
(* {HoldPattern[h[τ⎵Subscript⎵x*_]] :> τ⎵Subscript⎵x} *)


Do you see the problem? Subscript[τ, x] _ got parsed as Subscript[τ, x]*_ – multiplication between Subscript[τ, x] and "something". That is why the following code returns a somehow "unexpected" result:

h[2 Subscript[τ, x]]
(* Subscript[τ, x] *)


Therefore, you need to force the parser to understand Subscript[τ, x]_ as Pattern[τ, Blank[]]. The cleanest way to do this is by using a colon:

Clear[h];
h[Subscript[τ, x] : _] := Subscript[τ, x]
h[4]
(* 4 *)

• Thank you! But where is the semicolon you mentioned? There is only colon in h[Subscript[τ, x] : _] := Subscript[τ, x]. And there is error when I run it: Syntax::sntxf: "h[" cannot be followed by "Subscript[\[Tau],x]:_]".` Is there any error? Sorry if I misunderstood you, I'm a big noob for Mathematica. Jun 13, 2023 at 11:32
• Could you show your complete code if convenient? I think I can figure out where I made a mistake by checking your code. Jun 13, 2023 at 11:40
• @XiangyuCui, ah, I meant colon instead of semicolon, sorry for the confusion. Note that you still have to first symbolize your variable, your code should look like this. Jun 15, 2023 at 21:02
• Sure, thank you! Jun 17, 2023 at 8:30