3

I have a series of pgfplots-based figures that I compile to pdf images using the standalone package. I currently have one .tex-file per figure. The problem with this is that since it might just be the data file that changes between the .tex-files, this potentially involves a lot of copied code. Consider the following example:

\documentclass{standalone}
\usepackage{tikz, pgfplots}

\begin{document}
\begin{tikzpicture}

\begin{axis}[
xlabel=$x$,
ylabel=$y$]

\addplot[color=black]
table[x=x, y=y] {table1.txt};
%table[x=x, y=y] {table2.txt};
%table[x=x, y=y] {table3.txt};

\end{axis}

\end{tikzpicture}
\end{document}

The above includes my current solution to the problem stated above: I simply uncomment the appropriate row depending on which .txt-file I want to import, and repeat and recompile until all the pdf files have been generated.

Is there a more elegant way of achieving this, preferably automated (so that a bunch of different pdf files are compiled at once)? The closest I've come to a solution is the multi-functionality in standalone, allowing multiple-page outputs. But I'd prefer output in the form of multiple pdf-images.

  • Do you mean making the data file a variable and including it various data file names while keeping the code identical? – percusse Jul 11 '14 at 18:51
  • Yes, the only thing that changes is the filename of the data file. – andreasdr Jul 11 '14 at 18:58
  • This is the sort of thing I would handle using a command line tool (such as a perl script). If no one posts a solely LaTeX solution and you're open to using command line tools, I can illustrate one approach I use. – A.Ellett Jul 11 '14 at 19:07
  • Sure, sounds like a solution as good as any! If you would have the time to formulate it into a full answer, that would be much appreciated! – andreasdr Jul 11 '14 at 19:10
  • No worries, no rush at all! – andreasdr Jul 11 '14 at 19:14
1

Here's a perl script that can achieve what you want:

#!/usr/bin/perl
use File::Copy;
use strict 'vars';

foreach my $fh (@ARGV)
  {
    &MAIN($fh);
  }

sub MAIN {
   my ($source_data) = @_;
   $source_data =~ s/\.tex$//;

   open SOURCE, "<$source_data.tex" or die;
   my @source = <SOURCE>;
   close SOURCE;

   if (not -e "tmp_data.tex" )
     {
       open TMP, ">tmp_data.tex";
       close TMP;
     }

   open TARGET, ">tmp_data.tex" or die;
   foreach my $line ( @source )
     {
       print TARGET $line
     }
   close TARGET;

   system ("pdflatex standalone.tex");
   move ("standalone.pdf", $source_data . ".pdf");
}   

Suppose the data you are using is in the following files:

data_01.tex
data_02.tex
data_03.tex

Also, as your standalone file:

\documentclass{standalone}
\usepackage{tikz, pgfplots}

\begin{document}
\begin{tikzpicture}

\begin{axis}[
xlabel=$x$,
ylabel=$y$]

\addplot[color=black]
table[x=x, y=y] {tmp_data};

\end{axis}

\end{tikzpicture}
\end{document}

If you name the above script publish.pl, then at the command line you'll call

$ perl publish.pl data_01.tex

This script will copy data_01.tex to tmp_data.tex. If then compiles the standalone file using pdflatex. Finally, it moves the pdf to data_01.pdf.

The script is written to allow you to iterate over the command line arguments to create all the individual pdf versions of the data you want. I believe that as long as you have perl installed on your computer, this script should run find regardless of your OS.

5

Imagine you have a standalone document called myplotfile.tex and two data files data1.dat and data2.dat then you can change the file name before the includion of each instance of the same file

\documentclass{article}
\usepackage{filecontents}
\usepackage{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}


\begin{filecontents*}{myplotfile.tex}
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\begin{document}
\begin{tikzpicture}
\begin{axis}[xlabel=$x$,ylabel=$y$]
\addplot[color=black]table[x=a, y=b] {\myfile};
\show\myvar
\end{axis}
\end{tikzpicture}
\end{document}
\end{filecontents*}


\begin{filecontents*}{data1.dat}
a b
1 2
2 4
3 8
4 16
\end{filecontents*}

\begin{filecontents*}{data2.dat}
a b
2  1
4  2
8  3
16 4
\end{filecontents*}


\begin{document}

\def\myfile{data1.dat}
\input{myplotfile}

\def\myfile{data2.dat}
\input{myplotfile}
\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.