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?

bene
  • 2,090
  • 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 09 '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 09 '13 at 12:14
  • @tohecz you should make that an answer. – David Carlisle Oct 09 '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 09 '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 09 '13 at 12:30
  • ahh, now I got the comment from tohecz. I'll try that. – bene Oct 09 '13 at 12:32
  • Thanks a lot tohecz, it works. The reason, why I save the height of a figure is, because I externalize pgfplots which I generated with matlab2tikz and set their height and width within my main latex document. And of course, I want to remake the figure if I changed the dimensions and that's how I do it at the moment. I don't know if there are better possibilities to realize that, but for me this flow now works perfectly. – bene Oct 09 '13 at 12:42
  • @bene Usually there are better possibilities, but if this one works for you, it's fine :) and as well, this way you practice and improve your TeXnicianity! :) – yo' Oct 09 '13 at 14:06

1 Answers1

16

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?

yo'
  • 51,322