Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I've seen some examples where shell scripts are incorporated into LaTeX documents. However, I haven't seen any example of an entire LaTeX document being incorporated into a shell script. Does anyone have an example of what this would look like? [Not just a reference to the LaTeX file, but actually include the entire document into the script -- from the opening class to the document end.]

I was thinking about having the shell script contain the command line to build the LaTeX document, followed by the entire LaTeX document (incorporated directly into the script), and then a sleep command wile the document builds, and then a latexmk -c command at the end.

share|improve this question
add comment

1 Answer

up vote 14 down vote accepted

I will assume that by shell you mean a Unix shell such as bash. You may want to look at the "here document" feature of bash. The simplest you can do is

#!/bin/bash

latex <<theend
\documentclass{article}
\begin{document}
Blah blah
\end{document}
theend
echo "Done!"

however this may lead to some problems if the latex processing needs multiple passes. It will also clutter your current directory with bunch of extra files produced by latex, such as log file, .aux file, etc., which may not be what you want.

A better option is to create a temporary directory, extract the LaTeX document into it using cat and to run LaTeX with this document as input. You may want to run LaTeX several times to make sure all cross references are resolved. At the end you just copy the generated .dvi or .pdf file back to your current directory.

An example of such script is below. It uses mktemp to create a temporary directory. As far as I know, mktemp is not available on every Unix, but it should be available on every system that has GNU coreutils.

There are probably better ways how to handle pretty much every step of the process, but this should get you started.

#!/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 > myfile.tex <<'THEEND'
\documentclass{article}
\begin{document}
Blah blah \(x^2 + 1\) or $x^2 + 1$.
\end{document}
THEEND

# If the file extracts succesfully, try to run pdflatex 3 times.
# If something fails, print a warning and exit
if [[ -f 'myfile.tex' ]]
then
   for i in {1..3}
   do
      if pdflatex myfile.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 to original directory and exit
cp myfile.pdf $curdir
exit 0
share|improve this answer
    
Yes -- that is exactly what I had in mind. If it is not too much trouble, can you please insert into your example the cat line of code to help deal with the potential problem you mentioned. –  lawlist Mar 11 '13 at 16:32
    
@lawlist: See the changes. –  Jan Hlavacek Mar 12 '13 at 4:35
1  
@JanHlavacek But your document cannot use $ for math. –  JLDiaz Mar 12 '13 at 8:17
2  
@JLDiaz You say that like it's a bad thing! –  Loop Space Mar 12 '13 at 13:47
1  
See the zeroth's edit: enclosing the THEEND string in single quotes prevents $ expansion and makes all the quoting unncessary. –  Jan Hlavacek Mar 12 '13 at 16:44
show 6 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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