[Tex/LaTex] How to define new command for commands like \frac{} or \int{}^{}

macrosmath-mode

We know that we could define a new command for boldfaced or calligraphic C, for example I can define \newcommand{\Cx}{\mathbb{C}}, which will produce the boldfaced C, i.e. $\mathbb{C}$ for me. But how do I define new command for say more complicated commands, e.g. \frac{}{} for getting fractions or say \int{}^{} for definite integrals? I tried defining \newcommand{\fr(,)}{\frac{}{}}, but it is not working. It seems to me there should be a correct way to define it.

Best Answer

You can define a command with parameters:

\newcommand{\divbytwo}[1]{\frac{#1}{2}}

The #1 is the placeholder for the first parameter to the macro: \divbytwo{3} will expand to \frac{3}{2}.

Edit: incorporating comments from the OP and @SašoŽivanović asking/answering about macros with more than one argument -

\newcommand{\divby}[2]{\frac{#1}{#2}}

will do the job. The [2] tells TeX how many parameters there will be. They're put in places #1 and #2. Of course in this simple example\divby is just frac so you gain little by defining it.