Using `\input` to insert a section of a document into another
You could use the catchfilebetweentags
package.
In your main file:
\usepackage{catchfilebetweentags}
....
\ExecuteMetaData[file.tex]{graph}
and in your file.tex
%<*graph>
code for the graph
%</graph>
You can set up your file containing the code for the graphs like
% from https://tex.stackexchange.com/a/234521/4427
\GRAPH graph1
\begin{tikzpicture}
\begin{axis}[ylabel={Y label},xmin=-1,xmax=1,width=3in,height=2in]
\addplot coordinates {(-1,-1) (1,1)};
\coordinate (NE) at (rel axis cs: 1,1);% upper right corner of axis
\end{axis}
\path (-15mm,-5mm) ($(NE)+(3mm,2mm)$);
%\draw[red] (-15mm,-5mm) rectangle ($(NE)+(3mm,2mm)$);% to fine tune offsets
\end{tikzpicture}
\ENDGRAPH
\GRAPH graph2
\begin{tikzpicture}
\begin{axis}[xmin=-10,xmax=10,width=3in,height=2in]
\addplot coordinates {(-10,-10) (10,10)};
\coordinate (NE) at (rel axis cs: 1,1);
\end{axis}
\path (-15mm,-5mm) ($(NE)+(3mm,2mm)$);
%\draw[red] (-15mm,-5mm) rectangle ($(NE)+(3mm,2mm)$);% to fine tune offsets
\end{tikzpicture}
\ENDGRAPH
(the first comment is just to show comments are honored and to state the source for the code). I saved it as murfitt-graphs.tex
, but the name is arbitrary and you can have several files like this. The structure is important: the line \GRAPH
should be followed by the symbolic name of the graph (whatever string of ASCII characters you want).
Now your main document should have the following code; the relevant part is between %% define \inputgraph
and `%% end
\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{calc}
%% define \inputgraph
\newcommand{\inputgraph}[2]{% #1 = file, #2 = graph name
\long\def\GRAPH ##1#2 {}%
\input{#1}
}
\let\ENDGRAPH\endinput
%% end
\begin{document}
\inputgraph{murfitt-graphs}{graph2}
\inputgraph{murfitt-graphs}{graph1}
\inputgraph{murfitt-graphs}{graph2}
\end{document}
What's the advantage over catchfilebetweentags
? That here the code is not read in as the argument of a macro. In case you use a wrong label, you'll get an error
Runaway argument?
graph1 \begin {tikzpicture} \begin {axis}[ylabel={Y label},xmin=-1,xm\ETC.
! File ended while scanning use of \GRAPH.
and no graph will be processed.
With the clipboard
package:
graphs.tex:
\documentclass{article}
\usepackage{clipboard}
\newclipboard{mygraphs}
\begin{document}
Something ...
\Copy{graph1}{Code of graph 1}
\Copy{graph2}{Code of graph 2}
\Copy{graph1}{Code of graph 3}
More code ...
\end{document}
main.tex:
\documentclass{article}
\usepackage{clipboard}
\openclipboard{mygraphs}
\begin{document}
A nice graph:\Paste{graph2}
\end{document}
You must run pdflatex
in the document with the \Copy
first and then compile the document/s with \Paste
commands.