LaTeX: how to use the required default arg argument for optional?

I am trying to create a LaTeX command with two arguments, one of which is optional. I usually do it like

\newcommand{\whatever}[2][default]{first #1 second #2}

where defaultis the default value for the first argument. But for this command, I want the value of the second argument to be used as the default value for the first argument - that is, I want

\whatever{blah}
\whatever{foo}
\whatever[lol]{rofl}

equivalently

\whatever[blah]{blah}
\whatever[foo]{foo}
\whatever[lol]{rofl}

Does anyone know how to do this? If necessary, I can go down to a simple TeX.

+3
source share
3 answers

The LaTeX kernel has a built-in way to do this, although it is not widely used. Here is an example:

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

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

, \makeatletter , .sty .cls.

+4

:

\usepackage{ifthen}

...

\newcommand{\whatever}[2][uniquenonesense]{
   ... 
   \ifthenelse{\equal{#2}{uniquenonesense}}{#1}{#2}
   ...
}

, - , , .

uniquenonesense .

+2
    \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.
0
source

Source: https://habr.com/ru/post/1705751/


All Articles