LaTeX:如何使用必需的参数作为可选参数的默认值?

LaTeX:如何使用必需的参数作为可选参数的默认值?,latex,definition,tex,Latex,Definition,Tex,我试图创建一个带有两个参数的LaTeX命令,其中一个是可选的。通常我会像你一样做这件事 \newcommand{\whatever}[2][default]{first #1 second #2} 其中,default是第一个参数的默认值。但是对于这个命令,我希望第二个参数的值用作第一个参数的默认值,也就是说,我希望 \whatever{blah} \whatever{foo} \whatever[lol]{rofl} 相当于 \whatever[blah]{blah} \whatever[

我试图创建一个带有两个参数的LaTeX命令,其中一个是可选的。通常我会像你一样做这件事

\newcommand{\whatever}[2][default]{first #1 second #2}
其中,
default
是第一个参数的默认值。但是对于这个命令,我希望第二个参数的值用作第一个参数的默认值,也就是说,我希望

\whatever{blah}
\whatever{foo}
\whatever[lol]{rofl}
相当于

\whatever[blah]{blah}
\whatever[foo]{foo}
\whatever[lol]{rofl}
有人知道怎么做吗?如果需要的话,我可以选择纯文本。

一个丑陋的黑客:

\usepackage{ifthen}

...

\newcommand{\whatever}[2][uniquenonesense]{
   ... 
   \ifthenelse{\equal{#2}{uniquenonesense}}{#1}{#2}
   ...
}
想必你想要更干净的东西,但这就是我想要的


根据您的语义,
uniquencesense
可能是空的。

LaTeX内核有一个内置的方法来实现这一点,尽管它没有被广泛使用。下面是一个例子:

\makeatletter
\newcommand\@foo[2][]{[1: #1, 2: #2.] }
\newcommand\foo{\@dblarg\@foo}
\makeatother

\foo{same}
\foo[hello]{world}

显然,如果在
.sty
.cls
文件中执行此操作,则可以删除
\makeatletter
命令。

对我有效;-)当然,干净会很好,但我真的只想要能完成工作的东西。与我读过的许多其他乳胶代码相比,这不是什么黑客行为……直接从马嘴里说出来总是最好的。
    \newcommand{\haha}[2]{\backslash whatever \left[ #1 \right] 
    \left[   #1     \right] }

    \begin{document}
    $\haha{blah}{blah}$\\
    $\haha{stupid}{Idiot}$
    \end{document} % This works very well.