13

I don't know too much about writing and reading to and from files. But I managed to create a nice command for me where I need to write something into a text-file and later-on read it again.

\newcommand{\customwrite}[1]{
  \newwrite\tempfile%
  \immediate\openout\tempfile=filename_#1.txt%
  \immediate\write\tempfile{\the\figheight}%
  \immediate\closeout\tempfile%
}

The reading is achieved by

\newcommand{\customread}[1]{
  \newread\tempfile%
  \openin\tempfile=filename_#1.txt%
  \read\tempfile to \fileline%
  \setlength\figheightex\fileline%
  \closein\tempfile%
}

Both things works fine for me as long as I have not too much of this calls. Recently, I had a bigger document and everything failed and I got the message No room for a new \write. I searched for this error and what I found is, Latex is only capable of opening 16 write and 16 read streams. Apparently \closeout and \closein don't actually close these streams what I thought.

My question is now: is it somehow possible to actually close these streams?

  • you can use \tempfile repeatedly, without more than one \newwrite. Or am I missing something? And you shloud have a seperate one \mytempout for writing and \mytempin for reading... – yo' Oct 9 '13 at 12:10
  • Ok, sorry. I truncated too much from my file. filename.txt actually changes by an optional argument, I corrected this in my posting. Furthermore, I can use \tempfile repeatedly. I only need this "variable" once while executing the command. and since I always only execute one of the commands, it was working for me using the same "variable" \tempfile for writing and reading – bene Oct 9 '13 at 12:14
  • @tohecz you should make that an answer. – David Carlisle Oct 9 '13 at 12:27
  • @bene as tohez says just move the \new.... out of the definitions and just execute them once in the preamble. – David Carlisle Oct 9 '13 at 12:28
  • If you are just saving the height of a figure, why use a file at all rather than just save the macro? – David Carlisle Oct 9 '13 at 12:30
15

You reserve a new read/write slot every time you read or write, but you don't need it. Moving \new... out of the definitions does the trick:

\newwrite\mytempout
\newcommand{\customwrite}[1]{%
  \immediate\openout\mytempout=filename_#1.txt %
  \immediate\write\mytempout{\the\figheight}%
  \immediate\closeout\mytempout
}

\newread\mytempin
\newcommand{\customread}[1]{%
  \openin\mytempin=filename_#1.txt %
  \read\mytempin to \fileline
  \setlength\figheightex\fileline
  \closein\mytempin
}

I added and removed some of the %s to make it more reasonable, see e.g. Where are the necessary places to be appended with % to remove unwanted spaces?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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