日暮途遠(Linux)

Just another WordPress.com site

LaTeXの特殊文字を変換するスクリプト

leave a comment »

#!/bin/bash
fn=$1
sed -n '/\\begin{document}/{=};' $fn > $$.start
sed -n '/\\end{document}/{=};' $fn > $$.end

declare -i s=`cat $$.start`
declare -i e=`cat $$.end`
start=`expr $s + 1`
end=`expr $e - 1`
rm $$.start $$.end

#echo "${s}-${e}"

cat $1 | sed -e "${start},${end}{
s/#/\\\\#/g;
s/_/\\\\_/g;
s/>/\\\\verb|>|/g;
s/</\\\\verb|<|/g;
s/&/\\\\&/g;
s/%/\\\\%/g;
}" -e "${start},${end}{
/^\\\\/!s/\^/\\\\verb|^|/g;
/^\\\\/!s/~/\\\\verb|~|/g;
/^\\\\/!s/|/\\\\verb+|+/g;
/^\\\\/!s/$/\\\\\\\\/;
}"

#/^\\\\/!s/}/\\\\}/g;
#/^\\\\/!s/{/\\\\{/g;

Emacsのorg-modeで生成したTexファイルを対象にしました。プリアンブル以外の思わぬところでTexのコマンドが使われたりしているので、本文中でも{}の変換はしないようにしています。他にも問題が起こる可能性があるので、なるべく特殊記号は使わないようにしています。
sedを使いましたが、ちょっと無理がある感じです。思いつきの自己流でやっています。

Written by 廣野秀樹

2012年6月5日 @ 3:29 午後

カテゴリー: 未分類

Tagged with

コメントを残す