/************************************************************************************************************* Program to insert a figure into a tex document. Required: Name of figure file (must specify path if in a different location from output file and extension, either .pdf or .eps). Name of output file. Options: headtitle foottitle frametitle subtitle/subcaption note label position (default is top of page) beamer replace Written by Gabriel Chodorow-Reich and provided without any warranty This version: April 2016 *************************************************************************************************************/ capture program drop TexFigure program define TexFigure version 12.0 syntax anything(name=infilename) using/, [title(string) headtitle(string) foottitle(string) frametitle(string) subtitle(string asis) subcaption(string asis) COLumns(integer 0) note(string) label(string) position(string) headspace(string) notespace(string) notesize(string) stringquotes beamer centerline subfigure replace] #delimit; if `"`headspace'"'==`""' local headspace 0cm; if `"`notespace'"'==`""' local notespace 0cm; if `"`notesize'"'==`""' local notesize \footnotesize; /************************************************************************************************************* Preliminary options *************************************************************************************************************/ if `"`stringquotes'"'!=`""' local stringquote `"\string""'; if `columns'==0 local columns: word count `infilename'; if `"`title'"'!=`""' local headtitle `"`title'"'; /************************************************************************************************************* Parse figures *************************************************************************************************************/ if `:word count `infilename''==1 {; /*Single graph in figure*/ local infilename `infilename'; /*Get rid of any quotes around infilename*/ if `"`subtitle'"'!=`""' local figure `"`figure' `"`subtitle'"' _n"'; if `"`centerline'"'==`""' local figure `"\includegraphics{`stringquote'`infilename'`stringquote'}"'; else local figure `"\centerline{\includegraphics{`stringquote'`infilename'`stringquote'}}"'; local figure `"`"`figure'"' _n"'; }; else {; /*Multiple graphs in figure*/ if `"`subfigure'"'==`""' local subfigure minipage; forvalues i = 1/`:word count `infilename'' {; /*Loop over graphs*/ local c = `c'+1; local figure `"`figure' `"\begin{`subfigure'}{`=1/`columns''\textwidth}"' _n"'; /*Each graph in separate minipage*/ local figure `"`figure' `"\centering"' _n"'; if `"`subtitle'"'!=`""' local figure `"`figure' `"`:word `i' of `subtitle''"' _n "'; /*Subtitle of graph if specified*/ if `"`subcaption'"'!=`""' local figure `"`figure' `"\caption{`:word `i' of `subcaption''}"' _n "'; /*Subtitle of graph if specified*/ local figure `"`figure' `"\includegraphics{`stringquote'`: word `i' of `infilename''`stringquote'}"' _n"'; local figure `"`figure' `"\end{`subfigure'}%"' _n"'; if `c'==`columns' & `c'!=`:word count `infilename'' {; /*If macro columns specified, start next graphs on new line*/ local figure `"`figure' `"\newline"' _n"'; local c = 0; }; }; }; /************************************************************************************************************* Additional options *************************************************************************************************************/ if `"`position'"'==`""' local position `"!t"'; if `"`headtitle'"'!=`""' local prefigure `"`"\caption{`headtitle'}"' _n"'; local prefigure `"`prefigure' `"\vspace{`headspace'}"' _n"'; if `"`foottitle'"'!=`""' local postfigure `"`"\caption{`foottitle'}"' _n"'; if `"`label'"'!=`""' & `"`beamer'"'==`""' local postfigure `" `postfigure' `"\label{`label'}"' _n"'; else if `"`label'"'!=`""' local beamerlabel [label=`label']; if `"`note'"'!=`""' {; local postfigure `"`postfigure' `"\newline \begin{minipage}{\hsize} \rule{0pt}{9pt}"' _n `"`notesize'"' _n `"\vspace{`notespace'}"' _n `"\noindent"' _n"'; local postfigure `"`postfigure' `"`note'"' _n"'; local postfigure `"`postfigure' `"\end{minipage}"' _n"'; }; if `"`replace'"'==`"replace"' file open outfile using `"`using'"', write replace; else file open outfile using `"`using'"', write; if `"`frametitle'"'!=`""' local beamerframetitle `"\frametitle{`frametitle'}"'; if `"`beamer'"'!=`""' {; local beamerhead `" `"\begin{frame}`beamerlabel'"' _n `"`beamerframetitle'"' _n"'; local beamerfoot `"`"\end{frame}"'"'; }; /************************************************************************************************************* Write file *************************************************************************************************************/ file write outfile `beamerhead' `"\begin{figure}[`position']"' _n `"\centering"' _n `prefigure' `figure' `postfigure' `"\end{figure}"' _n `beamerfoot' ; file close outfile; end;