Create multiple pdf files from same tex file

Viewed 1813

I have multiple data files (result_a.csv, result_b.csv, ..) and I want to create plot for each one (result_a.pdf, result_b.pdf - or similar). The plot is the same but the input file is different and the output file is different. Is there a way I can run a loop, pass the parameter names from the outside and save the output with a distinctive name?

Assume that my code for creating the plot -

\documentclass[tikz]{standalone}

\usepackage{pgfplots}
\pgfplotsset{ytick style={draw=none}, xtick style={draw=none}}
\usetikzlibrary{patterns}

\newcommand\param{a}

\begin{document}
\begin{tikzpicture}
\footnotesize
    \begin{semilogxaxis}
\addplot[color=red,mark=triangle] table [x=x,y=y,col sep=comma, mark=*] {result_\param.csv}; 
\end{semilogxaxis}
\end{tikzpicture}




\end{document}
2 Answers

Assuming your file is called document.tex, you can pass the name of the csv file to the document by compiling with the following command

pdflatex -jobname="document-b" "\\newcommand*\\version{b}\\input{document}"

and the document:

\documentclass[tikz]{standalone}

% setting a default value in case it is compiled without the newcommand
\unless\ifdefined\version
\def\version{a}
\fi


\usepackage{pgfplots}
\pgfplotsset{ytick style={draw=none}, xtick style={draw=none}}
\usetikzlibrary{patterns}

\newcommand\param{a}

\begin{document}
\begin{tikzpicture}
\footnotesize
    \begin{semilogxaxis}
\addplot[color=red,mark=triangle] table [x=x,y=y,col sep=comma, mark=*] {result_\version.csv}; 
\end{semilogxaxis}
\end{tikzpicture}

\end{document}

If you are working under a Linux Distro or under a Mac OS, you may use a shell script. Let init.tex be the tex file with

\documentclass[tikz]{standalone}

\usepackage{pgfplots}
\pgfplotsset{ytick style={draw=none}, xtick style={draw=none}}
\usetikzlibrary{patterns}

\newcommand\param{a}

\begin{document}
\begin{tikzpicture}
\footnotesize
    \begin{semilogxaxis}
\addplot[color=red,mark=triangle] table [x=x,y=y,col sep=comma, mark=*] {

and let ends.tex be the file containing

}; 
\end{semilogxaxis}
\end{tikzpicture}

The following script reads each .csv file, puts its contents in a tex file organized as follows:

  • init.tex
  • result_x.csv
  • ends.tex

produces the tex file result_xx.tex and compiles it, providing the file result_xx.pdf

#!/bin/bash

for i in $(ls *.csv)
do
    a=$(basename -s .csv $i)
    cp init.tex $a.tex
    echo $i >> $a.tex
    cat ends.tex >> $a.tex
    pdflatex $a.tex

done

It is a naive solution, there are more experienced than me bash--script writers here in SO, but it should work.

PS. Remember to make the script executable to make it work:

chmod 755 myscript.sh

See for example here.

Related