2

I've searched for but not found an answer to this.

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>}}
1
  • 1
    Try something like \def\newnote#1{\edef\csname #1\endcsname\inlineImage{-1.5}{#1}}
    – Eddy_Em
    Aug 13, 2013 at 19:01

2 Answers 2

0

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.

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

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.

3
  • 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
  • It may be noteworthy that you can also do \expandafter\newcommand\csname #1\endcsname to still have the possibilities to use \newcommand syntax. Aug 23, 2013 at 6:11
  • @Qrrbrbirlbel, \newcommand is overhead for such simple task.
    – Eddy_Em
    Aug 24, 2013 at 21:35

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.