# \end{filecontents} and \input{} on the same line

This code generates output that shows contents of excerpt1.tex

\documentclass{article}
\usepackage{filecontents}

\begin{document}
Pragraph is starting.
\begin{filecontents}{excerpt1.tex}
Part that must be in two documents.
\end{filecontents}
\input{excerpt1.tex}
Paragraph ends.
\end{document}


This code does not generate output that shows contents of excerpt1.tex

\documentclass{article}
\usepackage{filecontents}

\begin{document}
Pragraph is starting.
\begin{filecontents}{excerpt1.tex}
Part that must be in two documents.
\end{filecontents}\input{excerpt1.tex}
Paragraph ends.
\end{document}


I wonder the reason behind this situation. I have encountered this anomaly in a very important document. Is there a list of such caveats that must be taken into account before any irrecoverable errors happen?

• I don't know how filecontents is implemented, but some environments are defined to ignore what follows (on the same line) \begin{...} (one example would be lstlisting) and/or \end{...}. It's both safer and preferable (from a code-formatting standpoint) to put \input{...} on the next line rather than stick it on the same line as \end{filecontents}. Is there a specific reason why you want to have them on the same line? – jub0bs Mar 23 '14 at 6:56
• the behavior is documented: The begin and end tags should each be on a line by itself. (texdoc filecontents), and also a warning is written to the .log file: LaTeX Warning: Ignoring text '\input{excerpt1.tex}' after \end{filecontents} on input line 8. This behavior is from the original filecontents environment as provided by the LaTeX kernel in file ltclass.dtx; package filecontents extensions have not changed that aspect. – user4686 Mar 23 '14 at 7:16
• Dear @Jubobs, thanks for the invaluable recommendation about code formatting. My reason is to remove any spaces at the beginning and end of the excerpt. Obviously, my solution is not feasible. – Kadir Mar 23 '14 at 8:24

As noted in the comments, text from \end{filecontents} up to the end of the line is ignored. Thus the space before the excerpt is not caused by having \input on the next line. In fact the space before the excerpt is caused by the line break after 'Paragraph is starting.' The simplest way to get rid of this is to use percentage signs at the end of the line. Similarly, you need percentages after 'two documents.' and \input{excerpt1.tex} to prevent a space after the excerpt.

\documentclass{article}
\usepackage{filecontents}
\begin{document}
Paragraph is starting.%
\begin{filecontents}{excerpt1.tex}
Part that must be in two documents.%
\end{filecontents}
\input{excerpt1.tex}%
Paragraph ends.
\end{document}


One of these can be automated using \ignorespaces.

\documentclass{article}
\usepackage{filecontents}
\newcommand\myinput[1]{\input{#1}\ignorespaces}
Paragraph is starting.%
\begin{filecontents}{excerpt1.tex}
Part that must be in two documents.%
\end{filecontents}
\myinput{excerpt1.tex}
Paragraph ends.
\end{document}