なんだこれは

はてなダイアリーから移転しました。

eq2imgだ。

こいつの引数が大変すぎるから、mkimgが必要なんだ。実は~/.eq2imgというファ
イルを直読みするので~/.eq2imgに書き込み権限を許すと任意のコマンドが実行
可能なので注意しろよ。

#!/bin/bash
# eq2img -- equation part of tex source to gif/png converter 
#             using platex , dvips and ImageMagick --

TimeStamp="Time-stamp: <06/08/14 01:43:10 fu7mu4>"
Version="1.0macosx-vine"
ScriptName=$(basename "$0")
INITFILE="${HOME}/.eq2img"

### CODE ###
ShowUsage () {
    cat <<EOF
$ScriptName Script for making transparent gif/png
$ScriptName :USAGE
$ScriptName {-h|-v}
$ScriptName [-ne|-nq|-c color|-hf headderfile|-of outputfile|eqfile]
-h :show help and quit
-v :show version and quit

-g / -p  : convert to gif/png format 
-e / -np : with/without eps
-q / -nq : need/remove equation file after this run
-m / -nm : with/without message
-w / -nw : with/without warning
-i / -ni : input/don't input tex command directory

-c color : change another foregrounf color 
-hf file : change headder file name
-of file : change output file name
 
EOF
}

ShowPara () {
    cat <<EOF
$ScriptName Script is now configured values ...

INITFILE $INITFILE
DEFHEADDER $DEFHEADDER
SOURCE $SOURCE
OUTPUT $OUTPUT
TMPDIR $TMPDIR
Format $Format
Color $Color
NeedEPS $NeedEPS
NeedEq $NeedEq
EOF
}

### read from init file ###
if [ -r "$INITFILE" ] ; then
    . "$INITFILE"
fi
### read from arguments of this script
while [ "$1" != "" ]
do
    if [ -r "$1" ] ; then
        #Input is readble
	readble="$1"
    else
	if [ "$1" == "-h" ] ; then             ##help
	    ShowUsage 
	    exit 
	elif [ "$1" == "-v" ] ; then
	    echo "$ScriptName Version $Version"
	    echo "Last Modified $TimeStamp"   ##help
	    exit
	elif [ "$1" == "-batch" ] ; then
	    TeXmode="batch"
	elif [ "$1" == "-nonstop" ] ; then
	    TeXmode="nonstop"
	elif [ "$1" == "-p" ] ; then
	    Format="png"
	elif [ "$1" == "-g" ] ; then
	    Format="gif"
	elif [ "$1" == "-nw" ] ; then
	    NeedWarning="NO"
	elif [ "$1" == "-w" ] ; then
	    NeedWarning="YES"
	elif [ "$1" == "-nm" ] ; then
	    NeedMessage="NO"
	elif [ "$1" == "-m" ] ; then
	    NeedMessage="YES"
	elif [ "$1" == "-ne" ] ; then
	    NeedEPS="NO"
	elif [ "$1" == "-e" ] ; then
	    NeedEPS="YES"
	elif [ "$1" == "-nq" ] ; then
	    NeedEq="NO"
	elif [ "$1" == "-q" ] ; then
	    NeedEq="YES"
	elif [ "$1" == "-i" ] ; then
	    NeedInput="YES"
	elif [ "$1" == "-ni" ] ; then
	    NeedInput="NO"
	elif [ "$1" == "-hf" ] ; then
	    if [ "$2" != "" ] ; then
		DEFHEADDER=$2
		shift
	    else
		ShowUsage
	    fi
	elif [ "$1" == "-of" ] ; then
	    if [ "$2" != "" ] ; then
		OUTPUT=$2
		shift
	    else
		ShowUsage
	    fi
	elif [ "$1" == "-c" ] ; then
	    if [ "$2" != "" ] ; then
		Color=$2
		shift
	    else
		ShowUsage
	    fi
	else
	    SOURCE=${readble:-$1} ##SOURCE is new file
	fi
    fi
    shift
done

### default variables
DEFHEADDER=${DEFHEADDER:-"headder"}
HEADDER=${TEXHEADDERFILE:-$DEFHEADDER}
SOURCE=${readble:-"eq2gif$$"}
OUTPUT=${OUTPUT:-$SOURCE}
NeedMessage=${NeedMessage:-"YES"}
NeedWarning=${NeedWarning:-"YES"}
NeedEPS=${NeedEPS:-"YES"}
NeedEq=${NeedEq:-"YES"}
NeedInput=${NeedInput:-"NO"}
TeXmode=${TeXmode:-"batch"}
Color=${Color:-"yellow"}
TMPDIR=${DEFTMPDIR:-"/tmp/eq2img$$"}
Format=${Format:-"gif"}

### variables SECTION END ###

## SHOW PID ##
#in order to kill this process when platex waiting for your input

if [ $NeedMessage == "YES" ] ; then
    echo "$ScriptName 's PID : $$"
else
    if [ $TeXmode != "batch" ]; then
	if [ $TeXmode != "nonstop" ] ; then
	    echo "$ScriptName 's PID :$$"
	fi
    fi
fi

## Make TMPDIR ##

if [ ! -d "$TMPDIR" ] ; then
    mkdir "$TMPDIR"
fi

if [ ! -d "$TMPDIR" ] ; then
    if [ $NeedWarning == "YES" ]; then
	echo "Cannot Make $TMPDIR , exit"
	echo "check DEFTMPDIR variable"
    fi
    exit 1
fi

## Define file names ##

TMPBASE="${TMPDIR}/${SOURCE}"
TMPTEX="${TMPBASE}.tex"
TMPAUX="${TMPBASE}.aux"
TMPLOG="${TMPBASE}.log"
TMPDVI="${TMPBASE}.dvi"
TMPEPS="${TMPBASE}.eps"
TMPGIF="${TMPBASE}.${Format}"

### making tex file section ###
## make headder ##

if [ -r "$HEADDER" ]; then
    cat "$HEADDER" > "$TMPTEX" #headder exist 
else
    echo '\documentclass[12pt]{article}' > "$TMPTEX" #use default headder
    echo '\usepackage{amsmath,amssymb}' >> "$TMPTEX"
    echo '\begin{document}' >> "$TMPTEX"
fi

echo '\pagestyle{empty}' >> "$TMPTEX" #pagestyle empty is needed for dvips

## merge headder with source of tex ##

if [ -r "$SOURCE" ]; then
    cat "$SOURCE" >> "$TMPTEX"
else
    if [ $NeedInput == "YES" ] ; then
	echo "type tex command , that treat as in  \[...\] env"
	read -er TEXLINEINPUT        ## directory tex source input
	echo '\[' >> "$SOURCE" 
	echo $TEXLINEINPUT >> "$SOURCE"
	echo '\]' >> "$SOURCE"
        cat "$SOURCE" >> "$TMPTEX" 
    else
	if [ $NeedWarning == "YES" ] ; then
	    echo "can not read $SOURCE and not recieve directly input"
	fi

	/bin/rm -rf "$TMPDIR"
	exit
    fi
    
fi

echo '\end{document}' >> "$TMPTEX"

## remove equation part file

if [ "$NeedEq" != "YES" ] ; then
    if [ -e "$SOURCE" ] ; then
	/bin/rm -f "$SOURCE"
    fi
fi

### making tex file section ### 

cd "$TMPDIR"

### compile and convert ###

## platex compile TEX->DVI##
if [ "$TeXmode" == "batch" ] ; then
    platex -interaction=batchmode "$TMPTEX" > /dev/null 2> /dev/null
elif [ "$TeXmode" == "nonstop" ] ; then
    platex -interaction=nonstopmode "$TMPTEX" > /dev/null 2> /dev/null
else
    platex "$TMPTEX" > /dev/null 2> /dev/null
fi

if [ ! -r "$TMPDVI" ] ; then
    if [ $NeedWarning == "YES" ] ; then
	echo "DVI file $TMPDVI is not exist"
    fi
    exit 1
fi

## by dvips ,DVI -> EPS

dvips -q -E "$TMPDVI" -o "$TMPEPS"

if [ ! -r "$TMPEPS" ] ; then
    if [ $NeedWarning == "YES" ] ; then
	echo "Enhanced PostScript file $TMPEPS is not exist"
    fi
    exit 1
fi

## EPS -> GIF , by convert (ImageMagick)

convert -density 800x800 "$TMPEPS" -transparent white -fill "$Color" -fuzz 15% -opaque black "$TMPGIF"
##convert

# first file name is input (format is automatic detected) 
# second file name is output (format is automatic detected)

# -density XxY :resolove along with x and y direction (X,Y dpi)
# -transparent color : transparent an area where is painted in that
# -color fill color area : fill color in an area where is given bellow
#  "-opaque" option 
# -fuzz parcent : error for color 
# -opaque color : return an area where patinted given color 

cd "$OLDPWD"

### END SECTION ###
## move need files 

if [ $NeedEPS == "YES" ] ; then
    cp "$TMPEPS" "${OUTPUT}.eps"
fi

cp "$TMPGIF" "${OUTPUT}.${Format}"

## remove the others
/bin/rm -rf "$TMPDIR"