4

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.

1

2 Answers 2

8

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.

2
  • Thank you, but can we handle two parameters together by any chance: something like: \newcommand{\divby}[1][2]{\frac{#1}{#2}}?
    – Mathmath
    Feb 21, 2013 at 23:55
  • 1
    The optional argument of \newcommand (the number in square brackets) determines the number of arguments of the new command. So, in your case: \newcommand{\divby}[2]{\frac{#1}{#2}} Feb 22, 2013 at 1:33
1

You can also use the "old" macro for the fractions (used in plain TeX):

{a\over b}

that produces the same result of

\frac{a}{b}

For integrals, I suggest you the following macro:

\def\intx#1{\int {#1}\,dx}

that produces the integral of #1 with its differential (where x is your variable)

1
  • 5
    Using \over is best avoided in LaTeX, and also \def is discouraged in document preambles.
    – egreg
    Feb 22, 2013 at 9:47

You must log in to answer this question.

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