Showing posts with label latex. Show all posts
Showing posts with label latex. Show all posts

Sunday, April 7, 2013

[Bash] extractBib: extract bib items from a source bibtex database for a given tex file

#!/bin/bash
# extract bib items from a source bibtex database for a given tex file
# usage:
#          extractBib tex-file bibtex-database output-bibtex-file
# e.g.,
#          extractBib mypaper.tex mybibtexdatabase.bib mypaper.bib
# http://scriptdemo.blogspot.com

function getkeys()
{
   texfile=$1
   mykeys=`grep "cite.*{.*}" ${texfile} | sed -e 's/\ //g' | awk '{for (i=1; i<=NF;i++) {if ($i ~ /cite/) {print $i}}}' | sed -e 's/\}/\n/g' | awk -F\{ '{print $2}' | sed -e 's/\,/\n/g' | sed '/^[\ ]*[\ ]*$/d' | sort | uniq | sed '/^\\\\/d' | sed '/:/d'` 
   echo ${mykeys}
}

if [ $# -eq 3 ]; then
   texfile=$1; bibfile=$2; outfile=$3
elif [ $# -eq 2 ]; then
   texfile=$1; bibfile=$2; outfile="extractbib_"$1
else
   echo "usage:"
   echo "          extractBib tex-file bibtex-database output-bibtex-file"
   exit
fi

# make sure no overwrite
[ -e ${outfile} ] && echo "${outfile} exists alreay! exit" && exit

allkeys=`getkeys ${texfile}`
if [ ${#allkeys[*]} -ne 0 ]; then
   touch ${outfile}
   for eachkey in ${allkeys[*]}
   do
       #echo ${eachkey}
       sed -ne "/${eachkey}/,/^[\ ]*\}$/p" ${bibfile} >> ${outfile}
   done
fi

Thursday, July 5, 2012

[Bash] remove the temporary files generated by latex

#!/bin/bash
# to clean the "*.blg *.bbl *.aux *.spl" files
# usage:
#          cleanlatex option
#                          -l : just list those files
#                          -f : remove by force
#                      -dvi : dvi files as well
#                  -name : only those with a specifc filename
#    e.g.,
#          cleanlatex -l
#          cleanlatex -f -name pdftest -dvi
#          http://scriptdemo.blogspot.com   

[ $# -eq 0 ] && sed -n '3,11p' `which cleanlatex` && exit

isFLname=0
isForce=0
isList=0
isDvi=0
isDebug=0
fname='*'

while test -n "$1"
do
     case "$1" in
      -l|-L)
        isList=1
        shift
        ;;
      -f|-F|-force|-del|-remove)
        isForce=1
        shift
        ;;
      -dvi)
        isDvi=1
        shift
        ;;
      -name)
        fname=$2
        fname=${fname%.*}
        shift;
        shift;
        ;;
      -debug|-Debug)
        isDebug=1
        shift
        ;;
      *)
        echo "unkown option: $1"
        shift
        ;;
     esac
done

[ ${isList} -eq 1 ] && isForce=0
flist="${fname}.spl ${fname}.aux ${fname}.bbl ${fname}.blg ${fname}.log"
[ ${isDvi} -eq 1 ] && flist="${flist} ${fname}.dvi"
if [ ${isForce} -eq 0 ]; then
   if [ ${isDebug} -eq 1 ]; then
      echo "ls ${flist}"
   else
      eval "ls ${flist} 2>/dev/null"
   fi
   exit
else
   if [ ${isDebug} -eq 1 ]; then
      echo "rm -f ${flist}"
   else
      eval "rm -f ${flist} 2>/dev/null"
   fi
fi

Wednesday, June 6, 2012

[Bash] ps2png: convert ps to png format with high quality

previous solution using ImageMagicK works too, but may not produce the high quality expected.

#!/bin/bash
# convert a single-page ps file into png format using gs
# usage:
#            ps2png psfile.ps
# Note:
#           1: need gs installed
#           2: same name is used for the png file
#           http://scriptdemo.blogspot.com

if [ $# == 0 ]; then
   sed -n '2,4p' `which ps2png`
   exit
else
  for psname in $*
  do
     if [ -e $psname ]; then
        pngname=`echo ${psname%%.*}`
          gs -r300 -dTextAlphaBits=4 -sDEVICE=png16m -sOutputFile=${pngname}.png -dBATCH -dNOPAUSE ${psname}
     else
        echo "${psname} does not exist!"
     fi
  done
fi

Friday, May 18, 2012

[Bash] ris2bib: convert RIS citation file to bibtex format

#!/bin/bash
# convert ris format to bibtex format
# usage:
#          ris2bib.sh ris-file(s)
#          www.scriptdemp.blogspot.ca

getCTag()
{
   for cTag in $*
   do
      nl=`grep "^${cTag}" ${ris} | wc -l`
      [ ${nl} -ne 0 ] && echo "${cTag}" && exit
   done
   echo ""
}

[ $# -eq 0 ] && sed -n '2,5p' `which ris2bib.sh` && exit

for ris in $*
do
    if [ -e ${ris} ]; then
       dos2unix ${ris}
       auTag=`getCTag AU A1 A2`
       tlTag=`getCTag T1 TI`
       joTag=`getCTag JO JA`
       yrTag=`getCTag PY Y1`
       doTag=`getCTag M3 DO`

       voTag="VL"; spTag="SP"; epTag="EP"; urTag="UR"; isTag="IS"
       publisher=`grep "^PB\ " ${ris} | awk -F\- '{print $2}' | sed -e 's/^\ //g'`

       authorList0=`grep "^${auTag}\ " ${ris} | awk -F\- '{print $2"@"}'`
       authorList=`echo ${authorList0} | sed -e 's/^\ //g' -e 's/\.\ /\./g' -e "s/\@$//" | sed -e 's/@/\ and\ /g'`

       titleStr=`grep "^${tlTag}\ " ${ris} | cut -c7-`
       jourStr=`grep "^${joTag}\ " ${ris} | awk -F\- '{print $2}' | sed -e 's/^\ //g'`
       volStr=`grep "^${voTag}\ " ${ris} | awk -F\- '{print $2}' | sed -e 's/\ //g'`
       SPStr=`grep "^${spTag}\ " ${ris} | awk -F\- '{print $2}' | sed -e 's/\ //g'`
       EPStr=`grep "^${epTag}\ " ${ris} | awk -F\- '{print $2}' | sed -e 's/\ //g'`
       if [ ${#EPStr} -ne 0 ]; then
          pageStr="${SPStr}--${EPStr}"
       else
          pageStr=${SPStr}
       fi
       if [ ${#isTag} -ne 0 ]; then
          isnStr=`grep "^${isTag}\ " ${ris} | cut -c7-`
       fi
       yearStr=`grep "^${yrTag}\ " ${ris} | awk -F\- '{print $2}' | sed -e 's/\ //g' | cut -c-4`
       if [ ${#doTag} -ne 0 ]; then
          doiStr=`grep "^${doTag}\ " ${ris} | awk -F\- '{print $2}' | rev | awk -F\: '{print $1}' | rev | sed -e 's/^\ //g'`
       fi

       if [ ${#urTag} -ne 0 ]; then
          urlStr=`grep "^${urTag}\ " ${ris} | awk -F\- '{print $2}' | sed -e 's/^\ //g'`
       fi

       #generate the key
       mykey=`echo ${authorList} | awk -F\, '{print $1}'`
       mykey="${mykey}${yearStr}"
       titleKey=`echo ${titleStr} | awk '{print $1""$2}'`
       mykey=`echo "${mykey}${titleKey}" | awk '{print tolower($0)}' | sed -e 's/\-//g'`
       echo ""
       echo @article{"${mykey},"
       echo " author={${authorList}},"
       echo " title = {{${titleStr}}},"
       echo " journal = {${jourStr}},"
       echo " year = {${yearStr}},"
       echo " volume = {${volStr}},"
       [ ${#isnStr} -ne 0 ] && echo " number = {"${isnStr}"},"
       [ ${#doiStr} -ne 0 ] && echo " doi = {"${doiStr}"},"
       [ ${#urlStr} -ne 0 ] && echo " url = {${urlStr}},"
       if [ ${#publisher} -eq 0 ]; then
          echo " pages = {${pageStr}}"
       else
          echo " pages = {${pageStr}},"
          echo " publisher = {${publisher}}"
       fi
       echo " }"
       unset auTag tlTag joTag voTag spTag epTag isTag
   else
      echo ""
      echo "${ris} is not found!"
      echo ""
   fi
done

Monday, May 7, 2012

[Bash] combine eps files into a single one

#!/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}

ShowCalendar