Vi(m) tip #5: convert list of image files into latex figures

Alec Jacobson

November 11, 2009

weblog/

If I select and copy (CMD+C) a bunch of image files in Finder then paste (CMD+V) in TextEdit, then recopy and paste into vi(m), I can run the follow commands to make each file into a full blown latex figure. (Of course you can also just copy and paste the list using ls on any unix/linux machine.) For eps files:
:%s/^\(.*eps\)$/\r\\begin{figure}[tbp]\r\\centering\r\\epsfig{file=\1,width=0.9\\linewidth,clip=}\r\\caption{}\r\\end{figure}/
and for others (png here, just replace with jpg etc.):
:%s/^\(.*png\)$/\r\\begin{figure}[tbp]\r\\centering\r\\includegraphics[width=0.9\\textwidth]{\1}\r\\caption{}\r\\end{figure}/
Example: The first one-liner substitutes:
bilapl_all_bnds_ex.eps
bilapl_all_bnds_ey.eps
trilapl_all_bnds_ex.eps
trilapl_all_bnds_ey.eps
trilapl_all_bnds_ez.eps
with this:
\begin{figure}[tbp]
\centering
\epsfig{file=bilapl_all_bnds_ex.eps,width=0.9\linewidth,clip=}
\caption{}
\end{figure}

\begin{figure}[tbp]
\centering
\epsfig{file=bilapl_all_bnds_ey.eps,width=0.9\linewidth,clip=}
\caption{}
\end{figure}

\begin{figure}[tbp]
\centering
\epsfig{file=trilapl_all_bnds_ex.eps,width=0.9\linewidth,clip=}
\caption{}
\end{figure}

\begin{figure}[tbp]
\centering
\epsfig{file=trilapl_all_bnds_ey.eps,width=0.9\linewidth,clip=}
\caption{}
\end{figure}

\begin{figure}[tbp]
\centering
\epsfig{file=trilapl_all_bnds_ez.eps,width=0.9\linewidth,clip=}
\caption{}
\end{figure}