# How can I replicate a command name as an argument?

I am using two layers of \newcommands to create symbols inline with text, using \includegraphics. (It's for music symbols.)

At present, I have this as the generic command:

\newcommand*{\inlineImage}[2]{\raisebox{#1pt}{\fbox{\includegraphics{__#2__}}}}


And then many commands of the form:

\newcommand*{\Eighth}{\inlineImage{0}{Eighth}}
\newcommand*{\Sixteenth}{\inlineImage{-1.5}{Sixteenth}}


This works, but it is cumbersome to type the same text twice during set up. Of course, I could employ a command with arguments in my document body, but I don't want to do that. Is there a way to replace the specific commands with something of the form:

\newcommand*{\Sixteenth}{\inlineImage{-1.5}{<thecommandname>}}

• Try something like \def\newnote#1{\edef\csname #1\endcsname\inlineImage{-1.5}{#1}} Aug 13, 2013 at 19:01

The etoolbox package provides the \csdef command:

\newcommand*{\inlineImage}[2]{\raisebox{#1pt}{\fbox{\includegraphics{__#2__}}}}
\newcommand*\defnote[2]{\csdef{#1}{\inlineImage{#2}{#1}}}


Then use

\defnote{Eighth}{0}
\defnote{Sixteenth}{-1.5}


to create the macros \Eighth and \Sixteenth as you wanted.

• LaTeX provides \@namedef. Aug 23, 2013 at 5:50

To make command from text use \csname .. \endcsname macros. For example, if you will write

\def\newnote#1{\expandafter\def\csname #1\endcsname{\inlineImage{-1.5}{#1}}}


then commands like

\newnote{Fourth}
\newnote{Eighth}
\newnote{Sixteenth}


will define notes \Fourth, \Eighth and \Sixteenth, which will insert appropriate images.

• Thanks Eddy_Em: this gets me a lot closer! However, part of the problem is that I need to adjust the raisebox variable. It was daft of me not to make that explicit with my example, which I have now corrected. How can I adapt the above answer to do this? This is beyond my abilities at the moment! Aug 22, 2013 at 23:11
• @Qrrbrbirlbel, \newcommand is overhead for such simple task. Aug 24, 2013 at 21:35