I am writing a package that needs to write at various times to an external file. For that I am using the \newwrite\mywriter and \immediate\openout\mywriter method. I know to use \closeout when done with the file, and I do this. What I am wondering: Does LaTeX close open files, if the process exits prematurely with errors before \closeout is reached? If it doesn't (and I think it doesn't), is there something I should do in my code to make it safer in this case, and is there something I can do to close the files before I run LaTeX again?

  • What makes you think the file is left open? – Werner Nov 3 '16 at 6:03
  • @Werner - Newbie concern. Nothing more. I haven't actually faced this situation yet. So far, my simple tests of writing out a LaTeX code file and \inputing it later have worked fine. I just have been doing a lot of Python scripting lately, where, though the garbage collection is pretty robust, it is still not a good idea to leave files open. try ... finallly constructs and the like are great for cleanup, but we don't seem to have such things in LaTeX. I shall not be so hesitant to drill into TeX source the next time I encounter such an input/output question. – celticflute Nov 3 '16 at 10:52

The source of TeX tells differently from your belief.

See modules 1332 and 1333 in tex.web. Only files open for input are not closed.

enter image description here

enter image description here

  • Thank you so much for the look under the hood ... and for your patience with a newbie. (Warning: newbie taking the bait ...) so, I wonder what is different about files open for input. – celticflute Nov 3 '16 at 11:04
  • @celticflute Since the premature exit (fatal error or user's choice) can happen at an arbitrary nesting level of \input, it's quite hard to go back, but it's quite probable that the operating system will take care of the issue. – egreg Nov 3 '16 at 11:34
  • Ah, got it. OK, I shall have faith in the OS and move on to creating a useful product (a package for building a Celtic music tunebook, BTW). Thanks again. – celticflute Nov 3 '16 at 12:32

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.