TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is TeX able to distinguish beween a file containing, say, the single character a/ byte 97; and a file containing this same character followed by a newline marker (which is system-dependent)?

I had a bit of trouble creating a file with no trailing newline, so in case you need it, here is one way:

luatex "\directlua{io.output('test1.txt'):write('a') io.output('test2.txt'):write('a\string\n')}\bye"

Can TeX distinguish test1.txt from test2.txt (besides their name)? It seems that \read and \readline both discard a trailing newline and set \ifeof to true at the same time regardless of the presence of a trailing newline.

share|improve this question
up vote 16 down vote accepted

TeX also removes space characters at the right side of the input line, then it inserts the character configured via \endlinechar. The character is not inserted if \endlinechar is negative or greater than 255. The last restriction is different in LuaTeX/XeTeX. Because of the removal/replacing at the end of line vanilla TeX is not able to distinguish these files. Therefore TeX cannot read arbitrary binary files, e.g. to extract the size of an image file, …

In pdfTeX provides \pdffiledump. It returns the data, specified via offset and length, as hex dump. Therefore the end of file can be analyzed for end of line markers. (Package bmpsize uses \pdffiledump to analyze image file formats for meta data like width and height.)

And LuaTeX provides Lua functions for file I/O.

share|improve this answer
Thank you Heiko. It seems like a recurring theme in my questions to find out that XeTeX lacks the appropriate pdfTeX primitive and lacks the Lua functions provided by LuaTeX :(. – Bruno Le Floch Aug 4 '12 at 11:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.