0

I made a graph in one latex file using database through pgfplotstableread, because I wouldn't like to have a excel format in my paper.

Now, I would like to export this graph to another latex file. I know I can upload the database and copy and paste the graph code, but it makes my new file always slow to compile.

Since the graph is done, is there any way to export the figure to the new file?

  • unclear if you are simply needing to import built table output (say pdf) into the main-la.tex which you can do with \include pdf – user170109 Mar 26 at 22:59
  • 2
    Welcome to tex.sx. The suggestion to process only this graph into a pdf file and then insert that into the other latex file using \includegraphics is probably going to be the most efficient method, as long as the graph doesn't change. – barbara beeton Mar 27 at 0:04
2

As Barbara suggested, create a separate pdf and include it in every document you like.

1) Put the code to create your graph in a standalone document, let's call it mygraph.tex. Here an example taken from the pgfplotstable tag wiki:

% example taken from https://tex.stackexchange.com/tags/pgfplotstable/info
\documentclass{standalone}
\usepackage{filecontents}
\begin{filecontents*}{pgfplotstable.dat}
# Convergence results
# fictional source, generated 2008
level   dof     error1  error2  info    grad(log(dof),log(error2))      quot(error1)
1       4       2.50000000e-01  7.57858283e-01  48      0               0
2       16      6.25000000e-02  5.00000000e-01  25      -3.00000000e-01 4
3       64      1.56250000e-02  2.87174589e-01  41      -3.99999999e-01 4
4       256     3.90625000e-03  1.43587294e-01  8       -5.00000003e-01 4
5       1024    9.76562500e-04  4.41941738e-02  22      -8.49999999e-01 4
6       4096    2.44140625e-04  1.69802322e-02  46      -6.90000001e-01 4
7       16384   6.10351562e-05  8.20091159e-03  40      -5.24999999e-01 4
8       65536   1.52587891e-05  3.90625000e-03  48      -5.35000000e-01 3.99999999e+00
9       262144  3.81469727e-06  1.95312500e-03  33      -5.00000000e-01 4.00000001e+00
10      1048576 9.53674316e-07  9.76562500e-04  2       -5.00000000e-01 4.00000001e+00
\end{filecontents*}

\usepackage{booktabs}
\usepackage{pgfplots, pgfplotstable}

\pgfplotstableset{% global config, for example in the preamble
% these columns/<colname>/.style={<options>} things define a style
% which applies to <colname> only.
    columns/dof/.style={int detect,column type=r,column name=\textsc{Dof}},
    columns/error1/.style={
        sci,sci zerofill,sci sep align,precision=1,sci superscript,
        column name=$e_1$,
      },
    columns/error2/.style={
        sci,sci zerofill,sci sep align,precision=2,sci 10e,
        column name=$e_2$,
      },
    columns/{grad(log(dof),log(error2))}/.style={
        string replace={0}{}, % erase ’0’
        column name={$\nabla e_2$},
        dec sep align,
      },
    columns/{quot(error1)}/.style={
        string replace={0}{}, % erase ’0’
        column name={$\frac{e_1^{(n)}}{e_1^{(n-1)}}$}
      },
    empty cells with={--}, % replace empty cells with ’--’
    every head row/.style={before row=\toprule,after row=\midrule},
    every last row/.style={after row=\bottomrule}
  }

\begin{document}
% Read the file into a macro and use this to typeset the table
\pgfplotstableread{pgfplotstable.dat}{\loadedtable}

\begin{tikzpicture}
    \begin{axis}
        \addplot table[x=level,y=dof] {\loadedtable};
    \end{axis}
\end{tikzpicture}
\end{document}

2) Compile it to get mygraph.pdf:

enter image description here

3) Put your mygraph.pdf in every document you like with \includegraphics:

\documentclass{article}
\usepackage{caption}
\usepackage{graphicx}

\begin{document}
    \begin{figure}[htb]\centering
        \caption{My wonderful graph}
        \includegraphics{mygraph.pdf}
    \end{figure}
\end{document}

enter image description here

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.