2

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

\newcommand{\mymacro}[4]{%
something{#1}
...
}%

and is called as

\mymacro{\includegraphics{..}}{..}{..}{..}

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

\newcommand{\mymacro}[4]{%
something{#1}
\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.

7
  • Would you be interested in a LuaLaTeX-based solution?
    – Mico
    Aug 7, 2018 at 11:43
  • I've never used it, but it should be compatible with a tikzexternalize execution
    – Acorbe
    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
    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
    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. Aug 7, 2018 at 12:33

1 Answer 1

4

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

\documentclass{article}
\usepackage[T1]{fontenc}
\newcommand\mymacro[4]{%
  \parsefurther#1\endparse%
}
\def\parsefurther#1#2\endparse{The argument of \detokenize{#1} is 
  ``\detokenize{#2}''.\\
  \detectoptional{#2}[#2]\enddetect%
}
\def\detectoptional#1[#2]#3\enddetect{%
  \ifx\relax#3\relax Optional: none.  Primary: \detokenize{#1}.
  \else \extractoptional#1%
  \fi
}
\newcommand\extractoptional[2][]{Optional: \detokenize{#1}. 
                                 Primary: \detokenize{#2}.}
\begin{document}
\mymacro{\includegraphics{this_is_the_argument}}{..}{..}{..}

\mymacro{\specialincludegraphics[options]{this_is_the_argument}}{..}{..}{..}
\end{document}

enter image description here

4
  • let me try.... 1 minute
    – Acorbe
    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
    Aug 7, 2018 at 12:23
  • @Acorbe Thanks. I added a few bells and whistles. Aug 7, 2018 at 12:23
  • :) it's a great technical insight..too bad I can upvote only once.. thanks :)
    – Acorbe
    Aug 7, 2018 at 12:28

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.