I ran across this post
Possible to have a shell script containing the whole LaTeX document?
The solution in that post works perfectly as written. However I wanted to be able to choose the name of the files it produces at run time by using a shell variable $1 so I could choose the filename at the command line upon invocation of the script.
So I tried to replace the filename myfile.tex with $1.tex throughout the shell script:
#!/bin/bash
# Create a temporary directory
curdir=$( pwd )
tmpdir=$( mktemp -dt "latex.XXXXXXXX" )
# Set up a trap to clean up and return back when the script ends
# for some reason
clean_up () {
cd "$curdir"
[ -d "$tmpdir" ] && rm -rf "$tmpdir"
exit
}
trap 'clean_up' EXIT SIGHUP SIGINT SIGQUIT SIGTERM
# Switch to the temp. directory and extract the .tex file
cd $tmpdir
# Quoting the 'THEEND' string prevents $-expansion.
cat > $1.tex <<'THEEND'
\documentclass{article}
\begin{document}
Hello World
\end{document}
THEEND
# If the file extracts succesfully, try to run pdflatex 3 times.
# If something fails, print a warning and exit
if [[ -f '$1.tex' ]]
then
for i in {1..3}
do
if pdflatex $1.tex
then
echo "Pdflatex run $i finished."
else
echo "Pdflatex run $i failed."
exit 2
fi
done
else
echo "Error extracting .tex file"
exit 1
fi
# Copy the resulting .pdf file and .tex file to original directory, display .pdf file and exit
cp $1.pdf $curdir;
cp $1.tex $curdir;
evince $1.pdf
exit 0
This did not work and gave me the following error:
Error extracting .tex file
Where did I go wrong here?
$1.texin most places, but you forgot to change the lineif [[ -f 'myfile.tex' ]], which checks formyfile.tex(which doesn't exist) and therefore returns an error. – Arun Debray Jan 23 '16 at 20:33