I want to include parts of a child document, specified by a range of line numbers, as LaTeX input, while producing useful SyncTeX information. It should also be possible to re-include lines already in the main document.


Given input files

% -- main.tex
 ..:  ...
 20:  Before input.\par
 21:  \inputlines{2}{4}{child.tex}
 22:  After input.\par
 23:  Repeat: \inputlines{22}{22}{main.tex}
 ..:  ...

% -- child.tex
  1: 1 First line.\par
  2: 2 Second line.\par
  3: 3 Third line.\par
  4: 4 Fourth line.\par
  5: 5 Fifth line.\par

I want the output to contain

Before input.
2 Second line.
3 Third line.
4 Fourth line.
After input.
Repeat: After input.

and invoking inverse search on "2 second line." should actually direct the editor to the correct line in child.tex.

Existing partial solutions

Related question. In "\input only part of a file", a solution is provided, that does what I want except for SyncTeX support. As far as I can tell, that is an unfortunate side effect of processing a file with the \read mechanism.

Crucially, this solution is robust in situations containing multi-line constructs, such as multi-line \defs or the equation environment.

Package "listings". The command \lstinputlisting of the listings package provides options firstline and lastline, that perform the desired filtering, while providing SyncTeX support.

It is however specialized for including files in verbatim and I was unable to verify

  • How their filtering-by-line works, and
  • if it can be transferred to LaTeX input without breaking either multi-line constructs or SyncTeX.

Package "catchbetweentags". The catchfilebetweentags package provides a mechanism for including file contents marked explicitly. However, for my purposes requiring explicit %<*tag> ... %</tag> comments is undesirable, and SyncTeX information is lost anyway.


I am researching options for this, in order to rewrite the apxproof package with SyncTeX support. Currently this package creates an auxiliary file, that is later included, thereby creating junk SyncTeX information, that points to the auxiliary file.

Reading files partially, if possible without losing SyncTeX support, would allow building the functionality on a different basis.

MWE with apxproof, demonstrating the problem

This proof has its contents stored in a child document.
SyncTeX will correctly point to this file
\item It doesn't fix the loss of precision in \verb|thmrep|.
\item With many proofs, the large number of child documents
      can become rather inconvenient.

\let\appendixprelim\relax % Prevent newpage before typesetting proofs.
\preto\appendix{\noindent\hrulefill Appendix starts here}



This is a theorem, that will be repeated in the appendix.

SyncTeX sort of works here, for 

It does however point only to the end of the environment,
not to individual paragraphs.

This is the proof, which will be put into the appendix via \verb|\VerbatimOut| mechanism.

\textsf{apxproof} defers it to the appendix,
by writing it out to \verb|\jobname.axp|
and then \verb|\input|ing it in the appendix.

Sadly, this mechanism causes SyncTeX to consider
the auxiliary file \verb|\jobname.axp| to be
the source of the contents.



Compiled Screenshot

  • For my purpose, not really (though I'll keep in in mind for other usecases). (a) For reimplementing/extending apxproof, the solution would have to be able to re-read from the main document too, which creates a bootstrapping problem. (b) Using such constructs, or environments to the same effect, would sabotage the overall usability, leaving the loss of SyncTeX information or workarounds like \begin{proof}\input{proof.123.inc}\end{proof} as lesser evil.
    – kdb
    Jun 27, 2018 at 7:56
  • @JohnKormylo As far as I can tell, storing tokens into macros discards the SyncTeX information.
    – kdb
    Jun 27, 2018 at 14:40
  • @JohnKormylo SyncTeX is the only reason for me to be looking into this actually. The apxproof package is quite useful, but its usability is hindered strongly by the loss of synctex support, so I was hoping to achieve the same behavior in some manner that preserves SyncTeX information.
    – kdb
    Jun 28, 2018 at 11:22
  • A complete MWE would be useful here. I'm wondering if a savebox might work. Jun 28, 2018 at 13:23
  • 1
    Whatever SyncTeX is supposed to do, it doesn't work on my system. Never mind. Jun 28, 2018 at 17:59

2 Answers 2


I found a solution, under the constraint that what one wants to re-read is delimited by an environment. Having no time to clean it up right now, I just post the raw example.

The work is done by the \InputPartialFile macro.

%% == filterfiles-selfreparse.tex



Line \the\inputlineno.
Line \the\inputlineno.
Line \the\inputlineno.

Line \the\inputlineno.
Line \the\inputlineno.
Line \the\inputlineno.

First paragraph inside fold.

\ifcsname IPF@nesting\endcsname\the\IPF@nesting\fi

  Stuff inside nested fold.

Second paragraph inside fold = end of fold.

\end{fold}After fold, same line.
After fold, next line.

%% Must use line-skipping instead of \@gobble, because
%% \@gobble updates \inputlineno only after the first character
%% of the line has been lost.
% next lines are whitespace-sensitive.

  %% Usage: \InputPartialFile{STARTLINE}{FILENAME}
  %% Read FILENAME from STARTLINE until a balanced \end{ENV} form.
  %% Technical limitation: Anything after the \end{..} on the same
  %% line will be included.
  %% Currently NOT nestable.
        \advance\IPF@nesting by 1
      }{}{\errmessage{Failure to patch \string\begin}}%
        \advance\IPF@nesting by -1
      }{}{\errmessage{Failure to patch \string\end}}%
  \@@input #2\relax


  • Idea summary: the solution \input the file, but use some TeX macro to "skip through" the first part of the content, and modify the \end macro to detect when the environment terminates and execute \endinput.
    – user202729
    Jan 27, 2022 at 11:17

With LuaLaTeX, using the callback it's possible to do this. See In LuaLaTeX, how do I pass the content of an environment to Lua verbatim? for other examples.

%! TEX program = lualatex

line 1

line 2

line 3

function readbuf(buf)
    if not (saved_synctex_tag==nil) then
        --[[ this code should only be executed once after the \\input ]]
    if 7<=tex.inputlineno and tex.inputlineno<=11 then
        return nil
        return ""



    luatexbase.add_to_callback('process_input_buffer', readbuf, 'readbuf')
    }\input{\currfilename}\directlua{luatexbase.remove_from_callback('process_input_buffer', 'readbuf')}



The \iffalse ... \fi is just for demonstration. There's no need for commands there, line number is sufficient.

Both forward and backward search works well. If you don't need the forward search outside the subfile, it's possible to remove the synctex parts.

Regarding the forward search, the problem here is that if there are multiple tag that corresponds to the same file, the synctex utility will only output the entry that corresponds to the last tag (read man 5 synctex and other sources for details of the synctex file format).

To fix it, it may be possible to use tex.set_synctex_tag in LuaTeX to be the same as the tag of the parent file, and do not record the tag entry of the subfile (with set_synctex_no_files).

Also make sure that the \input is on the same line as the \directlua, otherwise the one-time call will be executed before \input.

See also https://tex.stackexchange.com/a/632368/250119 for an example of the usage of the former function.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .