1

I would like to retain the multifile structure of my LaTeX projects, but journals usually require a single .tex file. Is there any way to print such a combined document as a 'cached' file with the help of LaTeX? The solutions provided for this problem (Submitting a journal article as a single tex file) did not seem practical to me.

Here is a trivial MWE:

sub1.tex:

This is the content of sub1.tex.

sub2.tex:

This is the content of sub2.tex.

main.tex:

\documentclass{article}

\begin{document}

This is a test document.

\input{sub1.tex}

\input{sub2.tex}

\end{document}

The combined file would contain the following:

\documentclass{article}

\begin{document}

This is a test document. This is the content of sub1.tex. This is the content of sub2.tex.

\end{document}

TobiR
  • 584
  • https://tex.stackexchange.com/questions/21838/replace-inputfilex-by-the-content-of-filex-automatically – gigiair May 22 '21 at 11:51
  • Since the output of LaTeX is either DVI, PS or PDF (not .tex), no. – John Kormylo May 22 '21 at 14:09
  • input just adds the file at that point so it is only a moment's work with a text editor. latex never constructs the whole document in memory so can not help with this sort of task – David Carlisle May 22 '21 at 17:22

1 Answers1

2

Using the perl latexpand script provided in TeX Live it is possible to get what you want.

Using the same structure as your MWE

.
├── main.tex
├── sub1.tex
└── sub2.tex

and running from a terminal:

latexpand --verbose --keep-comments -o submit.tex main.tex

You get the file submit.tex which contains:

\documentclass{article}

\begin{document}

This is a test document.

This is the content of sub1.tex.

This is the content of sub2.tex.

\end{document}

  • Thanks for posting this answer. Meanwhile it came to my mind that there may be some other latex files (.cls, .sty, *.bst) which could be included in the merged document file. However, performing this actions does not seem to be a trivial task even if one can suppose that the files to be included are in the file subdirectories of a main project directory. I thought of writing a small program which pastes the content of ALL the auxiliary files within filecontents before their first use, but each file type requires separate and difficult treatment. – TobiR May 22 '21 at 19:08
  • In that case you should do this "by parts", first you run latexpand, then you run bundledoc. Although I would simply use filecontents in the submit.tex file to add .bst and extra stuff. – Pablo González L May 22 '21 at 19:13
  • @TobiR bundledoc already does that job :D – Pablo González L May 22 '21 at 19:16
  • @TobiR If you dare to write your own script keep in mind the verbatim content and commented lines with macros, there things can get a bit complicated (I went through this situation some time ago). – Pablo González L May 22 '21 at 19:19
  • Thanks Pablo. Do you know a Windows version of bundledoc? Which dependencies does this program have? By the way, does the .dep file contain the dependencies in their precedences? Can one rely on this file to write a small code by specifying the standard packages and including the non-standard files without version numbers? – TobiR May 22 '21 at 19:23
  • @TobiR You must have a version of perl (ActiveState or Straberry) installed. The .dep file is more or less the same information that -recorder gives you, but it is easier to filter (easy is relative). The standard package thing is a trial and error thing, but logic says that any package that you haven't customized yourself is standard. – Pablo González L May 22 '21 at 19:28
  • Pablo, I realized that we have bundledoc on our computer cluster (under Linux), so even our group leader will be able to run it easily (we cannot expect spending too much time with this file conversion from him ;)). Is it sufficient to use bundledoc to get a single document from a single project directory (with subdirectories)? Could you please help me with the command to run this script? – TobiR May 22 '21 at 19:37
  • @TobiR Give me a minute to do a test – Pablo González L May 22 '21 at 19:41
  • OK, thanks, just take your time. – TobiR May 22 '21 at 19:42
  • You will still need two steps, first latexpand, then add RequirePackage{snapshot} to submit.tex, then run pdflatex submit.tex and finally bundledoc --config=texlive-unix.cfg --verbose submit.dep. You should check the bundledoc documentation, I think it is easier to generate a configuration file and then run it. – Pablo González L May 22 '21 at 19:50
  • Oh, I see. The only problem is that we compile our LaTeX projects under Overleaf, so the LaTeX packages are not up-to-date on our cluster. It would be useful to skip the "pdflatex submit.tex" step somehow which cannot work properly on our computer system. – TobiR May 22 '21 at 19:59
  • @TobiR This step is only for creating the .dep file. You should keep in mind that you will run into another problem along the way (I see it in the future), do you have images loaded in separate directories? ... If so, you will have to make other adjustments :) ...more work more fun :D – Pablo González L May 22 '21 at 20:05