0

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?

closed as off-topic by egreg, Kurt, Arun Debray, Sean Allred, user13907 Jan 23 '16 at 23:53

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not fall within the scope of TeX, LaTeX or related typesetting systems as defined in the help center." – egreg, Kurt, Arun Debray, Sean Allred, Community
If this question can be reworded to fit the rules in the help center, please edit the question.

  • You use $1.tex in most places, but you forgot to change the line if [[ -f 'myfile.tex' ]], which checks for myfile.tex (which doesn't exist) and therefore returns an error. – Arun Debray Jan 23 '16 at 20:33
  • 2
    This is a shell scripting problem; maybe unix.stackexchange.com is better. – egreg Jan 23 '16 at 20:33
  • I thought about that. But I found the solution here, so I figured that meant that it was TeX centric enough to be okay to ask. Thanks @Arun Debray, changed that in the example, it still throws the same error. – A Feldman Jan 23 '16 at 20:42
  • @egreg, I went ahead and posted there as well. Thanks for the suggestion. – A Feldman Jan 23 '16 at 20:49
  • If we don't already, one day we'll have a migration path between the two… one day. unix.stackexchange.com/q/257245/28980 – Sean Allred Jan 23 '16 at 22:59
2

Replacing

if [[ -f '$1.tex' ]]

with

if [[ -f "$1.tex" ]]

fixed the error.

The double quotes were needed to expand the variable.

Not the answer you're looking for? Browse other questions tagged or ask your own question.