TransWikia.com

Q: Loop over all tokens in the body of a macro

TeX - LaTeX Asked by Michael Nüsken on May 24, 2021

I am looking for TeX-code that loops over all tokens in a macro definition.
meaning transforms everything to “other” (catcode 12) characters and is thus not directly suited (with or without @strip@prefix).

Any ideas?

2 Answers

In response to the OP comment, "Ultimately, I want to list all macros in the definition of zz."

EDIT: I reformulated my answer with the tokcycle package because I found the original answer from 3.5 years ago did not handle the case well if control sequences were embedded down inside groups inside the macro. Now, nested groups pose no challenge to the task.

documentclass{article}
usepackage[T1]{fontenc}
usepackage{tokcycle}
newcommandlooptokens[1]{expandafterlooptokensauxexpandafter{#1}{#1}}
newcommandlooptokensaux[2]{%
  The macro detokenize{#2}contains these control sequences:
  tokcycle{}{processtoks{##1}}{detokenize{##1}}{}{#1}}
begin{document}
defzz{What textit{textsc{macros like today}} are contained 
  textbf{in this argument} relax??}
looptokens{zz}
end{document}

enter image description here

Answered by Steven B. Segletes on May 24, 2021

Poor man's way. If I understand correctly you want all macros encountered in the meaning (even within braces).

documentclass{article}

makeatletter
deffetchsubmacros#1{deffsm@temp{#1}expandafterfsm@imeaningfsm@temp~}
catcode`& 7
deffsm@i#1->#2~%
   {begingroupttfamilycatcode`{=9 catcode`}=9 everyeof{&}%
    makeatletterscantokens{fsm@ii#2}}
deffsm@ii#1{ifx&#1endgroupexpandafter@gobbletwofifsm@iii#1}
catcode`& 4
deffsm@iii#1{ifcatrelaxnoexpand#1Found: string#1parfifsm@ii}
makeatother                       

begin{document}


fetchsubmacros{What textit{foo macros} are bar contained textbf{in emph{this argument}} relax??}

end{document}

enter image description here

Answered by user4686 on May 24, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP