0

If I define a newcommand within another newcommand, and both the inner and outer have arguments, how do I access the arguments of the inner newcommand? In the example below, where I have '***' I want to access the '#1' argument that is passed into the inner newcommand.

\newcommand{\defTerm}[3]{%
    \expandafter\newcommand\csname #1\endcsname[1][]{%
        \ifthenelse{\equal{#***}{}}%
            {\textbf{\tooltip{#2}{\begin{varwidth}{10cm}#3\end{varwidth}}}}%
            {\textbf{\tooltip{#***}{\begin{varwidth}{10cm}#3\end{varwidth}}}}%
    }%
}

An example usage of this is:

\defTerm{ETNZ}{Emirates Team New Zealand}{The winner of the 35th America's Cup}

then in the text:

\ETNZ was the winner in Bermuda. \ETNZ[They] beat BMW Oracle Racing.

UPDATE

Corrected \ETNZ{They} to \ETNZ[They] in my example.

1
  • 1
    Welcome. Did you try to use ##1?
    – TeXnician
    Commented Feb 23, 2018 at 5:27

1 Answer 1

5

The # is doubled in each level of nested definitions:

\newcommand{\defTerm}[3]{%
    \expandafter\newcommand\csname #1\endcsname[1][]{%
        \ifthenelse{\equal{##1}{}}%
            {\textbf{\tooltip{#2}{\begin{varwidth}{10cm}#3\end{varwidth}}}}%
            {\textbf{\tooltip{##1}{\begin{varwidth}{10cm}#3\end{varwidth}}}}%
    }%
}

Another example:

\documentclass{article}
\begin{document}

\newcommand*{\foo}[3]{%
  foo=(#1, #2, #3)\par
  \def\foobar##1##2{%
    foo=(#1, #2, #3) foobar=(##1, ##2)\par
    \newcommand{\foobarbaz}[1]{%
      foo=(#1, #2, #3) foobar=(##1, ##2) foobarbaz=####1\par
    }%
    \foobarbaz{##2}%
  }%
  \foobar{#2}{#3}%
}

\foo{A}{B}{C}

\end{document}

Result

1
  • Thanks - solved... after I corrected my own example above.
    – pancake
    Commented Feb 26, 2018 at 2:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .