この広告は、90日以上更新していないブログに表示しています。
『数学ガール/フェルマーの最終定理』の校正の都合上、LaTeXのソースからすべての \command を抽出するスクリプトを書きました。
# find_tex_commands.rbrequire'pathname'commands =Array.newPathname.glob("*.tex").eachdo |file| open(file,"r")do |f| f.each_linedo |line| line.scan(/\\[a-zA-Z]+/).eachdo |c|nextif commands.include?(c) commands << cendendendendprint commands.sort.join("\n")
実行結果はおおよそ以下のようになります。
> ruby find_tex_commands.rb\LaTeXe\Longleftrightarrow\Longrightarrow(略)\subsection\sum\tableofcontents\text\theta\times\title\to\today\ton\tonsub\underbrace\underline(略)
今回のスクリプト作成時、どうしても一行中に複数個のマッチが存在する場合の正規表現の使い方がわからず、lurker_さんにString#scanを教えていただきました。感謝。
追記:
emeitchさんから、Setのほうがすなおかもと教えていただきました。そうですね、uniquenessの保証を自前でやらなくてもよいから…。
# find_tex_commands.rbrequire'pathname'require'set'commands =Set.newPathname.glob("*.tex").eachdo |file| open(file,"r")do |f| f.each_linedo |line| line.scan(/\\[a-zA-Z]+/).eachdo |c|# next if commands.include?(c) commands << cendendendendprint commands.sort.join("\n")
引用をストックしました
引用するにはまずログインしてください
引用をストックできませんでした。再度お試しください
限定公開記事のため引用できません。