I was wondering how I can redefine an existing command by an own command using futurelet to determine the number and types of parameters? Basically what I wanted to do is similar to
\renewcommand{\log}[2][]{\mathrm{log}_{#1}\left( #2 \right)}
but with the additional option that the user can use the command without argument, i.e. write \log 5
to obtain an output log 5, since some people prefer writing the logarithm without brackets.
Here's what I tried
%logarithm
\newcommand{\logRM}{\mathrm{log}} %log operator
\newcommand{\logOneArg}[1]{\logRM\left(#1\right)} %\logRM()
\newcommand{\logTwoArgs}[2][]{\logRM_{#1}\left(#2\right)} %\logRM_basis()
\DeclareRobustCommand\log{\futurelet\logNext\logCheck}
\def\logCheck{%
\ifx\bgroup\logNext \expandafter\logOneArg %if \log is followed by {
\else%
\ifx[\logNext \expandafter\logTwoArgs %if \log is followed by [
\else%
\expandafter\logRM %otherwise
\fi%
\fi%
}
I encountered two problems. One of which was that the optional argument did not act as I supposed it would, instead, \log[2]{8}
gave me an output log()[2]8. The other problem was that I got an error message because \log
was already defined. I tried to circumvent the problem by denoting above command by \xlog
and then trying one of the following, but neither worked.
\def\log{\xlog}
\let\log\xlog
\renewcommand{\log}{\xlog}
\edit:
Maybe it was not clever to formulate two questions in one problem. So here is the information for the no argument case I would like to have. If we leave out the optional parameter, then
%logarithm
\newcommand{\logRM}{\mathrm{log}} %log operator
\newcommand{\logOneArg}[1]{\logRM\left(#1\right)} %\logRM()
\DeclareRobustCommand\xlog{\futurelet\logNext\logCheck}
\def\logCheck{%
\ifx\bgroup\logNext \expandafter\logOneArg %if \log is followed by {
\else%
\expandafter\logRM %otherwise
\fi%
}
defines a command that will have no brackets if I don't have any arguments, i.e. I can write \xlog^+(x)
for the positive part of the logarithm and \xlog{\frac{x+1}{x}}
to have brackets of varying size around a more complicated expression.
Now one question is, how can I call this function \log even though \log is already defined? the other question is how to expand this macro to allow an additional optional argument, e.g. the basis of the logarithm, since it doesn't seem to work the way I posted in my first post.
The way (I hope) this should work is lined out in my edited post above. It works with futurelet to see if there is any argument at all, if we leave out the case with the optional argument like in my edited question, then
\xlog^+ : [1, \infty) \to \mathbb{R}_+
\xlog{\frac{x}{x+1}}
both produce the output we expect (which is log^+ : [1, \infty) \to R
or log(x/(x+1))
respectively). I guess what I'm trying to say is that I'm looking to write this function in a way that latex does not wait for an argument if I don't write braces, because there won't be any. And sorry about my mistake with the brackets, I'm no native speaker, but at least I know now.
\edit:
Thought about your comment regarding the spacings and realized that the better way to generalize the \log
command was of course to use \let
, so we keep all the functionality of the old command and get the new ones, i.e. with something like this
\let\logRM\log %log operator
\newcommand{\logOneArg}[1]{\logRM\left(#1\right)} %\logRM()
%\newcommand{\logTwoArgs}[2][]{\logRM_{#1}\left(#2\right)} %\logRM_basis()
\DeclareRobustCommand\xlog{\futurelet\logNext\logCheck} %look what next character is
\def\logCheck{%
\ifx\bgroup\logNext \expandafter\logOneArg% %if \log is followed by {
\else%
%\ifx[\logNext \expandafter\logTwoArgs% %if \log is followed by [
%\else%
\expandafter\logRM %otherwise
%\fi%
\fi%
}
\renewcommand{\log}{\xlog}
Now if \log
is not followed by any braces, it behaves just like the initial \log
, thus if we want to have parentheses with nath, we can still do that. On the other hand if we write \log{expression}
we get the expression in braces. Now the only question remaining is how to extend that to take an optional argument like the base of the logarithm.
\log{5}
and\log 5
will both work. Also have a look at thexparse
package.