TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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 tried to reimplement some kind of a toc facility. I write wouts argument into a file and try to reinclude this. So the Problem is, that \ior_str_gto:NN seems to stop at linebreaks. Please consider the following example:

\usepackage{ expl3, l3keys2e, xfrac, xparse}



 \file_if_exist:nTF {myout.out} {   
    \ior_close:N\ior_out }
    { \typeout{no file! rerun}


\DeclareDocumentCommand\wout { m } { 
\DeclareDocumentCommand\rout {  } { 


The Question is: How can i make \ior_to_gstr to read in the whole file? and: is there a better way to implement such a function using l3?

share|improve this question
\ior_to:Nn, etc. read one line at a time. If you look at LaTeX2e, the TOC concept is implemented by \inputing the entire file. We have some experimental stuff for doing whole file loops, but not yet anything 'release ready'. Perhaps suggest what might work for you! – Joseph Wright Jun 23 '12 at 18:20
I thought that you might answer this. My TeX knowledge is far away from writing s.th. for the kernel. – bloodworks Jun 23 '12 at 18:41
up vote 8 down vote accepted

The LaTeX2e kernel reads files for tables, etc. using \input rather than line by line. However, it is possible to set up a line-by-line read here using epxl3 to work in a 'string' manner. There is an experimental function called \ior_str_map_inline:Nn which does more or less what seems to be wanted here:



\iow_new:N \g_my_out_iow
\ior_new:N \g_my_out_ior
\tl_new:N \g_my_out_tl

 \file_if_exist:nTF { myout.out }
     \ior_open:Nn \g_my_out_ior { myout.out }
     \ior_str_map_inline:Nn \g_my_out_ior 
      { \tl_gput_right:Nn \g_my_out_tl {#1 \par } }
     \ior_close:N \g_my_out_ior
   { \typeout{no file! rerun} }

\iow_open:Nn \g_my_out_iow { myout.out }

\DeclareDocumentCommand\wout { m }
  { \iow_now:Nn \g_my_out_iow {#1} }
\DeclareDocumentCommand\rout {  } 
  { \tl_use:N \g_my_out_tl }


(I have tided up a few variable names.) I've added \par to each line as it's not clear to me quite what is wanted. I've also written unexpanded to file, again as the wider context is not clear.

share|improve this answer
There are a few changes I will be making to the I/O code before the next CTAN update. That should not impact here, but I do notice I need to address the 'always global' nature of streams! – Joseph Wright Jun 23 '12 at 18:58
Thats very fine. I´m definitively going to look up this function. One thing still worries me: i do open the output stream for the entire compile time. I wonder if there is a tex function which allows opening out streams w/o recreating the whole file (such as >> in the shell). – bloodworks Jun 23 '12 at 19:19
@bloodworks There is no >> equivalent in TeX: one can read a file and write back, but there are issues. The LaTeX3 recommendation is not to keep the file open unless you need \iow_shipout:Nn or similar. If you want \iow_now:Nn then you can save everything to a tl and do a one-shot write at the end of the document. (We may well look at the shipout issue again, as one can solve this too, but it's more tricky and at the moment not something we need.) – Joseph Wright Jun 23 '12 at 19:21
@JosephWright: Would it make sense to add a function (in \ior or \file) to read the contents of a whole file? – Bruno Le Floch Jun 24 '12 at 9:22
Whats about tl_extract_between – bloodworks Jun 24 '12 at 19:55

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.