# Expandafter, but with full expansion?

Problem. I want to write a macro, that effectively replaces

\MyMacro\SomeOtherMacro{arg1}{arg2}StuffAfter


by

SomeOtherMacrosExpansionWithArg1Arg2\MyOtherMacro StuffAfter


i.e. essentially \expandafter, but with full macroexpansion. Is this possible?

Purpose. I want to read parts of a file conditionally, while preserving SyncTeX informaton, for example by processing only the lines N through M of a document.

Skipping of the first N lines is possible with a construct like

\def\my@skip#1{%
\ifthen{\the\inputlineno < \N}{%
\my@skip
}{%
(what goes here?)
}%
}


but I don't know how to continue ignoring tokens after the M th line.

Similar problems are typically solved by collecting the tokens into a macro, but this is exactly what destroys SyncTeX information, so it is not viable.

The \lstinputlisting package has firstline and lastline options, that have the behavior I want, but by reading the file verbatim, they don't have to deal with macro expansion.

• You can fully expand with \edef, something like this: \edef\FullExpansion{\SomeOtherMacro{arg1}{arg2}}% \expandafter\MyMacro\FullExpansion StuffAfter. A complete example of what you are trying to achieve would make it easier for a more concrete answer. – Phelype Oleinik Jun 27 '18 at 20:52
• If I understand it correctly, do you want to insert \MyOtherMacro after \SomeOtherMacro has absorbed its arguments? If it's not just a toy problem, please add an example where this should be useful. – egreg Jun 27 '18 at 21:06
• \def\MyMacro#1#2#3{#1#2#3\MyOtherMacro}...\MyMacro\SomeOtherMacro{arg1}{arg2}StuffAfter does what you ask, but is that the answer you want? – David Carlisle Jun 27 '18 at 21:12
• @egreg Added an explanation of the intent, where it came up. – kdb Jun 28 '18 at 11:43
• @DavidCarlisle Not applicable, because it requires knowledge of the number of arguments the macro consumes. I need a solution that works regardless of the subsequent token (i.e. for all command sequences, and also for letters etc.). ... Which may actually mean, it is impossible (e.g. with superscript ^ tokens)? – kdb Jun 28 '18 at 11:45