# Insert a tex file pdf inside another tex file dynamically

Suppose that both tex1.tex and tex2.tex are two independent tex files in the sense that both compile and generate a pdf file independently. I would like to include the output of tex2.tex in the tex1.tex dynamically. I want to achieve this with one step of just compiling tex1.tex file ( and latex automatically do the second compile as a prerequisite step if I have referred to its result)

My Question: Is there any mechanism (like some instruction that can be added to tex1.tex) telling latex that when I am compiling tex1.tex it should first compile tex2.tex to its corresponding pdf, and then compile tex1.tex document with the tex2.pdf file included as a picture inside it.

Update:

I am adding example files for tex1.tex and tex2.tex to make the question more concrete: suppose my tex1.tex is as bellow:

\documentclass[11pt]{beamer}
\usetheme{default}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\author{Qartal}
\title{Inserting output of a tex file as pdf in one step}

\begin{document}
\maketitle

\begin{frame}
\frametitle{}
I want to inset bellow this line my diagram from file (tex2.tex):
\end{frame}
\end{document}

I want to inset bellow this line my diagram from file (tex2.tex):

\end{document}


and my tex2.tex file is as bellow:

% TikZ chains with labeled edges
% Author: Stefan Kottwitz , http://texblog.net
\documentclass[a4paper,10pt]{article}
\usepackage{tikz}
%%%<
\usepackage{verbatim}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}%
%%%>
\usetikzlibrary{arrows,chains,matrix,positioning,scopes}
%
\makeatletter
\tikzset{join/.code=\tikzset{after node path={%
\ifx\tikzchainprevious\pgfutil@empty\else(\tikzchainprevious)%
edge[every join]#1(\tikzchaincurrent)\fi}}}
\makeatother
%
\tikzset{>=stealth',every on chain/.append style={join},
every join/.style={->}}
\tikzstyle{labeled}=[execute at begin node=$\scriptstyle, execute at end node=$]
%
\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of math nodes, row sep=3em, column sep=3em]
{ 0 & A  & B  & C  & 0 \\
0 & A' & B' & C' & 0 \\ };
{ [start chain] \chainin (m-1-1);
\chainin (m-1-2);
{ [start branch=A] \chainin (m-2-2)
[join={node[right,labeled] {\eta_1}}];}
\chainin (m-1-3) [join={node[above,labeled] {\varphi}}];
{ [start branch=B] \chainin (m-2-3)
[join={node[right,labeled] {\eta_2}}];}
\chainin (m-1-4) [join={node[above,labeled] {\psi}}];
{ [start branch=C] \chainin (m-2-4)
[join={node[right,labeled] {\eta_3}}];}
\chainin (m-1-5); }
{ [start chain] \chainin (m-2-1);
\chainin (m-2-2);
\chainin (m-2-3) [join={node[above,labeled] {\varphi'}}];
\chainin (m-2-4) [join={node[above,labeled] {\psi'}}];
\chainin (m-2-5); }
\end{tikzpicture}
\end{document}

• Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – user31729 May 21 '15 at 23:56
• @ChristianHupfer I added example files. – qartal May 22 '15 at 0:17
• Your example files are not really minimal. I am not sure if your are not after externalization really... – user31729 May 22 '15 at 0:20
• I will check later on if your files work with bashful – user31729 May 22 '15 at 0:32
• Have you considered a Makefile? – Hagen von Eitzen May 22 '15 at 8:32

## 2 Answers

Perhaps this is what you want: Use bashful package, which allows the execution of shell commands or external programs (I can test this only on Linux right now)

The external program(s)/commands must be 'enclosed' in a

\bash
...
\END


pair.

Note: The compilation of the 2nd file is done each time when the outer wrapper document is compiled.

tex1.tex

\documentclass{article}

\usepackage{pdfpages}
\usepackage{bashful}
\begin{document}

\bash[stderr]
pdflatex tex2ext
\END

\includepdf{tex2ext.pdf}

\end{document}


tex2ext.tex

\documentclass{article}

\usepackage{blindtext}
\begin{document}
\blindtext[2]

\end{document}

• I omitted a screen shot, since \blindtext is quite well-known – user31729 May 22 '15 at 0:04

Looks like a job for shell escape (\write18):

\documentclass{article}
\begin{filecontents*}{\jobname2.tex}
\documentclass{article}
\usepackage{blindtext}
\begin{document}
\blindtext[2]
\end{document}
\end{filecontents*}
\usepackage{pdfpages}
\immediate\write18{pdflatex \jobname2}
\begin{document}
\includepdf{\jobname2.pdf}
\end{document}


I've used the same demo files as Christian Hupfer's answer but have run them together (using the filecontents environment) to make things a little smoother.

To get this to work you'll need to allow shell escape, so

pdflatex --shell-escape <filename>


at the command line or the equivalent in your editor. Note that there is a security risk with \write18 as in theory any instruction could be passed by a third-party file.