I want to define a new command (to be used only in mathmode), the execution of which depends on the input given to it. Particularly, I want to define \F as


if the first character in its input is ( , and as


if the first character in its input is not ( .

As an example, I would like \F{x} to output F(x) and \F{(x)} to output F(x).

I have no idea how to go about this.

2 Answers 2


No packages required.

$\F{(x)}$ $\F{(G(x))}$

$\F{x}$ $\F{G(x)}$ 

enter image description here


You can achieve this using the following code:

    \ifthenelse{\equal{(}{\firstletter} \AND \equal{)}{\lastletter}}{F#1}{F(#1)}%

It basically checks what the first and last characters of the argument are, and if those are '(' and ')', it will output the argument without extra parentheses.

  • Since I cannot accept two answers, I've upvoted your answer. I accepted the other answer since it doesn't require a package. Thank you though. Jul 21, 2020 at 16:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .