I have a macro (here \mymacro) that receives a command as one of the inputs (specifically a command of type \includegraphics{..}).

The macro reads as


and is called as


I am now in the necessity of parse the the content of the first argument as a string to extract content (i.e. parse the token \includegraphics{..} itself to extract file name and modifiers). I will use xstring parsing commands for the purpose.

In other words, I'd like to turn \mymacro into something like

\stringparsingcommands{ \as__a__string{#1}   }

My question is:

how to build such \as__a__string command? It needs to prevent #1 to be expanded as token and convert it into string.

I've been trying with \verbtocs from the package \xstring, yet with no success.

Any help is greatly appreciated.

  • Would you be interested in a LuaLaTeX-based solution?
    – Mico
    Commented Aug 7, 2018 at 11:43
  • I've never used it, but it should be compatible with a tikzexternalize execution
    – Acorbe
    Commented Aug 7, 2018 at 11:44
  • It would be helpful if you provided a bit more background information as to what's supposed to happen with the first argument of \mymacro. E.g., will it always be of the form \includegraphics{<some string>}, i.e., will it always start with \includegraphics and end with some material (a "string"?) that's encased in matching curly braces? If this is not the actual structure of the first argument, do please provide more in-depth information as to what should be expected.
    – Mico
    Commented Aug 7, 2018 at 11:53
  • I can write the specific case, I don't think it will help, though - the string parsing or the specifics of the \includegraphics call are irrelevant, I believe. I think the point is: from a call \mymacro{\command{string}} can one extract and e.g. display in the manuscript the string \command{string} as is (i.e. not expanded)?
    – Acorbe
    Commented Aug 7, 2018 at 11:59
  • 1
    why do you need to do this though, \includegraphics (or rather keyval) is already going to parse the argument and is already going to and report the filename and various options, you can use the result of that parse. Commented Aug 7, 2018 at 12:33

1 Answer 1


Do you mean something like this? EDITED to further extract optional and primary argument from 1st argument.

\def\parsefurther#1#2\endparse{The argument of \detokenize{#1} is 
  \ifx\relax#3\relax Optional: none.  Primary: \detokenize{#1}.
  \else \extractoptional#1%
\newcommand\extractoptional[2][]{Optional: \detokenize{#1}. 
                                 Primary: \detokenize{#2}.}


enter image description here

  • let me try.... 1 minute
    – Acorbe
    Commented Aug 7, 2018 at 12:14
  • !! Nice!! Thanks.. adapted to the purpose :) ... needed to extract the right filenames to properly tikzexternalize images post-processed with tikz.
    – Acorbe
    Commented Aug 7, 2018 at 12:23
  • @Acorbe Thanks. I added a few bells and whistles. Commented Aug 7, 2018 at 12:23
  • :) it's a great technical insight..too bad I can upvote only once.. thanks :)
    – Acorbe
    Commented Aug 7, 2018 at 12:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .