[comp.text.tex] Question about \endlinechar

dmjones@theory.lcs.mit.edu (David M. Jones) (02/26/91)

I have a question about the behaviour of \endlinechar that has come up
while I was designing some macros to help run a refereed journal.  The
basic problem is this: I have a number of files containing a single
line, the formal salutation of one of the editors of the journal.  For
example, I have file Meyer.tex containing the line "Professor Meyer".
At an appropriate time, I wish to open Meyer.tex for reading and store
its first line in the control sequence \EditorSalutation.  If the file
is unreadable or nonexistient, I want \EditorSalutation defined to be
a no-op.  \EditorSalutation will then be used in contexts such as

        Dear \EditorSalutation,

Normally, the end of line character at the end of the file Meyer.tex
would get translated into an unwanted space, and the line above would
expand to

        Dear Professor Meyer ,

The simplest solution seems to be to set \endlinechar to -1 when
reading Meyer.tex, so that new line characters get thrown away.  So, I
wrote the following code:

{\endlinechar=-1
\openin0=Meyer.tex
\ifeof0
  \global\let\EditorSalutation\relax
\else
  \global\read0 to\EditorSalutation
\fi
\closein0}

The problem is that \ifeof0 always tests true now, so
\EditorSalutation is always set to \relax.  Note that removing the
conditional gives the correct behavior when the file exists and is not
empty:

{\endlinechar=-1
\openin0=Meyer.tex
\global\read0 to\EditorSalutation
\closein0}

The workaround is to move the \endlinechar inside the conditional,
like so:

{\openin0=Meyer.tex
\ifeof0
  \global\let\EditorSalutation\relax
\else
  {\endlinechar=-1
   \global\read0 to\EditorSalutation}
\fi
\closein0}

This works, but it puzzles me.  Why should the value of \endlinechar
affect TeX's judgement of whether the file is empty or not?  Can
anyone explain this?  I hesitate to mention the word "bug", but this
is certainly non-intuitive behaviour.
------------------------------------------------------------------------------
David M. Jones                              |"The earth's a prison -- one can't
17 Simpson Ave #1; Somerville, MA 02144     | get away from it.... I'm still
ARPANET: dmjones@theory.lcs.mit.edu         | too young to lack desires, Not
UUCP: ...!mit-eddie!mit-athena!dmjones      | young enough now for mere play."
------------------------------------------------------------------------------

eijkhout@s41.csrd.uiuc.edu (Victor Eijkhout) (02/26/91)

dmjones@theory.lcs.mit.edu (David M. Jones) writes:

>{\endlinechar=-1
>\openin0=Meyer.tex
>\ifeof0

And there is no need read further.

If you would write
 \openin0=Meyer.tex
 \ifeof0
TeX would see `\openin0=Meyere.tex \ifeof0'
and presumably a space cannot appear in a file name.
Therefore the file name is `Meyer.tex'.

Now, what you wrote gives `\openin0=Meyer.tex\ifeof0 ...'
and the conditional gets evaluated during the scan
of the file name.

>Why should the value of \endlinechar
>affect TeX's judgement of whether the file is empty or not? 

I hope you understand that you've drawn the wrong conclusion?

Victor.

eijkhout@s41.csrd.uiuc.edu (Victor Eijkhout) (02/26/91)

eijkhout@s41.csrd.uiuc.edu (Victor Eijkhout) was quite
unclear when he wrote:

>dmjones@theory.lcs.mit.edu (David M. Jones) writes:

>>{\endlinechar=-1
>>\openin0=Meyer.tex
>>\ifeof0

>If you would write
> \openin0=Meyer.tex
> \ifeof0
>TeX would see `\openin0=Meyere.tex \ifeof0'

What I meant when writing `if you would write', was:
`if you would leave out the first line with \endlinechar'.
The starting spaces have no effect on TeX's processing\footnote
{Quiz: Why does the plain TeX \obeyspaces\obeylines combination
 not see starting spaces? Former participants of my
 Advanced TeX course excluded from competition}.

Here is then the solution:
{\endlinechar=-1
 \openin0=Meyer.tex\relax
 \ifeof0 ...

or replace the second line by 
 \openin0=Meyer.tex %
which I consider to be an abuse.

Victor.