cap program drop texpdf
program define texpdf, rclass
syntax using/ [, Copy(string) open]
	local oldcd `"`c(pwd)'"'
	****parsing out file name  and path
	local toparse=subinstr(`"`using'"',`"\"',`"/"',.)
	if regexm(`"`toparse'"',"(.+/)(.+\.tex$)") {
		local path=regexs(1)
		local file=regexs(2)
	}
	local copy2=subinstr(`"`copy'"',`"\"',`"/"',.)

	***Confirming has version of stata.sty in personal folder, if not get from S
	cap confirm file `"`c(sysdir_personal)'stata.sty"'
	if _rc!=0 {
		if "`c(os)'" == "Windows" copy 	`"S:/data/draft_ado_files/gibson_ados/stata.sty"' `"`c(sysdir_personal)'stata.sty"'
		if "`c(os)'" == "MacOSX" copy `"/Volumes/shared/data//draft_ado_files/gibson_ados/stata.sty"' `"`c(sysdir_personal)'stata.sty"'
	}

	***Confirming has version of stata.sty in folder with .tex file, if not get from personal
	cap confirm file `"`path'stata.sty"'
	if _rc!=0 {
		copy `"`c(sysdir_personal)'stata.sty"' `"`path'stata.sty"'
	}

	cd `"`path'"'
	cd

	*---------------------Typeset LaTeX Document----------------------*
	local fff=subinstr(`"`file'"',".tex","",1)
			**first**
	if "`c(os)'" == "Windows"{
		!taskkill /f /im "cmd.exe"   /t
		!taskkill /f /im "Acrobat.exe"   /t
		!taskkill /f /im "AcroRd32.exe"   /t
		cap rm `"`path'/`fff'.pdf"'
		!pdflatex "`path'/`fff'.tex"
		**need bibtex command**
		!pdflatex "`path'/`fff'.tex"
		di as smcl `"Click to open directory: {browse `"`path'"'}"'
		di as smcl `"CLICK TO OPEN: {browse `"`path'/`fff'.pdf"'}"'
		sleep 1000
		if `"`copy2'"'!=`""'	cap copy `"`path'/`fff'.pdf"' `"`copy2'/`fff'.pdf"', replace
		if `"`open'"'==`"open"' shell start  "AcroRd32.exe" "`path'/`fff'.pdf"
	} //end windows loop

	if "`c(os)'" == "MacOSX" {
		cap rm "`path'/`fff'.pdf"
		**test:  !/usr/texbin/bibtex	   "${sf}/aisdreport_year1"
		!/Library/TeX/texbin/pdflatex --shell-escape   "`path'/`fff'.tex"  "`path'/`fff'"
		!/Library/TeX/texbin/bibtex	   "`path'/`fff'"
		!/Library/TeX/texbin/pdflatex --shell-escape   "`path'/`fff'.tex"  "`path'/`fff'"
		**!/usr/texbin/makeindex  --shell-escape   "${latex}/`fff'.idx"  "${latex}/`fff'"
		!/Library/TeX/texbin/pdflatex --shell-escape   "`path'/`fff'.tex"  "`path'/`fff'"
		*copy pdf file to subdirectory
		if `"`copy2'"'!=`""'	cap copy `"`path'/`fff'.pdf"' `"`copy2'`fff'.pdf"', replace
		replace public
		!open "`path'/`fff'.pdf"
			di as smcl `"Click to open directory: {browse `"`path'"'}"'
			di as smcl `"CLICK TO OPEN: {browse `"`path'/`fff'.pdf"'}"'

	}  //end mac loop
	cd `"`oldcd'"'

end