Is there any way one can use \input to insert only a specific section of a document into another? I intend on using this to improve the readability of an article with lots of pgfplots graphs in it, by moving all of my graphs to another file, and using something like \input{file.tex:graph1} to insert each graph where it is needed, rather than have all of them displayed together
- 173
4 Answers
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.
- 1,121,712
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>
- 158,329
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.
- 80,769
Improving the accepted answer, if you want to be able to compile your murfitt-graphs.tex, write this instead:
\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{calc}
%-------------------------------------------------------------------------------
% Escape command
\newcommand{\GRAPH}[0]{}
\newcommand{\ENDGRAPH}[0]{}
%-------------------------------------------------------------------------------
\begin{document}
\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
\end{document}
The \newcommand{\COMMANDNAME}[0]{} is used as escape command. Inspiration from here.

\inputinto each document. – David Carlisle Jul 07 '17 at 09:29