10

I'd like to expand a macro inside \AtBeginDocument, although I guess the concrete macro does not matter.

\documentclass{article}

\makeatletter
\@for\x:=1,2,3\do{%
  \AtBeginDocument{\@input@{helper\x .tex}}%
}
\makeatother

\begin{document}
Do something with the definitions from helper1.tex etc. 
\end{document}

The file inclusion works without the \AtBeginDocument{...}, and of course it does not work with it, as \x is not expanded at call time. I have the feeling the right amount of \expandafters in the right places will do the job, but I don't see it.

1 Answer 1

13
\makeatletter
\@for\x:=1,2,3\do{%
  \begingroup\edef\x{\endgroup
    \noexpand\AtBeginDocument{\noexpand\@input@{helper\x.tex}}}\x
}
\makeatother

Or with expl3:

\usepackage{expl3}
\ExplSyntaxOn\makeatletter
\clist_map_inline:nn { 1 , 2 , 3 }
 {
  \AtBeginDocument{\@input@{helper#1.tex}}
 }
\makeatother\ExplSyntaxOff
2
  • 2
    +1 I suppose: you were quicker by a second as \x shorter than \tmp Commented Jul 4, 2013 at 22:59
  • @DavidCarlisle That's almost automatic; isn't using \x with six different meanings lovely?
    – egreg
    Commented Jul 4, 2013 at 23:01

You must log in to answer this question.

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