2012/01/02

D論お役立ち?スクリプト

○Labelのチェックをおこなう

Thesis全体で使用されている\labelコマンドを列挙し、chapter間の重複をしらべる。
\label{labtype:labname}という形式を仮定。labels.txtにchapter, labtype, labname, 重複の有無を列記。重複がなければ最後のcolumnは'ok'、あれば該当するchapter名がcsvで書かれる。重複がある場合も、初出時は'ok'になる仕様。

#!/usr/bin/env python

skipnames = [] #List tex filenames which should be excluded label search.
outfile = 'labels.txt'
maintex = 'ms.tex' #The main tex filename in which your articles are included.

def add_elem(elem,key,dic):

    if dic.has_key(key):

        dic[key].append(elem)

    else:
        
        dic[key] = [elem]

    return(0)

texnames = []
labels = {}

for line in open(maintex,'r'):

    if line.startswith('\include{') or line.startswith('%\include{'):
        texname = line.replace('\include{','').replace('}\n','').replace('%','')
        if not texname in skipnames:
            texnames.append(texname)

f = open(outfile,'w')

for texname in texnames:

    texfile = texname+'.tex'

    for line in open(texfile,'r'):

        if line.find('\label{') != -1:

            templine = line[line.find('\label{')+len('\label{'):]
            label = templine[:templine.find('}')]
            labtype, labname = label.split(':')
            add_elem(label,texname,labels)
            dupflag = 'ok'
            for texkey in labels.keys():
                if texkey == texname:
                    continue
                lablist = labels[texkey]
                if label in lablist:
                    if dupflag == 'ok':
                        dupflag = texkey
                    else:
                        dupflag += ','+texkey
                else:
                    pass
            f.write('%10s %10s %15s %20s\n' % (texname, labtype, labname, dupflag))
f.close()

○Referenceをsortする

reference.texに記述したreference listをアルファベット順にsortする。
コメントアウトしたものは消されます。Archiveは残すので間違えて消したりしても安心のはず。

#!/usr/bin/env python

import os,datetime

archive = 'ref_archives'
if not os.path.exists(archive):
    os.mkdir(archive)
else:
    pass

reffile = 'reference.tex'
date  = datetime.date.today().isoformat().replace('-','')
biblist = []

i = 0
row = 0
for line in open(reffile,'r'):

    if line.startswith('%') or line.startswith('\\begin') or line.startswith('\\end') or line == '\n':
        pass
    elif line.startswith('\\bibitem'):
        try:
            biblist.append(line.replace('\n',''))
        except:
            print(line)
            exit()
        i += 1
    else:
        try:
            biblist[i-1] = biblist[i-1]+line.replace('\n','')
        except:
            print(line)
            exit()
        pass
    row += 1

os.system('mv '+reffile+' '+archive+'/'+reffile.replace('.tex','_'+date+'.tex'))

f = open(reffile,'w')

bibdoc = {}

for bib in biblist:

    title = bib.replace(']','[').split('[')[1]
    citekey = bib.replace(']','[').split('[')[-1].replace('}','{').split('{')[1]
    content = bib.replace('\\bibitem[','').replace(title,'').replace(']{','').replace(citekey+'} ','')
    author = title.split('(')[0]
    year = title.replace(')','(').split('(')[1]

    bibdoc[title] = bib

keys = bibdoc.keys()

keys.sort()

f.write('\\begin{thebibliography}{}\n')
for key in keys:
    f.write(bibdoc[key]+'\n')
f.write('\end{thebibliography}\n')
f.close()

print('Done.')

○Abbreviationの各章での引用回数をcheckする

description環境で書かれたabbreviation listに各章での略語および元の用語の引用回数をコメントで追記します。
元の用語に関しては複数行にまたがるものは数え落とします。先頭の語のcapitalizeの有無はどっちでも拾います。

#!/usr/bin/env python

import os,datetime

skipnames = [
    'abbreviations',
    'reference',
    'acknowledgement',
    ] #List tex filenames which should be excluded label search.

maintex = 'ms.tex' #The main tex filename in which your articles are included.

texnames = []

for line in open(maintex,'r'):

    if line.startswith('\include{') or line.startswith('%\include{'):
        texname = line.replace('\include{','').replace('}\n','').replace('%','')
        if not texname in skipnames:
            texnames.append(texname)

archive = 'abb_archives'
if not os.path.exists(archive):
    os.mkdir(archive)
else:
    pass

def find_keyinchaps(key):

    chaplab = ''
    ch = 1
    for texname in texnames:
        if texname in skipnames:
            continue
        count = 0
        texfile = texname+'.tex'
        for line in open(texfile,'r'):
            if line.find(key+' ') != -1 or line.find(key+'s ') != -1 or line.find(key+')') != -1:
                count += 1
        if count != 0:
            chaplab += str(ch)+' x'+str(count)+', '
        ch += 1
    return(chaplab[:-2])

def find_continchaps(content):

    content = content.replace('-',' ').lower()

    chaplab = ''
    ch = 1
    for texname in texnames:
        if texname in skipnames:
            continue
        count = 0
        texfile = texname+'.tex'
        for line in open(texfile,'r'):
            line = line.replace('-',' ').lower()
            if line.find(content) != -1:
                count += 1
        if count != 0:
            chaplab += str(ch)+' x'+str(count)+', '
        ch += 1
    return(chaplab[:-2])

abbfile = 'abbreviations.tex'
date  = datetime.date.today().isoformat().replace('-','')
abblist = []

i = 0
row = 0
for line in open(abbfile,'r'):

    if line.startswith('\\chapter') or line.startswith('\\begin') or line.startswith('\\end') or line == '\n':
        pass
    elif line.startswith('\\item'):
        try:
            abblist.append(line.replace('\n',''))
        except:
            print(line)
            exit()
        i += 1
    else:
        try:
            abblist[i-1] = abblist[i-1]+line.replace('\n','')
        except:
            print(line)
            exit()
        pass
    row += 1

os.system('mv '+abbfile+' '+archive+'/'+abbfile.replace('.tex','_'+date+'.tex'))

f = open(abbfile,'w')

abbdoc = {}

for abb in abblist:

    abbkey = abb[abb.find('[')+1:abb.find(']')]
    maxnum = abb.find('%')
    if maxnum == -1:
        maxnum = len(abb)
    content = abb[abb.find('$\\ldots$')+len('$\\ldots$'):maxnum].strip(' ')

    abbdoc[abbkey] = content

keys = abbdoc.keys()

keys.sort()

f.write('\\chapter{Abbreviation}\n')
f.write('\\begin{description}\n')
for key in keys:
    usedchaps = find_keyinchaps(key)
    usedchaps2 = find_continchaps(abbdoc[key])

    f.write('\\item [%s] $\\ldots$ %s %s\n' % (key, abbdoc[key], '% '+usedchaps+' ; '+usedchaps2))

f.write('\\end{description}\n')
f.close()

print('Done.')