I was wondering if there is a way in which I can reuse the code, for example one figure, that is written in one file into another file.

I want to use something similar to xr package that allows you to reuse the labels references from one file into another. But I want to use it in the figures code or in entire paragraphs.

To illustrate what I need to do lets assume I have a file A.tex in which I already define a figure. And I have another file B.tex. And now I want to use the same definition of the figure in B. However, I want to "link" to the figure code in A; because if I do some change in A, I don't have to copy and paste everything in B again. Also, I want to do this with paragraphs. It is kind of quoting the previous file.

Is it possible using LaTeX? What do you suggest me to do?


I need to do this "copy & paste" to include text and figures from the main document, into a second one. This references should have the same number of figures, and same bibliography reference, etc. That's why I say that I need something similar to xr package.

I tried using \include and \input to see if it was readable. However, I run into another problem. The number of figures and references are not maintained in the second document. The \include creates new numbers for each document. But I need to maintain the same as the original document.

Is there a way to do this "copy & paste" automatically?

  • 1
    If the two documents are quite similar, i.e. only differ in some paragraphs, the package "versions" may be helpful. Aug 31, 2011 at 12:34
  • @Christoph: nice addition! I converted it to a comment, because it's not really an answer.
    – Stefan Kottwitz
    Sep 3, 2011 at 12:29

3 Answers 3


The standalone package is perfectly suited for this kind of task. As mentioned in About \include in LaTeX use the following for the figure:


Then, where you need this figure you you \input the figure. A good example is provided here.

This also allows you to fine tune the figure and reuse it as desired. Here is an example. Save the following as Ellipse.tex:



\coordinate (EllipseOrigin) at (0,0);

\draw [blue, thin, ->] (-5,0) -- (5,0) node [right] {$x$};
\draw [blue, thin, ->] (0,-4) -- (0,4) node [above] {$y$};

\draw [red, ultra thick]% Graph Ellipse
    (EllipseOrigin) ellipse [x radius=\XRadius,y radius=\YRadius];


Then where you want to use this:


Here is the graph of an ellipse:


If you want the exact same content (same Figure numbers) you should consider using pdfpages:


  • Hi, thanks for the pointer to the standalone package. I was able to compile the first part of your example. However, the last part, in which you use the pdfpages, does not compile. Is there a special way to compile it?
    – adn
    Sep 2, 2011 at 5:38
  • Also, I tried to add the figure environment into Ellipse.tex to check the figures numbers. But, it stop compiling when I added it. :(
    – adn
    Sep 2, 2011 at 6:03
  • Have corrected the typo in the pdfpages example. Not sure why the Figure did not work for you, but I prefer to have the Figure environment where the Figure is used (ie the main document) instead of the standalone file. Sep 2, 2011 at 15:52
  • Actually, what I need is test the ability of standalone to handle the figures numbers when they are alone. Because, if I create, let say, picture 20. Then I need to insert that same picture in the derive.tex document. That image should have again the same image number: 20. However, when I put the figure environment inside the standalone file, it doesn't allow me to compile. I get some errors ! LaTeX Error: Not in outer par mode ! Undefined control sequence. \@xfloat ...@fltovf \fi \global \setbox \@currbox \color@vbox \normalcolor \...
    – adn
    Sep 3, 2011 at 5:19
  • Ok, after some reading I found out that the problem with the "Not in outer par mode" error is because I want to put the figure environment within another environment that will restrict its float-ability. I imagine that the standalone creates some environments in which it places all the content the user defined. So, back to square one. Ideas?
    – adn
    Sep 7, 2011 at 8:11

You can move the code that needs to be used in multiple documents to a separate .tex file. Then, in each document write


or just


in the exact place where you would want to include the .tex file. \input will cause TeX to read the file and treat its content as if it was written directly instead of \input. If you know C or C++, then \input is very similar to the #include preprocessor directive.

  • Hi, thanks for the quick answer. However, I wanted to avoid this approach because I have several figures and paragraphs that I need to refer to. Then the original document will be really hard to read. I was hopping to find a clean solution that allow me to maintain both documents readable. The main idea is to be able to quote parts of the first document into the second one, and do not re-write the quotes when the first one change.
    – adn
    Aug 31, 2011 at 12:03
  • @adn: This approach won't be so hard to read, you can give the fragments proper names. What you want (like xr) can be implemented, but needs one more compilation.
    – Leo Liu
    Aug 31, 2011 at 12:23

You could put any code that wanted to re-use into your own package, and name it something like mypackage.sty, and then include it in the relevant documents using \usepackage{mypackage.sty}.

Below is a sample MWE:


 Your paragraph text goes here.%




 hello world



Note that if you plan to use the mypackage.sty in documents that reside in different directories, you will have to put it in a place where TeX will know to look for it.

  • Is there a way to use this definitions within the .tex file? So I can define it in the normal document and then just referencing it in the other document? Or should I stick with the .sty files?
    – adn
    Aug 31, 2011 at 13:33
  • If you put definitions in one .tex file, then in order to access these commands in another .tex file you would need to use \input or \include which might lead to undesired results.
    – cmhughes
    Aug 31, 2011 at 13:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .