2

I have a macro that is repeatedly called in my document

\newcommand\foo[1]{Process #1}
\newcommand\var[2]{Do something with #1 and #2, and then call '\foo{#2}', and again '\foo{#2}', and once again '\foo{#2}'}

The macro foo is expanded many times inside var with the same argument, one may think that passing the argument is just redundant, and could be possibly avoided.

Question: Is it possible to have a version of foo such that when var is defined as

\newcommand\var[2]{Do something with #1 and #2, and then call '\foo', and again '\foo', and once again '\foo'}

would lead to the result of the first example?

MWE

\documentclass[]{article}

\newcommand\foo[1]{Process #1}
\newcommand\var[2]{Do something with #1 and #2, and then call '\foo{#2}', and again '\foo{#2}', and once again '\foo{#2}'}

\begin{document}

\var{x}{y}

\end{document}

And this is the output enter image description here

I would like to have the same result, after defining var as

\newcommand\var[2]{Do something with #1 and #2, and then call '\foo', and again '\foo', and once again '\foo'}
  • Could you please provide a small minimum working example, for us to better see what you are trying to accomplish? – Steven B. Segletes Apr 30 '18 at 13:40
  • @StevenB.Segletes Absolutely, I just added it – caverac Apr 30 '18 at 13:47
  • @marmot Do you mean something like \newcommand\foo{Process \noexpand{#1}}? It doesn't work, right? I've try a bunch of combinations of edefs and \expandafters but I cannot make it work – caverac Apr 30 '18 at 13:48
  • 2
    Something close: \renewcommand\var[2]{\def\foo{Process #2}Do something with #1 and #2, and then call '\foo', and again '\foo', and once again '\foo'} – Steven B. Segletes Apr 30 '18 at 14:14
  • 1
    @StevenB.Segletes I see, thanks for your time. If you add these comments as an answer I will gladly accept it – caverac Apr 30 '18 at 23:31
1

The OP asked me to convert comments into an answer. None of which give exactly what the OP was hoping for.

The problem is that I see no way to get #2 or its equivalent value into \foo without doing something with #2 to save it somewhere. But that can only occur after \var is invoked or via some pre-\var macro invocation.

I show both these alternatives in the MWE below.

\documentclass[]{article}
\begin{document}
Original approach of OP

\newcommand\foo[1]{Process #1}
\newcommand\var[2]{Do something with #1 and #2, and then call '\foo{#2}', and again '\foo{#2}', and once again '\foo{#2}'}

\var{x}{y}

\hrulefill

Requires a leading \verb|\def| inside of \verb|\var|

\renewcommand\var[2]{\def\foo{Process #2}Do something with #1 and #2, and then call '\foo', and again '\foo', and once again '\foo'}

\var{x}{y}

\hrulefill

Defines \verb|\var| as desired, but requires invocation of \verb|\setvar|

\renewcommand\var[2]{Do something with #1 and #2, and then call '\foo', and again '\foo', and once again '\foo'}

\newcommand\setvar[2]{\def\foo{Process #2}\var{#1}{#2}}

\setvar{x}{y}
\end{document}

enter image description here

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.