1

I have a latex document like the below

\documentclass{article}
\usepackage{graphicx}
\newcommand{\plott}[1]{
\begin{figure}
        \includegraphics{#1}
\end{figure}
}

\begin{document} \plott{"c:\documents\chart1.pdf"} \plott{"c:\downloads\chart2.pdf"} \end{document}

Now as well as chart1.pdf and chart2.pdf I have files chart1.csv and chart2.csv (which live in the same folders as these charts) which contains the data that went into make the chart.

I want to add logic to \plott to copy these csvs to a third folder so that when I compile the document I get not only a document but also a folder containing all of the input data. Is it possible for latex to copy external files like this?

Stuart
  • 113

1 Answers1

2

Using \immediate\write18 you can run an external program from a .tex file, so

\newcommand{\plott}[2][]{
  \immediate\write18{cp #2 plott}
   \begin{figure}
    \includegraphics[#1]{#2}
   \end{figure}
}

(1) will include #2 in the output and

(2) will copy it into the plott folder.

Requirements for this solution:

(1) You are using a Unix-like OS.

(2) A TeX Live distribution is installed on your system.

(3) A folder named plott exists in the same directory of the main .tex file. You can create it automatically with \immediate\write18{mkdir plott} if you prefer.

A minimal working example

I used two images from TeX Live, but you can change the path, of course, and use other files:

\documentclass{article}
\usepackage{graphicx}

\newcommand{\plott}[2][]{ \immediate\write18{cp #2 plott} \begin{figure} \includegraphics[#1]{#2} \end{figure} }

\begin{document} \plott{/usr/local/texlive/2020/texmf-dist/tex/latex/mwe/example-image-a.jpg} \plott{/usr/local/texlive/2020/texmf-dist/tex/latex/mwe/example-image-b.jpg} \end{document}

Ivan
  • 4,368