GeneralCalling macros with arguments - using \csname

LaTeX specific issues not fitting into one of the other forums of this category.
gkamel
Posts: 25
Joined: Thu Oct 30, 2008 1:53 pm

Calling macros with arguments - using \csname

Post by gkamel »

Thanks for the replies.

I have removed Numberstring from the macro altogether for the time being, and just defined \temp as follows:

Code: Select all

\def\temp{#2}
where #2 is just a simple string, in this case "MacroNumber". But when I call

Code: Select all

\CheckCountAssign{SomeMacro}{MacroNumber}
\MacroNumber{world}
MiKTex complains that \MacroNumber is undefined. So there seems to be a problem with:

Code: Select all

\expandafter\def\expandafter\csname\temp\endcsname{\csname#1\endcsname}
The following is a minimum (non-)working example not containing \Numberstring:

Code: Select all

\documentclass{article}

\usepackage{etoolbox}

\begin{document}

\newcommand*\CheckCountAssign[2]{
  \ifcsdef{#1}{
    \stepcounter{#2}
    \def\temp{#2}
    % everything works fine upto this point.
    \expandafter\def\expandafter\csname\temp\endcsname{\csname#1\endcsname}
  }{}
}


\def\SomeMacro#1{Hello #1!}
\newcounter{MacroNumber}
\CheckCountAssign{SomeMacro}{MacroNumber}
\MacroNumber{world}

\end{document}
I really appreciate everyone's help.

George
George Kamel

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

Calling macros with arguments - using \csname

Post by phi »

The expansion is wrong, you have to expand \csname, not \temp, otherwise your command redefines \csname.

Code: Select all

\expandafter\def\csname\temp\endcsname{\csname#1\endcsname}
If you are not sure about things like that, simply use the commands from etoolbox:

Code: Select all

\csdef{\temp}{\csuse{#1}}
gkamel
Posts: 25
Joined: Thu Oct 30, 2008 1:53 pm

Re: Calling macros with arguments - using \csname

Post by gkamel »

Thanks a lot phi, it works okay now - I'll definitely take advantage of the etoolbox package in future.

Nicola, your suggestion worked a treat, thanks :)

George
George Kamel
Post Reply