TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.


\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}

\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
  \ior_close:N \l_user_content_ior




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


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?

share|improve this question
up vote 6 down vote accepted

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.

share|improve this answer
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:

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

and use, in your input file

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.