[texhax] macro expansion and \input

Neal H. Walfield neal at walfield.org
Mon Oct 1 11:34:57 CEST 2012


Hi,

I have a number of generated files containing latex snippets.  So far,
I've include them in my document using \input.  But, to make life a
bit easier, I want to use a macro to generate the full filenames on
the fly.  This is resulting in some macro expansion issues, which I
don't understand.  The following is what I want to do:

  \documentclass{article}
  
  \begin{document}
  
  % My general-purpose macro.
  \def\mutate#1{
    \def\helper{#1}
    \helper
  }
  
  \input{\mutate{23}}
  
  \end{document}

But, this results in:

  ! Missing control sequence inserted.
  <inserted text> 
                  \inaccessible 
  l.11 \input{\mutate{23}}

It seems like \input is protecting its input from expansion.
Interestingly, simple expansion, such as the following, works fine:

  \documentclass{article}

  \begin{document}

  \def\filename{12}
  \input{\filename}

  \end{document}

Based on this observation, I tried to preexpand \input's argument:

  \documentclass{article}
  
  \begin{document}
  
  % My general-purpose macro.
  \def\mutate#1{
    \def\helper{#1}
    \helper
  }
  
  \def\myinput#1{
    \edef\filename{#1}
    \input{\filename}
  }

  \myinput{\mutate{23}}
  
  \end{document}

But, this results in a different error:

  ! Undefined control sequence.
  \mutate #1-> \def \helper 
                            {#1} \helper 
  l.16   \myinput{\mutate{23}}
                              
When expanding \filename, tex wants expands \mutate's \helper control
sequence rather than define it!

Now, I'm stuck and would welcome some help...


Thanks,

Neal


More information about the texhax mailing list