# Latex3 input/output and expansion of macros

I have a package that formats a course outline and have a macro that reads a formatted user file and turns it into a section.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\tl_new:N \l_my_macro_tl
\tl_set:Nn \l_my_macro_tl {fish}

\NewDocumentEnvironment {mySection} {m}
{\noindent\textbf{\textsl{\large #1}}\hrulefill \par\smallskip}
{\par\medskip}

\NewDocumentCommand \makeOutlineSection{m}
{
\tl_clear:N \l_tmpa_tl
\ior_new:N \l_user_content_ior
\ior_open:Nn \l_user_content_ior {#1}           % read from #1
\ior_to:NN \l_user_content_ior \l_tmpa_tl       % put first line in tmpa
\begin{mySection}{\l_tmpa_tl}                   % first line is section title
\ior_to:NN \l_user_content_ior \l_tmpb_tl % rest of file in tmpb
\l_tmpb_tl                                % print tmpb
\end{mySection}
\ior_close:N \l_user_content_ior
}

\ExplSyntaxOff
\begin{document}

\makeOutlineSection{myfile.tex}

\end{document}


Where myfile.tex, for this example, has the form:

{My~Test~Section}
{
\ExplSyntaxOn
\l_my_macro_tl
\ExplSyntaxOff
}


If myfile.tex contains environments, such as longtable, from packages loaded in the preamble, then everything works as planned. However, it is having a hard time deciding what \l_my_macro_tl is. It seems that it is either that \ExplSyntaxOn isn't being recognized or there is some problem with expanding the macro. Could anyone explain why this is happening and possibly suggest a fix?

-

You can not change catcodes once the characters have been tokenised while looking for macro (or \read) arguments. Thus you need to take \ExplSyntaxOn out of the included file and put them around

    \ior_to:NN \l_user_content_ior \l_tmpb_tl


That would mean that the file had to have L3 syntax of course. Simplest from where you are is if you have two different commands (or a command and a star form) for loading l2 and l3.

On the other and if you input the file with \input rather than \read it would be read character by character like the main input file and then you could switch catcodes at any point so use \ExplSyntaxOn within the file.

-
Wow, that was quick! I was using input previously but thought I'd try it another way. Since the files to be included will contain straight text in addition to l3 commands, I'll have to stick with input (or type a whole lot of ~'s). Thanks for the response :) –  Scott H. May 15 '12 at 8:44

Mixing programming level commands and user level commands is a sure source of headaches and of course it is recommended not to do it.

Never use the programming level commands other than when when defining user level commands or programming other macros.

In your case, you can define a macro that accesses the desired token list:

\ExplSyntaxOn
\NewDocumentCommand{\gettl}{ m }
{
\tl_use:c { l_my_ #1 _tl }
}
\ExplSyntaxOff


and use, in your input file

{My~Test~Section}
{
\gettl{macro}
}

-