Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have a document that uses some \input commands and creates the bibliography via an external .bib file and bibtex.

Now I have to inline the \input commands (i.e. replace each \input command with the content of the corresponding file) and inline the bibliography (i.e. replace the bibliography command with the .tex code which is generated by bibtex). The inlining is required by the editor.

How can I do this automatically?

share|improve this question

5 Answers 5

up vote 1 down vote accepted

Before your \documentclass, add this:

Contents of file 1

Contents of file 2

And so on. No need to replace the \inputs

share|improve this answer
On a second read of the question, what you want to do is put mainfile.bbl in a filecontents environment before your documentclass call. –  Seamus Feb 7 '11 at 21:33
Good outside-the-box-answer! ATM I find that this is a relatively elegant solution to the problem. I wrote a small shell script to automatically creates these environments for a document - I posted it as another answer. –  maxschlepzig Feb 10 '11 at 20:44

rename the generated file.bbl to mybib.bbl and then replace your \bibliography command with \input{mybib.bbl}. If you are sure, that you won't run the bibtex program again, than simply replace it with \input{file.bbl}.

http://texnik.dante.de/misc/buildFile.pl is a Perl skript which creates one stand alone file. All \input and \include will be replaced by their contents

share|improve this answer
Ok, this answers the second part. At the end I want to automatically replace the \input commands as well. –  maxschlepzig Feb 7 '11 at 21:04
@maxschlepzig: http://texnik.dante.de/misc/buildFile.pl is a Perl skript which creates one stand alone file. All \input and \include will be replaced by their contents –  Herbert Feb 7 '11 at 21:33

Thinking outside the box for a moment, I'd be tempted to do something like use a text editor (or a sed script) to change the \input directives to a suitable #include and then pass the result through the C Preprocessor.

It would also be straight-forward to implement the effect of \input with a Perl script.

I'm sure it is possible to do this in pure TeX, but the purity may not be worth the effort if there isn't an existing mechanism to ask TeX to dump the token stream back to a file at the right moment in its process.

Note that regardless of implementation choice, I would make sure that I didn't accidentally destroy the original document with this replacement.

share|improve this answer
Yeah, the plan is to call the solution via a Makefile an create the inline version as a new file. –  maxschlepzig Feb 7 '11 at 21:47

Similar questions arise from time to time of (essentially) getting TeX to output a .tex file. The most practical way I found so far is to make all characters active (or variants that essentially amount to the same thing), and define them to store themselves to a buffer, which is then output to a file.

Then, the commands for which we wish to do more than just copy verbatim are redefined to do what we want (see the definition of \RLK@input below). There are subtelties, of course (only letters can appear in control sequence names, and not active characters).

Change InFile.tex to the file you want to "inline", and OutFile.tex to the output.




  \advance\count0 by 1\relax%
  \input \RLKinputfile %


  % catches the control word, naively stopping at the first non A-Za-z
  \@catchcs@false% We have reached the end...
  \@catchcs@true% ...except if we are in the range A-z...
  \@catchcs@false% ...and not in the range Z-a.
  % If we haven't reached the end, catch one more.
  % If we have, stop, do the relevant \RLK@... if it exists, otherwise
  % just output to the buffer. And don't forget to put #1 back in the stream.
  {\ifcsname RLK@\RLKcatchcs@csname\endcsname%
    \csname RLK@\RLKcatchcs@csname\expandafter\endcsname%

% Define \input{...} to have the desired behaviour: actually input the file.
% We could do the same for other commands (\usepackage, etc.)
\gdef\RLKaux@input#1{\expandafter\input \detokenize{#1} }

% Here it goes, we act.

% Then output the \RLKbuffer to a file.
\immediate\openout\RLKwrite \RLKoutputfile\relax

% tex, pdftex will be stopped there, 
% but latex, pdflatex will only see \relax.
\csname bye\endcsname

% this makes latex, pdflatex happy.

This should work with tex, latex, pdftex and pdflatex, and it shouldn't be hard to extend to other variants. Also, before people ask, RLK is just a random string of letters.

share|improve this answer

Using Seamus hints I wrote a small shell scripts that automatically inserts the necessary filecontents environments:

(for simplicity, it is assumed that all filenames don't contain any whitespace and that all input statements start at the fist column)


set -e
set -u


rm -rf inl2.tmp inl.tmp final
mkdir final

$SED 's@\\bibliography{'$BIB'}@\\input{'$BBL'}@' $TEXFILE > inl.tmp

for i in `grep '^\\\\input' inl.tmp | $SED 's/^[^{]\+{\([^}]\+\)}.*$/\1/'`; do
  if echo $i | grep '\.' > /dev/null ; then
  echo $file
  echo >> inl2.tmp
  printf '\\begin{filecontents}{'$i'}' >> inl2.tmp
  echo >> inl2.tmp
  cat $file >> inl2.tmp
  printf '\\end{filecontents}' >> inl2.tmp
  echo >> inl2.tmp
  echo >> inl2.tmp

cat inl2.tmp inl.tmp > $OUTPUT
share|improve this answer

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.