# TeXworks: Compile only current document with \includeonly

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.

-
That's not possible. How would LaTeX know which document is "currently open" in your editor? – Jubobs Mar 30 '13 at 14:55
@Jubobs LaTeX doesn't know, but TeXworks knows. – tstenner Mar 30 '13 at 14:58
@tstenner exactly so this probably needs to be an editor script rather than something (lua)tex does. I don't know texworks but certainly an editor such as emacs can easily edit the master file to add the name of the current buffer. – David Carlisle Mar 30 '13 at 18:06
@tstenner Assuming countable part1.tex-part10.tex. you can add \includeonly{currentpart} manually to main.tex with main.tex and currentpart.tex windows open simultaneously in TeXworks. Post a TeXworks feature request. – texenthusiast Mar 30 '13 at 21:50

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}

-