0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

TeXをコンパイルするShellScriptの作成

Last updated at Posted at 2018-05-01

作成動機

TeXで文書を書く必要があった。が、コンパイル毎にコマンドを打つのが面倒だったから

構造

edf78e47.png

概要説明

  1. ファイル情報を取得
  2. 以降texファイルの場合のみ実行
  3. ディレクトリに移動
  4. pdfファイルの作成
  5. 作業を続行するか尋ねる
  6. 4に戻る(無限ループ),または終了する

コード

# !/bin/bash#

echo "texファイルをドラッグ&ドロップ"
read -p "filepath: " filepath

base=`basename ${filepath}`
dir=`dirname ${filepath}`
filename=${base%.*}
expanded=${base##*.}

if [ ${expanded} = "tex" ];then
    ad=`pwd`
    cd; cd ${dir}
    
    while :
    do
		echo "コンパイルを開始します";echo
		platex ${filename}.tex;echo;echo

		dvips ${filename}.dvi;echo;echo
		ps2pdf ${filename}.ps;
		echo "コンパイルが完了しました"
		open ${filename}.pdf

	
		read -p "終了する場合はqを入力してください" res
		if [ $res = q ];then
	  	  break
		fi
    done
    
    cd; cd ${ad}
    
else
    echo "error: texファイルではありません"
fi

説明

ファイル情報を取得する部分

  • fileのpathを取得
read -p "filepath: " filepath
  • texファイル部分とpath部分の取得
# ファイル名(hoge.tex)
base=`basename ${filepath}`
# path(/user/name/huga/)
dir=`dirname ${filepath}`
  • ファイル名と拡張子の取得
# ファイル名
filename=${base%.*}
# 拡張子
expanded=${base##*.}

ディレクトリの移動部分

# 現在のディレクトリパスを保存
ad=`pwd`
#texファイルのディレクトリに移動
cd;cd ${dir}
・
・
・
# もとのディレクトリに戻る
cd;cd ${ad}

無限ループ部分

while:
do
	・
	・
	・
	# ユーザーから入力を受け付け
	read -p "終了する場合はqを入力してください" res
	# 入力内容が"q"ならループを抜ける
	if [ $res = q ];then
		break
	fi
done

コンパイル部分

	# texファイルからdviファイルの作成
	platex ${filename}.tex

	# dviファイルからpostScriptファイルの作成
	dvips ${filename}.dvi
	#psファイルからpdfファイルの作成
	ps2pdf ${filename}.ps
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?