Dav*_*d Z 4 latex definition tex
我正在尝试创建带有两个参数的LaTeX命令,其中之一是可选的。通常我会这样做
\newcommand{\whatever}[2][default]{first #1 second #2}
Run Code Online (Sandbox Code Playgroud)
其中default,第一个参数的默认值。但是对于此命令,我希望将第二个参数的值用作第一个参数的默认值-也就是说,
\whatever{blah}
\whatever{foo}
\whatever[lol]{rofl}
Run Code Online (Sandbox Code Playgroud)
相当于
\whatever[blah]{blah}
\whatever[foo]{foo}
\whatever[lol]{rofl}
Run Code Online (Sandbox Code Playgroud)
有谁知道如何做到这一点?如有必要,我可以使用普通的TeX。
尽管LaTeX内核没有得到广泛使用,但它具有一种内置的方法。这是一个例子:
\makeatletter
\newcommand\@foo[2][]{[1: #1, 2: #2.] }
\newcommand\foo{\@dblarg\@foo}
\makeatother
\foo{same}
\foo[hello]{world}
Run Code Online (Sandbox Code Playgroud)
显然,\makeatletter如果您在.sty或.cls文件中执行此操作,则可以删除这些命令。