File indexing completed on 2024-11-24 04:34:43

0001 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
0002 <html>
0003 <!-- This document is an unofficial reference manual for LaTeX, a
0004 document preparation system, version of November 2018.
0005 
0006 This manual was originally translated from LATEX.HLP v1.0a in the
0007 VMS Help Library.  The pre-translation version was written by
0008 George D. Greenwade of Sam Houston State University.  The
0009 LaTeX 2.09 version was written by Stephen Gilmore.  The
0010 LaTeX2e version was adapted from this by Torsten Martinsen.  Karl
0011 Berry made further updates and additions, and gratefully acknowledges
0012 using Hypertext Help with LaTeX, by Sheldon Green, and
0013 LaTeX Command Summary (for LaTeX 2.09) by
0014 L. Botway and C. Biemesderfer (published by the TeX Users
0015 Group as TeXniques number 10), as reference material.  We also
0016 gratefully acknowledge additional material appearing in
0017 latex2e-reference by Martin Herbert Dietze.  (From these references no
0018 text was directly copied.)
0019 
0020 Copyright 2007, 2008, 2009, 2010, 2011, 2012, 2013,
0021 2014, 2015, 2016, 2017, 2018 Karl Berry.
0022 
0023 Copyright 1988, 1994, 2007 Stephen Gilmore.
0024 
0025 Copyright 1994, 1995, 1996 Torsten Martinsen.
0026 
0027 Permission is granted to make and distribute verbatim copies of
0028 this manual provided the copyright notice and this permission notice
0029 are preserved on all copies.
0030 
0031 
0032 Permission is granted to copy and distribute modified versions of this
0033 manual under the conditions for verbatim copying, provided that the entire
0034 resulting derived work is distributed under the terms of a permission
0035 notice identical to this one.
0036 
0037 Permission is granted to copy and distribute translations of this manual
0038 into another language, under the above conditions for modified versions. -->
0039 <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
0040 <head>
0041 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
0042 <title>\openin &amp; \openout (LaTeX2e unofficial reference manual (November 2018))</title>
0043 
0044 <meta name="description" content="\openin &amp; \openout (LaTeX2e unofficial reference manual (November 2018))">
0045 <meta name="keywords" content="\openin &amp; \openout (LaTeX2e unofficial reference manual (November 2018))">
0046 <meta name="resource-type" content="document">
0047 <meta name="distribution" content="global">
0048 <meta name="Generator" content="makeinfo">
0049 <link href="index.html#Top" rel="start" title="Top">
0050 <link href="IndexDocument.html#Index" rel="index" title="Index">
0051 <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
0052 <link href="Input_002foutput.html#Input_002foutput" rel="up" title="Input/output">
0053 <link href="_005cread.html#g_t_005cread" rel="next" title="\read">
0054 <link href="Input_002foutput.html#Input_002foutput" rel="prev" title="Input/output">
0055 <style type="text/css">
0056 <!--
0057 /* $Id: latex2e.css 690 2018-09-15 15:20:19Z jimhefferon $
0058    Minor css for latexrefman. Public domain.
0059    Originally written by Jim Hefferon and Karl Berry, 2018. */
0060 
0061 /* So you can style for yourself */
0062 @import url("/css/latexreflocal.css");
0063 
0064 
0065 
0066 a.summary-letter {text-decoration: none}
0067 blockquote.indentedblock {margin-right: 0em}
0068 blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
0069 blockquote.smallquotation {font-size: smaller}
0070 div.display {margin-left: 3.2em}
0071 div.example {margin-left: 3.2em}
0072 div.lisp {margin-left: 3.2em}
0073 div.smalldisplay {margin-left: 3.2em}
0074 div.smallexample {margin-left: 3.2em}
0075 div.smalllisp {margin-left: 3.2em}
0076 kbd {font-style: oblique}
0077 pre.display {font-family: inherit}
0078 pre.format {font-family: inherit}
0079 pre.menu-comment {font-family: serif}
0080 pre.menu-preformatted {font-family: serif}
0081 pre.smalldisplay {font-family: inherit; font-size: smaller}
0082 pre.smallexample {font-size: smaller}
0083 pre.smallformat {font-family: inherit; font-size: smaller}
0084 pre.smalllisp {font-size: smaller}
0085 span.nolinebreak {white-space: nowrap}
0086 span.roman {font-family: initial; font-weight: normal}
0087 span.sansserif {font-family: sans-serif; font-weight: normal}
0088 ul.no-bullet {list-style: none}
0089 BODY {
0090   margin-top: 1em;
0091   margin-left: 1em; /* auto results in two-digit <ol> lost off left */
0092   margin-right: 1em;
0093   margin-bottom: 1em;
0094   /* the idea is to use the whole window, unless it is ridiculously
0095      wide, probably with too-small fonts, too.  */
0096   max-width: 64em;
0097 }
0098 
0099 /* Because we want @math{... @code ...} to be upright, not slanted,
0100    and Texinfo won't fix it. */
0101 code {font-style:normal; font-family:monospace; }
0102 
0103 /* We put a link to our own home page at the bottom. */
0104 div.referenceinfo {font-size:small;}
0105 
0106 -->
0107 </style>
0108 
0109 
0110 </head>
0111 
0112 <body lang="en">
0113 <a name="g_t_005copenin-_0026-_005copenout"></a>
0114 <div class="header">
0115 <p>
0116 Next: <a href="_005cread.html#g_t_005cread" accesskey="n" rel="next">\read</a>, Up: <a href="Input_002foutput.html#Input_002foutput" accesskey="u" rel="up">Input/output</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="IndexDocument.html#Index" title="Index" rel="index">Index</a>]</p>
0117 </div>
0118 <hr>
0119 <a name="g_t_005copenin-_0026-_005copenout-1"></a>
0120 <h3 class="section">27.1 <code>\openin</code> &amp; <code>\openout</code></h3>
0121 
0122 <a name="index-_005copenin"></a>
0123 <a name="index-_005copenout"></a>
0124 <a name="index-_005cclosein"></a>
0125 <a name="index-_005ccloseout"></a>
0126 <a name="index-file_002c-opening"></a>
0127 <a name="index-file_002c-closing"></a>
0128 <a name="index-open-a-file"></a>
0129 
0130 <p>Synopsis:
0131 </p>
0132 <div class="example">
0133 <pre class="example">\openin<var>number</var>=<var>filename</var>
0134 </pre></div>
0135 
0136 <p>or:
0137 </p>
0138 <div class="example">
0139 <pre class="example">\openout<var>number</var>=<var>filename</var>
0140 </pre></div>
0141 
0142 <p>Open a file for reading material, or for writing it.  The <var>number</var>
0143 must be between 0 and 15, as in <code>\openin3</code> (in LuaLaTeX
0144 <var>number</var> can be between 0 and 255).
0145 </p>
0146 <p>Here TeX opens the file <samp>presidents.tex</samp> for reading.
0147 </p>
0148 <div class="example">
0149 <pre class="example">\newread\presidentsfile
0150 \openin\presidentsfile=presidents
0151 \typeout{presidentsfile is \the\presidentsfile}
0152 \read\presidentsfile to\presidentline
0153 \typeout{\presidentline}
0154 </pre></div>
0155  
0156 <p>The <code>\newread</code> command allocates input stream numbers from 0
0157 to&nbsp;15 (there is also a <code>\newwrite</code>).  The
0158 <code>\presidentsfile</code> is more memorable but under the hood it is still
0159 a number; the first <code>\typeout</code> gives something like
0160 &lsquo;<samp>presidentsfile is 1</samp>&rsquo;.  In addition, <code>\newread</code> keeps track of
0161 the allocation so that if you use too many then you get an error like
0162 &lsquo;<samp>! No room for a new \read</samp>&rsquo;.  The second <code>\typeout</code> gives the
0163 first line of the file, something like &lsquo;<samp>1 Washington, George</samp>&rsquo;.
0164 </p>
0165 <p>Ordinarily TeX will not try to open the file until the next page
0166 shipout. To change this, use
0167 <code>\immediate\openin<var>number</var>=<var>filename</var></code> or
0168 <code>\immediate\openout<var>number</var>=<var>filename</var></code>.
0169 </p>
0170 <p>Close files with <code>\closein<var>number</var></code> and
0171 <code>\closeout<var>number</var></code>.
0172 </p>
0173 <p>How LaTeX handles filenames varies among distributions, and even can
0174 vary among versions of a distribution.  If the file does not have an
0175 extension then TeX will add a <samp>.tex</samp>.  This creates
0176 <samp>presidents.tex</samp>, writes one line to it, and closes it.
0177 </p>
0178 <div class="example">
0179 <pre class="example">\newwrite\presidentsfile
0180 \openout\presidentsfile=presidents
0181 \write\presidentsfile{1 Washington, George}
0182 \closeout\presidentsfile
0183 </pre></div>
0184 
0185 <p>But filenames with a period can cause trouble: if TeX finds a
0186 <var>filename</var> of <samp>presidents.dat</samp> it could look first for
0187 <samp>presidents.dat.tex</samp> and later for <samp>presidents.dat</samp>, or it
0188 could do the opposite.  Your distribution&rsquo;s documentation should say
0189 more, and if you find something that works for you then you are good,
0190 but to ensure complete portability the best thing is to use file names
0191 containing only the twenty six ASCII letters (not case-sensitive) and
0192 the ten digits, along with underscore and dash, and in particular with
0193 no dot or space.
0194 </p>
0195 <p>For <code>openin</code>, if TeX cannot find the file then it does not give
0196 an error.  It just considers that the stream is not open (test for this
0197 with <code>\ifeof</code>; one recourse is the command
0198 <code>\InputIfFileExists</code>, see <a href="Class-and-package-commands.html#Class-and-package-commands">Class and package commands</a>).  If you
0199 try to use the same number twice, LaTeX won&rsquo;t give you an error.  If
0200 you try to use a bad number then you get an error message like &lsquo;<samp>!
0201 Bad number (16).  &lt;to be read again&gt; = l.30 \openin16=test.jh</samp>&rsquo;.
0202 </p>
0203 
0204 
0205 
0206 <hr/><div class='referenceinfo'> <a href='https://latexref.xyz/'> <i>Unofficial LaTeX2e reference manual</i></a></div>
0207 </body>
0208 </html>