Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

What is the correct way to alias a command to another one?

\newcommand*{\keyword}{\emph}

or:

\newcommand*{\keyword}[1]{\emph{#1}}

Apart from the fact that the second is more explicit, is there any real differences?

share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

The difference will arise when the aliased command takes an optional argument.

\newcommand*\keyword{\othercommand}

will be able to handle any possible optional arguments. But

\newcommand*\keyword[1]{\othercommand{#1}}

will only be able to handle the situation where \othercommand is called without an optional argument.

In the case of \emph, this is not an issue as there is no optional argument to be taken there. So either approach should work fine.

When you are just strictly aliasing, my personal opinion would be to use the first

\newcommand*\keyword{\othercommand}

In a situation where I'm doing something more than just aliasing, I would use

\newcommand*\keyword[1]{\othercommand{#1}}

Purely for example's sake,

\newcommand*\keyword[1]{`\emph{#1}'}

(though others will most likely object that the quotes are unnecessary to \emph I only mean this as an illustration of what "more than just aliasing" might mean.)

Letting

Since this has come up in the comments, I'll mention a few things about \letting a command.

Sometimes you want to change the default behavior of a LaTeX macro. For example, suppose there is a macro defined as

\newcommand*\somecommand[1]{\emph{#1}}

but you would like it to behave differently. Then you could simply rewrite it as follows:

\renewcommand*\somecommand[1]{\textbf{#1}}

But then you might run into a situation where you would really like to have the original definition. In that case, before renewing the command, you could do something like

\let\oldsomecommand\somecommand
\renewcommand*\somecommand[1]{\textbf{#1}}

Now \somecommand will behave as you wish, but you also have \oldsomecommand as an available command to give you the old behavior.

In a nut shell, by writing \newcommand\keyword{\emph}, \keyword will expand to \emph. By writing \let\keyword\emph, \keyword will expand, not to \emph, but to the definition of \emph.

However, \let is not always able to capture the full power of a command that's been blessed/cursed with optional arguments. There are many posts on this site about how to handle such situations so I'll point you to just a few here:

share|improve this answer
    
Why not \letting it? –  percusse Mar 22 at 15:58
    
@percusse True enough. But a \let command does not always handle optional arguments correctly considering the different means of implementing optional arguments. –  A.Ellett Mar 22 at 15:59
1  
@percusse Because \let is not a documented command in the LaTeX manual. –  egreg Mar 22 at 16:01
    
@egreg Does this mean, that one shouldn't have \let in his document at all? –  Zii8roZi Mar 22 at 16:08
1  
@shilluc, I'm certain that's not at all what egreg meant. I suspect it's a bit more tongue in cheek than anything else. –  A.Ellett Mar 22 at 16:09
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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