[Tex/LaTex] TeXworks: Compile only current document with \includeonly

includetexworks

I have a large document with multiple parts:

main.tex:

\documentclass{article}
\begin{document}
\include{part1}
\end{document}

part1.tex:

% !TEX root = main.tex
Hello World!

This way, I can compile the PDF with part1.tex open, and I can compile just one part (\includeonly{part1}).

Is there a way to compile only the current document, i.e., insert \includeonly{currently opened file} before compiling?

My first idea was to use LuaTeX to insert an includeonly depending on command line arguments (using the Lua variable arg), but then I still didn't find a way to append the command line parameter.

Best Answer

It isn't exactly what I was asking for, but I does the job quite well for me. It uses Lua to determine the file that was changed last (so usually the file I am currently editing) and writes \includeonly{file} to the preamble.

includeonlylastmodified.lua

require "lfs"

function mtimesort(a,b)
   return lfs.attributes(a..'.tex',"modification") 

main.tex:

\documentclass{article}

\directlua{
   files = {'part1','part2'}
   dofile('includeonlylastmodified.lua')
}

\begin{document}
\include{part1}
\include{part2}
\end{document}
Related Question