Expand after all that stuff?
This is provided by etextools
as \expandnext
:
\usepackage{etextools}
\expandnext{\somecommand{Some Argument}}{\secondarg}
But if you only like to expand the second argument once you can use a double argument swapping, a trick I use in some of my packages:
\documentclass{article}
%% Implementation
\def\myswap#1#2{#2{#1}}
\def\expandafterallthat#1#2{%
\expandafter\myswap\expandafter{#2}{#1}%
}
%%%%%%%%%%%%%%%%
% Test case:
%
\makeatletter
\def\somecommand#1#2{%
\def\first{#1}%
\@onelevel@sanitize\first
\par\texttt{First argument: \first}%
\def\second{#2}%
\@onelevel@sanitize\second
\par\texttt{Second argument: \second}%
}
\makeatother
\def\secondarg{\empty Some stuff\empty}
\begin{document}
\expandafterallthat{\somecommand{\empty Some Argument\empty}}{\secondarg}
\end{document}
This correctly prints: (the \empty
s are to see if the argument got expanded further)
first argument: \empty Some Argument\empty
Second argument: \empty Some stuff\empty
Written without a package
\documentclass{article}
\newcommand\expandafterallthat[2]{%
\expandafter\expandafterallthataux\expandafter{\romannumeral -`0#2 }{#1}%
}
\newcommand\expandafterallthataux[2]{#2{#1}}
\newcommand\somecommand[2]{\detokenize{#1:::#2}}
\newcommand\secondarg{\thirdarg}
\newcommand\thirdarg{\fourtharg}
\newcommand\fourtharg{example}
\begin{document}
\expandafterallthat{\somecommand{Some Argument}}{\secondarg}
\end{document}
will multiply-expand input. (You don't say whether to expand #2
once or several times.)
With the latest engine releases (MiKTeX or upcoming TL'19), one can use \expanded
:
\documentclass{article}
\newcommand\expandafterallthat[2]{%
\expanded{\unexpanded{#1}{#2}}}
\newcommand\somecommand[2]{\detokenize{#1:::#2}}
\newcommand\secondarg{\thirdarg}
\newcommand\thirdarg{\fourtharg}
\newcommand\fourtharg{example}
\begin{document}
\expandafterallthat{\somecommand{Some Argument}}{\secondarg}
\end{document}
(\expanded
has always been in LuaTeX, so you can test there even without the latest updated engines.)
Since Joseph did not mention expl3
yet, let me do that: the LaTeX3 bundle includes a module for expansion control, l3expan
. In particular, this includes functions to expand arguments of a command, of the form \exp_args:N...
where the dots are letters corresponding to how the various arguments should be treated.
Many letters are in use: n
for a brace group which should be passed with no modification, o
to expand once, f
to expand fully from the left, stopping at the first non-expandable token, x
to expand fully throughout, like in an \edef
. There is also N
for a single non-braced token which should be passed as is, c
for text that should be made into a control sequence, then treated as an N
argument, and V
and v
for variables (see the l3expan documentation for details).
Here, you want to expand the second argument once, and leave the first unchanged, so the variant you need is \exp_args:Nno
: leave the function unchanged (N
), the first argument, braced, unchanged (n
), and the second expanded once (o
).
\newcommand{\foo}[2]{\showtokens{#1... #2}}
\newcommand{\sometext}{Some text.}
\RequirePackage{expl3}
\ExplSyntaxOn
\exp_args:Nno \foo {\error} {\sometext}
\ExplSyntaxOff