2

I would like to compile piece of LaTeX code without \begin{document} and \end{document} (the preamble is, of course, fixed). The reason is that sometimes I only write small pieces of text and don't want to include the basic lines every time (not even with \input, etc.). For now the only way I know is by running shell code like the following one.

DIR="$( cd "$( dirname "$0" )" && pwd )"
cd $DIR

xelatex -interaction=batchmode \\documentclass{einfart}\\usepackage{ProjLib}\\begin{document}\\input{<filename>}\\end{document}

Is there a better way to do so? For example, I don't know if there is a compatible way with latexmk. Also, is it possible to achieve a similar effect with Visual Studio Code's extension "LaTeX Workshop"?

(I do realize that omitting the \begin{document} etc. is probably not a good idea. I'm just exploring the possibilities for the workflow.)

2
  • you can't omit the begin documnt from the latex run but your edior should be able to do that for you, auctex in emacs has been able to run latex on any selected region since the 1980's. In the background it constructs a temporary file with the document preamble and the selected region wrapprd in begin document end document. Mar 22, 2022 at 0:03
  • 1
    I don't really see what's wrong with using a simple shell script for this.
    – frabjous
    Mar 22, 2022 at 1:03

1 Answer 1

1

Just for reference, below is the shell script I'm currently using for compile files with extension .piece.tex, where .piece is a mark to tell that this is not a complete LaTeX code file.

DIR="$( cd "$( dirname "$0" )" && pwd )"
cd $DIR

mkdir -p .aux

filename=$(ls -t *.piece.tex | head -n1)

echo "\\documentclass[use boldface, theorem in new line, simple name, theorem numbering = *]{einfart}" > .aux/${filename%.piece.tex}.temp.tex
echo "\\usepackage{ProjLib}" >> .aux/${filename%.piece.tex}.temp.tex
echo "\\usepackage{tikz-cd}" >> .aux/${filename%.piece.tex}.temp.tex
echo "\\\\begin{document}"   >> .aux/${filename%.piece.tex}.temp.tex
echo "\\input{$filename}"    >> .aux/${filename%.piece.tex}.temp.tex
echo "\\end{document}"       >> .aux/${filename%.piece.tex}.temp.tex

latexmk -xelatex -silent -output-directory=.aux -jobname=${filename%.piece.tex} .aux/${filename%.piece.tex}.temp.tex

mv .aux/${filename%.piece.tex}.pdf ${filename%.piece.tex}.pdf

For example, if you have a Document.piece.tex with the content:

This is a test document.

\begin{theorem}\label{thm1}
    \blindtext
\end{theorem}

\begin{theorem}\label{thm2}
    \blindtext
\end{theorem}

\begin{definition}\label{def1}
    \blindtext
\end{definition}

\cref{thm1,def1,thm2}

\dnf

Then after running the script, you shall get a Document.pdf looks like:

enter image description here


Some explanations:

The first two lines change the directory to the current one containing the shell script:

DIR="$( cd "$( dirname "$0" )" && pwd )"
cd $DIR

Then make a .aux folder for storing auxiliary files.

mkdir -p .aux

The script shall now find the latest .piece.tex file for compilation.

filename=$(ls -t *.piece.tex | head -n1)

It will produce a same named .temp.tex file in the .aux folder for latexmk to work on.

echo "\\documentclass[use boldface, theorem in new line, simple name, theorem numbering = *]{einfart}" > .aux/${filename%.piece.tex}.temp.tex
echo "\\usepackage{ProjLib}" >> .aux/${filename%.piece.tex}.temp.tex
echo "\\usepackage{tikz-cd}" >> .aux/${filename%.piece.tex}.temp.tex
echo "\\\\begin{document}"   >> .aux/${filename%.piece.tex}.temp.tex
echo "\\input{$filename}"    >> .aux/${filename%.piece.tex}.temp.tex
echo "\\end{document}"       >> .aux/${filename%.piece.tex}.temp.tex

And finally, compile with latexmk and move the .pdf file to the current folder.

latexmk -xelatex -silent -output-directory=.aux -jobname=${filename%.piece.tex} .aux/${filename%.piece.tex}.temp.tex
mv .aux/${filename%.piece.tex}.pdf ${filename%.piece.tex}.pdf

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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