Skip to content

Instantly share code, notes, and snippets.

@SebastianoF
Created October 14, 2018 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SebastianoF/07c952ade552014aae3e1531403b79ba to your computer and use it in GitHub Desktop.
Save SebastianoF/07c952ade552014aae3e1531403b79ba to your computer and use it in GitHub Desktop.
Count words in latex
import os
import subprocess
'''
Counts the words in the manually selected latex files in the list list_file
Commands to obtain the number of words in a .tex file:
'detex my_file.tex | wc -w'
where my_file.tex is one of the string in the list list_files
'''
root_project = os.getcwd()
list_files = ["Ch_introduction.tex",
"Ch_experiment_design_and_dataset.tex",
"Ch_diffeomorphic_registration.tex",
"Ch_neonatal_rabbit_brain_multi_atlas.tex",
"Ch_pipelines_image_analysis_and_preclinical_findings.tex",
"Ch_conclusions.tex"]
# list_files = ["chapters/Ch_introduction.tex",
# "chapters/Ch_pre_clinical_study.tex",
# "chapters/Ch_state_of_the_art.tex",
# "chapters/Ch_contributions_and_results.tex",
# "chapters/Ch_work_plan.tex"]
pagg = [0]*len(list_files)
for i in range(len(pagg)):
proc = subprocess.Popen(["detex -n " + list_files[i] + " | wc -w"], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
pagg[i] = int(out)
print "Number of words in file: " + list_files[i] + " is: " + str(pagg[i])
print "\n Summary:"
print pagg
print "\n Total: " + str(sum(pagg)) + " \n"
print "Message: \n"
msg = 'This document contains ' + str(sum(pagg)) + ' words'
for i in range(len(pagg)):
msg += '\n' + '${0} \mapsto {1}$,'.format(i+1, pagg[i])
print msg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment