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>\write (LaTeX2e unofficial reference manual (November 2018))</title> 0043 0044 <meta name="description" content="\write (LaTeX2e unofficial reference manual (November 2018))"> 0045 <meta name="keywords" content="\write (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="_005cmessage.html#g_t_005cmessage" rel="next" title="\message"> 0054 <link href="_005ctypeout.html#g_t_005ctypeout" rel="prev" title="\typeout"> 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_005cwrite"></a> 0114 <div class="header"> 0115 <p> 0116 Previous: <a href="_005ctypeout.html#g_t_005ctypeout" accesskey="p" rel="prev">\typeout</a>, Up: <a href="Input_002foutput.html#Input_002foutput" accesskey="u" rel="up">Input/output</a> [<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_005cwrite-1"></a> 0120 <h3 class="section">27.5 <code>\write</code></h3> 0121 0122 <a name="index-_005cwrite"></a> 0123 0124 <p>Synopsis: 0125 </p> 0126 <div class="example"> 0127 <pre class="example">\write<var>number</var>{<var>string</var>} 0128 </pre></div> 0129 0130 <p>Write <code>string</code> to the log file, to the terminal, or to a file 0131 opened by <code>\openout</code>. For instance, <code>\write6</code> writes to text 0132 stream number 6. 0133 </p> 0134 <p>If the following appears in <samp><var>basefile</var>.tex</samp> then it opens 0135 <samp><var>basefile</var>.jh</samp>, writes ‘<samp>Hello World!</samp>’ and a newline to 0136 it, and closes that file. 0137 </p> 0138 <div class="example"> 0139 <pre class="example">\newwrite\myfile 0140 \immediate\openout\myfile=\jobname.jh % \jobname is current file name 0141 ... 0142 \immediate\write\myfile{Hello world!} 0143 ... 0144 \immediate\closeout\myfile 0145 </pre></div> 0146 0147 <p>The <code>\newwrite</code> allocates a stream number, giving it a symbolic 0148 names to make life easier, so that <code>test 0149 \newwrite\myfile\the\myfile</code> produces something like ‘<samp>test 3</samp>’. 0150 Then <code>\openout</code> associates the stream number with the given file 0151 name. With that, <code>\write3</code> puts the string in the file. 0152 </p> 0153 <p>Typically <var>number</var> is between 0 and 15 because typically 0154 LaTeX authors follow the prior example and the number is allocated by 0155 the system. If <var>number</var> is outside the range from 0 to 15 or if it 0156 is not associated with an open file then LaTeX writes <var>string</var> to 0157 the log file. If <var>number</var> is positive then in addition LaTeX 0158 writes <var>string</var> to the terminal. Thus, <code>test \write-1{Hello 0159 World!}</code> puts ‘<samp>Hello World!</samp>’ followed by a newline in the log 0160 file. (This is what the <code>\wlog</code> command does; see <a href="_005cwlog.html#g_t_005cwlog">\wlog</a>). And 0161 <code>\write100{Hello World!}</code> puts the same in the log file but also 0162 puts ‘<samp>Hello World!</samp>’ followed by a newline in the terminal output. 0163 (But 16, 17, and 18 are special as <var>number</var>; see below.) 0164 </p> 0165 <p>In LuaTeX, instead of 16 output streams there are 256 0166 (see <a href="TeX-engines.html#TeX-engines">TeX engines</a>). 0167 </p> 0168 <p>Write to the current <samp>.aux</samp> file, which is associated with the main 0169 file or with the current include file, using 0170 <code>\write\@auxout{<var>string</var>}</code>. Write to the main <samp>.aux</samp> 0171 file using <code>\write\@mainaux{<var>string</var>}</code>. 0172 </p> 0173 <p>By default LaTeX does not write <var>string</var> to the file right away. 0174 This is because, for example, you may use <code>\write</code> to save the 0175 current page number but when TeX comes across a <code>\write</code> it may 0176 be not yet sure what page this is, since it has not yet done the page 0177 breaking. So you <code>\write</code> in one of three contexts. 0178 </p> 0179 <div class="example"> 0180 <pre class="example">\immediate\write\@auxout{<var>string</var>} 0181 \write\@auxout{<var>string</var>} 0182 \protected@write\@auxout{}{<var>string</var>} 0183 </pre></div> 0184 0185 <p>With the first, LaTeX writes <var>string</var> to the file. Any commands 0186 in <var>string</var> will be expanded (just as in <code>\edef</code> so that to 0187 prevent expansion you should use <code>\noexpand</code> or a <code>toks</code>, 0188 except that you should use <code>#</code> instead of <code>##</code>). With the 0189 second, <var>string</var> is stored on the current list of things (as a 0190 TeX “whatsit”) and kept until the page is shipped out and likewise 0191 the commands are unexpanded until shipout. The third, 0192 <code>\protected@write</code>, is like the second except that you can use 0193 <code>\protect</code> on fragile commands. The extra first argument allows you 0194 to locally insert extra definitions to make more commands be safe or 0195 have special definition during the write. 0196 </p> 0197 <p>Here <var>string</var> contains a control sequence. 0198 </p> 0199 <div class="example"> 0200 <pre class="example">\newwrite\jhfile 0201 \openout\jhfile=test.jh 0202 \newcommand{\triplex}{XXX} 0203 \write\jhfile{test \triplex test} 0204 </pre></div> 0205 0206 <p>This results in the file <samp>test.jh</samp> containing ‘<samp>test XXXtest</samp>’ 0207 followed by a newline. 0208 </p> 0209 <p>The cases where <var>number</var> is 16, 17, or 18 are special. Because of 0210 <code>\write</code>’s behavior when <var>number</var> is outside the range from 0 0211 to 15 described above, in Plain TeX <code>\write16</code> and 0212 <code>\write17</code> were sometimes used to write to the log file and the 0213 terminal. Note that in LaTeX the natural way to do that is with 0214 <code>\typeout</code> (see <a href="_005ctypeout.html#g_t_005ctypeout">\typeout</a>). The <code>\write18</code> command is even 0215 more special; modern TeX systems use it for giving commands to the 0216 operating system (see <a href="_005cwrite18.html#g_t_005cwrite18">\write18</a>). 0217 </p> 0218 <p>Ordinarily <code>\write</code> outputs a single line. Put in a newline with 0219 <code>^^J</code>. Thus, this produces two lines in the log file. 0220 </p> 0221 <div class="example"> 0222 <pre class="example">\wlog{Parallel lines have a lot in common.^^JBut they never meet.} 0223 </pre></div> 0224 0225 <p>The ability to write files raises security issues. If you compiled a 0226 downloaded LaTeX file and it overwrote your password file then you 0227 would be justifiably troubled. TeX systems by default only allow 0228 you to open files for writing that are in the current directory or in 0229 a subdirectory. This 0230 </p> 0231 <div class="example"> 0232 <pre class="example">\newwrite\jhfile 0233 \openout\jhfile=../test.jh 0234 </pre></div> 0235 0236 <p>gives an error like ‘<samp>Not writing to ../test.jh (openout_any = p). ! 0237 I can't write on file `../test.jh'</samp>’. Note that you can get such an 0238 error when trying to use commands such as <code>\include{../filename}</code> 0239 because LaTeX will try to open <samp>../filename.aux</samp>. The simplest 0240 solution is to put the included files in the same directory as the 0241 master file, or in subdirectories. 0242 </p> 0243 <a name="index-package_002c-answers"></a> 0244 <a name="index-answers-package"></a> 0245 0246 <p>A common case where authors want to write a file not already provided by 0247 LaTeX is for answers to exercises, or some other situation where you 0248 want to write out verbatim, without expanding the commands. CTAN has a 0249 number of packages for this; one is <samp>answers</samp>. 0250 </p> 0251 <table class="menu" border="0" cellspacing="0"> 0252 <tr><td align="left" valign="top">• <a href="_005cmessage.html#g_t_005cmessage" accesskey="1">\message</a>:</td><td> </td><td align="left" valign="top">Write part of a line to log file and terminal. 0253 </td></tr> 0254 <tr><td align="left" valign="top">• <a href="_005cwlog.html#g_t_005cwlog" accesskey="2">\wlog</a>:</td><td> </td><td align="left" valign="top">Write to the log file. 0255 </td></tr> 0256 <tr><td align="left" valign="top">• <a href="_005cwrite18.html#g_t_005cwrite18" accesskey="3">\write18</a>:</td><td> </td><td align="left" valign="top">Write to the operating system. 0257 </td></tr> 0258 </table> 0259 0260 0261 0262 0263 <hr/><div class='referenceinfo'> <a href='https://latexref.xyz/'> <i>Unofficial LaTeX2e reference manual</i></a></div> 0264 </body> 0265 </html>