# How can I use a macro as the (main) argument to \includegraphics

Disclaimer: I have tried to find an anser already, but everything I've found refers to the optional argument of \includegraphics or to other macros with their specific implementations.

I'm working on a (LuaLaTeX) package that includes images generated with an external program.

\lily{ c' }


will wrap the content c' in a proper document, invoke the LilyPond score writer to produce a score and then include the resulting PDF in the .tex document (using something like tex.print('\includegraphics{<resulting-file-hash>}')

In order to make it possible for secondary packages to use the generated results I implemented an option raw-pdf that instead of issueing the \includegraphics simply prints the generated hash back to the .tex document (tex.sprint(<generated-filename>))

\lily[raw-pdf]{ c' }


works correctly and will print the generated filename in the compiled .tex document.

But when I try to do

\includegraphics{\lily[raw-pdf]{ c' }}


! TeX capacity exceeded, sorry [input stack size=5000].
<argument> \@protected@testopt \lily
\\lily {}[raw-pdf]{ c' }
l.19 ...dafter\includegraphics{\lily[raw-pdf]{ c' }}


I assume this has something to do with expansion, but I'm completely at a loss here as to what is actually happening. I was told to try

\expandafter\includegraphics{\lily[raw-pdf]{ c' }}


but that doesn't help.

Here's a full minimal example

\documentclass{scrartcl}
\usepackage{lyluatex}
\begin{document}
\includegraphics{\lily[raw-pdf]{ c' }}
\end{document}


Note that the lyluatex package must be used from https://github.com/jperon/lyluatex and not the version already in TeXLive!

• As your command seems to have an optional argument it is probably not expandable. – Ulrike Fischer Mar 2 '18 at 7:36
• Maybe you should create two different macros, one for raw and the other which uses tex.print('\includegraphics{...}'). This way you might be able to create a fully expandable raw version. – Skillmon Mar 2 '18 at 8:13
• The problem is that in reality the \lily command will have to accept more optional arguments from the document authors – uli_1973 Mar 2 '18 at 8:38
• @uli_1973 Can you please fully specify the syntax for \lily? – egreg Mar 2 '18 at 9:11
• This is difficult because the code is distributed over a number of places in the package. But David Carlisle's answer led us on the right track. – uli_1973 Mar 2 '18 at 10:23

you can use macros that expand to a name in the argument but not arbitrary definitions, your example is like

 \includegraphics{\def\foo{aaa}\def\zz{bbb}\foo\zz}


which does not work.

What does work is

  \def\foo{aaa}\def\zz{bbb}\includegraphics{\foo\zz}


So you need to arrange that the macros that you use return the name in some macro which can then be used with \includegraphics.

In order to make it possible for secondary packages to use the generated results I implemented an option raw-pdf that instead of issueing the \includegraphics simply prints the generated hash back to the .tex document (tex.sprint(<generated-filename>))

you probably want to make \lily[raw-pdf]{..} define a macro say \lilyrawpdf then you can do

\lily[raw-pdf]{...}\includegraphics{\lilyrawpdf}


Note that this is not specific to \includegraphics if you look at your own \lily command it is likely that

\lily{\def\foo{c}\foo}


does not work like

\lily{c}


or

\def\foo{c}\lily{\foo}