Intro
I'm working on a lightweight package that allows you to put any content (typically LaTeX code) in an environment, then use that content multiple times, each time scanned separately, so you can use it both as verbatim output and interpreted as LaTeX.
Its main purpose is to typeset LaTeX code and its 'result' side-by-side; useful for documenting TeX packages without having to copy/paste code. But it has other purposes as well.
After experimenting with \scantokens until I gave up in tears, I arrived at the filecontents environment, which almost solved my problem singlehandedly!
The Problem
The problem is, when reintroducing filecontented code through \input, LaTeX adds an implicit newline at the end, which translates to whitespace that I can't seem to get rid of.
I've seen many semi-related questions floating around, with answers employing techniques like:
- reading a file line-by-line,
- using
\everyeofin some clever way, - playing with
\endlinecharor - using packages such as
catchfile
but none of them provides a clear answer to the following question:
The Question
How can I \input a file without getting the implicit newline?
Minimal Working Example
\documentclass[margin=1mm]{standalone}
\usepackage{filecontents}
\begin{filecontents*}{tmp.tex}
\LaTeX{}\end{filecontents*}
\begin{document}
\fbox{\input{tmp.tex}}
\end{document}

Note the extra whitespace to the right of the box.
Note also that I'm definitely not adding any explicit whitespace. Even a manually produced file (so, not using filecontents) with no newlines at all exhibits the same problem.


\pardoes nothing in restricted horizontal mode. – egreg Dec 09 '12 at 16:51\par, but about the single newline (\nif you will) that TeX adds to my input. It gets converted to a space, just like TeX always does with single newlines. No? – mhelvens Dec 09 '12 at 16:56\par– egreg Dec 09 '12 at 16:57abc\input{tmp}defand you'll see the\parin action. – egreg Dec 09 '12 at 17:14abcCONTENT def. Unless your tmp-file has, itself, an empty line. – mhelvens Dec 09 '12 at 17:29\LaTeX. See my own answer to see a version without the space. Indeed, it's 'simply' the end-of-line. My question is how to... get rid of it. :-) – mhelvens Dec 09 '12 at 17:44\emptyafter the last token in the last line – egreg Dec 09 '12 at 18:48\LaTeXor even\LaTeXrather than\LaTeX{}.) Had you used repeated\readyou would have indeed gotten a final\par. With\inputit is not a problem of an implicit newline. Of course, this doesn't answer your question about getting rid of this space... – Dec 09 '12 at 18:51read(never tried it). Yes, ending the last line with a comment char or a macro that gobbles space removes the visible space. That's why I used{}in the example. :-) But that doesn't imply an empty line to me, but simply a single eol. – mhelvens Dec 09 '12 at 19:19