#!/bin/bash # to combine several eps files into a single eps file # Usage: # combineEPS.sh eps-file[s] -o eps-combined.eps -nC nCol -nR nRow # e.g., # Given fig04a.eps, fig04b.eps, fig04c.eps, fig04d.eps, # to create a single 2x2 eps file, fig4.eps, just run: # combineEPS.sh fig04?.eps -o fig4.eps -nC 2 -nR 2 # # dependencies: # latex, dvips, ps2pdf, pdftops, pdfcrop # pdfcrop is from http://pdfcrop.sourceforge.net # http://scriptdemo.blogspot.ca chkcmd() { for nf in $* do isAvailable=`which ${nf} 2< /dev/null` [ ${#isAvailable} -eq 0 ] && echo 0 && exit done echo 1 } # check the dependencies isCMDOkay=`chkcmd latex dvips ps2pdf pdfcrop pdftops` if [ ${isCMDOkay} -eq 0 ]; then isCMDOay=`which latex` isCMDOay=`which dvips` isCMDOay=`which ps2pdf` isCMDOay=`which pdfcrop` isCMDOay=`which pdftops` exit fi nfile=0 isEPSFname=0 isnCol=0 isnRow=0 isDel=0 isCaption=0 [ $# -eq 0 ] && sed -n '2,9p' `which combineEPS.sh` && exit while test -n "$1" do case "$1" in -nC|-nc|-nCol|-ncol|-Ncol) nCol=$2 isnCol=1 shift shift ;; -nR|-nr|-nRow|-Nrow|-nrow) nRow=$2 isnRow=1 shift shift ;; -title|-Title|-Caption|-caption) myCaption=$2 isCaption=1 shift shift ;; -d|-delete|-del|-D) isDel=1; shift ;; -o|-O) epsfname=$2 isEPSFname=1 shift shift ;; *) sufstr=`echo $1 | awk -F. '{print $2}'` if [ ${#sufstr} -eq 0 ]; then tmpfile="$1".eps [ -f ${tmpfile} ] && epsfile[$nfile]=${tmpfile}.eps elif [ $sufstr == "eps" ]; then [ -f $1 ] && epsfile[$nfile]=$1 fi nfile=`expr $nfile + 1` shift ;; esac done [ ${isEPSFname} -eq 0 ] && epsfname="eps-combined.eps" if [ ${isnCol} -eq 1 ] && [ ${isnRow} -eq 0 ]; then nRow=`echo "${nfile}/${nCol}" | bc -l | awk '{ if ($1 == int($1)) {printf "%d\n", $1} else { if ($1>0) {printf "%d\n",int($1)+1 } else {printf "%d\n",int($1)} } }'` fi if [ ${isnRow} -eq 1 ] && [ ${isnCol} -eq 0 ]; then nCol=`echo "${nfile}/${nRow}" | bc -l | awk '{ if ($1 == int($1)) {printf "%d\n", $1} else { if ($1>0) {print "%d\n",int($1)+1 } else {printf "%d\n",int($1)} } }'` fi if [ $isnRow -eq 0 ] && [ $isnCol -eq 0 ]; then nCol=1 nRow=${nfile} fi colstr=`printf "%*s\n" ${nCol} " " | sed -e 's/\ /c/g'` nwidth=$(printf "%.2f\n" `echo "1/${nCol}" | bc -l`) [ ${nwidth} == "1.00" ] && nwidth="" #echo -e "nCol=${nCol}\nnRow=${nRow}\nnWid=${nwidth}" # header of tex file texfile=${epsfname%.*}.tex dvifile=${epsfname%.*}.dvi psfile=${epsfname%.*}.ps logfile=${epsfname%.*}.log auxfile=${epsfname%.*}.aux pdfname=${epsfname%.*}.pdf header[0]='\documentclass[letterpaper,12pt]{article}' header[1]='\usepackage{epsfig}' header[2]='\begin{document}' header[3]='\thispagestyle{empty}' header[4]='\begin{figure}' header[5]='\centering' header[6]="\begin{tabular}{${colstr}}" printf "%s\n" ${header[*]} > ${texfile} n=0 nfileC=`expr ${nfile} - 1` for nL in `seq 1 ${nRow}` do for nC in `seq 1 ${nCol}` do if [ $n -lt ${nfile} ]; then epsname=${epsfile[$n]} if [ ${nC} -eq ${nCol} ]; then if [ ${nL} -eq ${nRow} ]; then tmpstr="" else tmpstr=" \\\\" fi else tmpstr=" &" fi [ ${n} -eq ${nfileC} ] && tmpstr="" figline[$n]="\epsfig{file=${epsname},width=${nwidth}\linewidth,clip=}${tmpstr}" #printf "%s\n" "${figline[$n]}" >> ${texfile} n=`expr $n + 1` fi done done printf "%s %s\n" ${figline[*]} >> ${texfile} # tail of tex file mytail[0]="\end{tabular}" if [ ${isCaption} -eq 1 ]; then mytail[1]="\caption${myCaption}" else mytail[1]="" fi mytail[2]="\end{figure}" mytail[3]="\end{document}" printf "%s\n" ${mytail[*]} >> ${texfile} #compile the tex file latex ${texfile} dvips -t letter -Ppdf -o ${psfile} ${dvifile} && rm -f ${dvifile} ps2pdf ${psfile} && rm -f ${psfile} ${auxfile} ${logfile} [ ${isDel} -eq 1 ] && rm -f ${texfile} # crop the pdf file pdfname=${epsfname%.*}.pdf pdfcrop ${pdfname} && mv ${pdfname%.*}-crop.pdf ${pdfname} && pdftops -eps ${pdfname} ${epsfname} |
Monday, May 7, 2012
[Bash] combine eps files into a single one
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment