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
    Welcome. Did you try to use ##1? – TeXnician Feb 23 '18 at 5:27
2

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

  • Thanks - solved... after I corrected my own example above. – pancake Feb 26 '18 at 2:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.