Skip to content

Instantly share code, notes, and snippets.

@corbett
Last active December 10, 2015 22:28
Show Gist options
  • Save corbett/4502064 to your computer and use it in GitHub Desktop.
Save corbett/4502064 to your computer and use it in GitHub Desktop.
Takes images in the directory input via command line and resizes it to Latex floating image table of desired dimensions (rows x columns)
#!/usr/bin/env python
# encoding: utf-8
"""
create_latex.py
Created by Christine Corbett Moran on 2013-01-10.
Takes images in the directory input via command line and resizes it to Latex floating image table of desired dimensions (rows x columns)
usage: ~/Documents/Projects/astrophysics/Scripts/Plotting/create_latex.py 3 4 *png
within the directory the png files reside
"""
import sys
import os
header=r"""\begin{figure*}[htp]
\begin{center}$
\begin{array}{%s}"""
row=r"\includegraphics[width=%f\textwidth]{{%s/%s}%s}"
middlerow=r" &"
endrow=r" \\"
footer=r"""\end{array}$
\end{center}
\end{figure*}"""
def main():
rows=int(sys.argv[1])
columns=int(sys.argv[2])
#split into files and extensions to allow me to use periods or . in the file names in latex without it getting confused about extension
files=[os.path.splitext(f)[0] for f in sys.argv[3:]]
extensions=[os.path.splitext(f)[1] for f in sys.argv[3:]]
if rows*columns!=len(files):
print "error number of files must equal rows x columns"
sys.exit()
width=1./columns
print header % ('c' * columns)
for i in range(rows):
for j in range(columns):
f=files[columns*j+i]
ex=extensions[columns*j+i]
image=row % (width,os.getcwd(),f,ex)
if i!=(rows-1):
print image + middlerow
else:
print image + endrow
print footer
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment