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
While a definitive answer can only come from the Stanford team involved in development of TeX, and from Professor Knuth in particular, I think we can see some possible reasons.
First, Knuth designed TeX primarily to solve a particular problem (typesetting The Art of Computer Programming). He made TeX sufficiently powerful to solve the typesetting problems he faced, plus the more general case he decided to address. However, he also kept TeX (almost) as simple as necessary to achieve this. While expandable macros are useful, they are not required to solve many issues.
Secondly, there are cases where an expandable approach would be at least potentially ambiguous. Bruno's \edef\foo{\def\foo{abc}}
is a good case. I'd say that here the expected result with an expandable \def
is that \foo
expands to nothing, but I'd also say this is not totally clear. There is the much more common case where you want something like
\begingroup
\edef\x{%
\endgroup
\def\noexpand\foo{\csname some-macro-to-fully-expand\endcsname}%
}
\x
which would be made more complex with expandable primitives.
The above example points to another grey area: what would happen about things like \begingroup
and more importantly \relax
. The fact that the later is a non-expandable no-op is often important in TeX programming. (Indeed, the fact that \numexpr
, etc., gobble an optional trailing \relax
is sometimes regarded as a bad thing.)
Finally, I suspect that ease of implementation is important. The approach of having separate expansion and execution steps makes the flow relatively easy to understand, and I also suspect to implement. An approach which mixes expansion and execution requires a more complex architecture. Here, we have to remember when Knuth was writing TeX, and the fact that programming ideas which we take for granted today were not necessarily applicable in the late 1970s. A fully-expandable approach would I suspect have made the code more complex and slower. The speed impact is one that was important when TeX was running on 'big' computers.
Best Answer
TeX is based on macros and macro expansion. When you type something like
\mbox{abc}
, you're telling TeX to perform a possibly very complicated sequence of commands.The case of
\mbox
is not really so complicated, because the definition iswhich basically means
When you find
\mbox
, look for an argument (which can contain\par
, because of\long
)Replace
\mbox{<argument>}
with\leavevmode\hbox{<argument>}
It's not ending here: also
\leavevmode
is a macroso TeX duly replaces
\leavevmode
with\unhbox\voidb@x
and the final input is equivalent to having written(in the case of
\mbox{abc}
). The road stops here, because\unhbox
and\hbox
are primitive commands that don't expand, but are passed to the next stage;\voidb@x
is just an alias for a number (precisely a box register index and that register contains a void box).The user is not concerned with these details about primitive commands: it's the programmer's responsibility to provide the user with a suite of commands for the common typesetting tasks.
A macro such as
\chapter
does much more than this. It checks for a possible*
(unnumbered chapter) or for an optional argument (the header entry) and absorbs the mandatory argument (the chapter title). Then it executes a\cleardoublepage
command for starting from a fresh page (an odd numbered one iftwoside
is in force), leaves some vertical space, steps the counter for the chapter number, typesets the chapter title, makes the annotation for the table of contents, leaves some other vertical space and sets things up so that the next paragraph is not indented. It does something more than this, actually, but all with a simpleinput. The big advantage is that a programmer can change every detail of the action of
\chapter
, so that different chapter styles can be produced without changing the user's syntax.Every time TeX finds some command defined with
\def
, it performs this expansion, that is, absorbing the arguments as declared in the definition and replacing the tokens so found with the replacement text of the macro. Then it starts anew trying to expand the first token it is presented with. If not a macro (or one of the expandable primitives) it performs some typesetting/bookkeeping action with it: typeset a word, step a counter, write a note in an auxiliary file and so on.One could write a book about macro expansion and TeX/LaTeX macros. I did, actually.
;)
You never saw
\def
? Well, pretty normal! You probably have been told that a personal command should be defined with\newcommand
and it's true! However,\newcommand
is itself a macro that performs quite a lot of other tasks before, eventually, reducing the input to some well formed list of tokens for\def
.