なんだこれは

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

需要あるらしいので貼ってみた。

TeX数式部分から背景透明のGIF続き

意外に需要があったのではりはりする。あ、必要なのはBash/Perl/ImageMagick/platex/dvipsってところね。VineLinuxなら確実にそろう。テスト環境はMacOSX WorkShopとVineLinux 3.2。

まず、CUIのフロントエンドmkimgから。これはBash。これにTeXファイルを食わせるとcutterというPerlTeXの数式環境部分eqpartとプレアンブルheadderに分解するんだ。あと、eqpartいっこいっことheadderからミニTeXファイルを作らせて、それをconvertしてpngかgifにするんだ。それをeq2imgというんだよ。もちろんオプションで完全に制御できるんだけど、設定ファイルをホームに置けば自動で読むんだよ。はっはっは

mkimg

mkimg texfile [texfile2...]

mkimg section*.tex

も可。-iで実はTeXの数式部分を直書きするという荒技が可能。

mkimg -i

で出るプロンプトに対して、

\sin(x)

とか入力してみろ。

#!/bin/bash
# mkimg -- CUI frontend of cutter and eq2img -- 

TimeStamp="Time-stamp: <05/09/18 16:13:57 fu7mu4>"
Version="2.0"
ScriptName=$(basename "$0")

ShowUsage () {
	    cat <<EOF
$ScriptName Script for making equation gif/png from tex source
$ScriptName :USAGE
$ScriptName [-h|-v|-ne|-nq|-c color|-f filename|texfile1 [texfile2 ..] ]

Show and Quit
-h :show help and quit
-v :show version and quit

Input
-hf headderfile :use anothre file as headderfile
-i :allow to directory input tex-command. use this if file was empty
-ni:not allow to input; igonre file if that file was empty 

Output Files
-p : as png format
-g : as gif format
-c color: change text color, color name, white, hex '#ffffff' ...

Output File Name
-of basename: change base name of output files
-0 : add 0 before number of output files
-x : don't add 0 before number of output files (default)


Other Files
-e :with eps
-ne:no eps
-q :with eq files 
-nq:no equation files

Message and Warning
-nm:no message
-m :message
-nw:no warning
-w :with warning

If you want more infomation , see readme.tex
EOF
}

#default value
Need0="NO"

while [ "$1" != "" ]
do
    if [ -r "$1" ] ; then
        #Input is readble
	readbles=( ${readbles[@]} "$1" )
    else
	if [ "$1" == "-h" ] ; then             ##help
	    ShowUsage 
	    exit 
	elif [ "$1" == "-v" ] ; then
	    echo "mkgif Version $Version"
	    echo "Last Modified $TimeStamp"   ##help
	    exit
	elif [ "$1" == "-0" ] ; then
	    Need0="YES"
	elif [ "$1" == "-p" ] ; then
	    Format="png"
	elif [ "$1" == "-g" ] ; then
	    Format="gif"
	elif [ "$1" == "-x" ] ; then
	    Need0="NO"
	elif [ "$1" == "-e" ] ; then
	    NeedEPS="YES"
	elif [ "$1" == "-ne" ] ; then
	    NeedEPS="NO"
	elif [ "$1" == "-q" ] ; then
	    NeedEq="YES"
	elif [ "$1" == "-nq" ] ; then
	    NeedEq="NO"
	elif [ "$1" == "-m" ] ; then
	    NeedMessage="YES"
	elif [ "$1" == "-nm" ] ; then
	    NeedMessage="NO"
	elif [ "$1" == "-w" ] ; then
	    NeedWarning="YES"
	elif [ "$1" == "-nw" ] ; then
	    NeedWarning="NO"
	elif [ "$1" == "-hf" ] ; then
	    if [ "$2" != "" ] ; then
		if [ -r "$2" ] ; then
		    DEFHEADDER="$2"
		fi
		shift
	    else
		ShowUsage
	    fi
	elif [ "$1" == "-of" ] ; then
	    if [ "$2" != "" ] ; then
		OutPut="$2"
		shift
	    else
		ShowUsage
	    fi
	elif [ "$1" == "-c" ] ; then
	    if [ "$2" != "" ] ; then
		ForeColor="$2"
		shift
	    else
		ShowUsage
	    fi
	else
	    ShowUsage
	fi
    fi
    shift
done
#
OutPut=${OutPut:-"image"}
# generate eq2gifp options

if [ "$NeedEPS" == "YES" ] ; then
    Eq2Options=( ${Eq2Options[@]} "-e" )
else
    Eq2Options=( ${Eq2Options[@]} "-ne" )
fi

if [ "$NeedEq" == "YES" ] ; then
    Eq2Options=( ${Eq2Options[@]} "-q" )
else
    Eq2Options=( ${Eq2Options[@]} "-nq" )
fi

if [ "$NeedWarning" == "YES" ] ; then
    Eq2Options=( ${Eq2Options[@]} "-w" )
else
    Eq2Options=( ${Eq2Options[@]} "-nw" )
fi

if [ "$NeedMessage" == "YES" ] ; then
    Eq2Options=( ${Eq2Options[@]} "-m" )
else
    Eq2Options=( ${Eq2Options[@]} "-nm" )
fi

if [ "$ForeColor" != "" ] ; then
    Eq2Options=( ${Eq2Options[@]} "-c" "$ForeColor" )
fi

if [ "$Format" == "png" ] ; then
    Eq2Options=( ${Eq2Options[@]} "-p" )
else
    Eq2Options=( ${Eq2Options[@]} "-g" )
fi

if [ "$DEFHEADDER" != "" ] ; then
    Eq2Options=( ${Eq2Options[@]} "-hf" "$DEFHEADDER" )
fi

# execute perl script to cutting TeX source to get equation parts file
# eqpart1, eqpart2, ...

#echo "Options: ${Eq2Options[@]}"

cutter "${readbles[@]}"


# Bad source code sometime stop platex compiling to wait your input, 

echo "$ScriptName PID: $$" 

i=1
while [ "$i" -le 999 ]
do
  if [ $i -lt 10 ] ; then
      num=00${i}
  elif [ $i -lt 100 ] ; then
      num=0${i}
  else
      num=${i}
  fi

  inputfile=eqpart${i}
  if [ "$Need0" == "YES" ]; then
      outputfile=${OutPut:-"eqpart"}${num}
  else
      outputfile=${OutPut}${i}
  fi

  if [ -r "$inputfile" ] ; then
      #echo "${Eq2Options[@]}"
      eq2img "$inputfile" "${Eq2Options[@]}" "-of" "$outputfile"
      i=$(expr $i + 1)
  else
      i=1000 #escape while loop
  fi
done