7

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

  • there are packages that do this but it is much simpler to just put the shared part into a separate tex file that you \input into each document. – David Carlisle Jul 7 '17 at 9:29
  • @DavidCarlisle That is sort of the idea, however there are lots of graphs, so I wanted to put them all in the same tex file and reference that as required, rather than having a separate file for each graph. – DividedByZero Jul 7 '17 at 9:34
7

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}

enter image description here

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.

|improve this answer|||||
7

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>
|improve this answer|||||
3

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.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.