4

I have a project for which I have needed to resort to the morewrites package due to getting a No room for new \write. Up until a few months ago (which was when I last worked on it), this worked quite nicely. But when I tried compiling the document today, it was suddenly uncooperative. After trying to pinpoint the error, I have it narrowed down to this MWE.

\documentclass{article} % Uncomment for error %\usepackage{morewrites} \usepackage{filecontents} \newcounter{globalCounter} \newcounter{hostCounter} \begin{filecontents*}{main.ctr} 123 2 \end{filecontents*} \newcommand*\globalCounterFile{main.ctr} \newwrite\globalCounterFH \immediate\openin\globalCounterFH=\globalCounterFile \immediate\read\globalCounterFH to \globalCounterLine \immediate\read\globalCounterFH to \hostCounterLine \immediate\closein\globalCounterFH \setcounter{globalCounter}{\globalCounterLine} \setcounter{hostCounter}{\hostCounterLine} \stepcounter{globalCounter} \begin{document} This document has been compiled at least~\theglobalCounter~times across~\thehostCounter~hosts so far.\\ \end{document} 

The MWE works fine as long as \usepackage{morewrites} is commented out, but as soon as the package is loaded, I get the following error when compiling with pdflatex or xelatex:

! Bad number (19). l.15 \immediate\openin\globalCounterFH =\globalCounterFile 

Since I can't just remove morewrites from my big project, this is a slight bit of an issue.

The code above for reading from main.ctr is very heavily inspired by the counttex package, but I needed to change it slightly for my project to adapt it to my needs. Basically, I need to keep track of two counters instead of one, which are both stored in a file and read line-by-line. Really

I have read that lualatex does not require morewrites, but the main project was written with pdflatex in mind, and trying to compile it with lualatex yields errors which I expect will be trickier to localize and fix than this one.

I am looking for

  1. Information on why this happens, just out of pure curiosity,
  2. a solution, if one exists, to solve this problem and to get me back my project without needing to port it to lualatex or get it working without morewrites (which I already tried last year, rather painfully, resulting in failure). I would prefer to avoid major changes on the project, since I'm basically done with it. But I want to still be able to compile it for archivability purposes and the occasional bugfix in the future.
0

1 Answer 1

4

Your document works by pure chance. The instruction \newwrite\globalCounterFH (without morewrites) essentially defines \globalCounterFH as an integer and it happens that the integer turns out to be 3 (the write streams 0, 1 and 2 are reserved).

You want to do

\newread\globalCounterFH 

not \newwrite.

The reason for the “Bad number” error is that \openin expects an integer in the range 0–15, but morewrites allocates numbers for write streams starting from 19. However, this is irrelevant, seen the above explanation.

By the way, \immediate is a no-op when it precedes \openin and \read.

1
  • Ah, the pitfalls of copy-pasting-and-slightly-changing code without fully understanding its intricacies. Curious that it worked for so long and then broke, but ah well, such is life. In any case, my big document compiles flawlessly once again, thanks! Commented Jan 7, 2018 at 23:47

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.