Partial file input

Is there a way to input a part of a LaTeX file1.tex specified by say some markers into another LaTeX file file2.tex? (For example, I don't want to input \begin{document}, \maketitle, etc.)

I know that I could create file3.tex with the input stuff for file1.tex and file2.tex, but being able to do it directly (as above) would be much more convenient.

Here's an example of file1.tex with what I want to include in file2.tex:

\documentclass[11pt]{amsart}
\title{My Tikz Pictures}
\begin{document}
\maketitle

% Start input here

\newcommand{\picA}{...}
\newcommand{\picZ}{...}

% End input here

\begin{document}
....
\end{document}

• Depends if you want to input the file up to a given point, or from that point onwards, but it should be simple either way. Can you please clarify (maybe with an example skeleton of the files) – Phelype Oleinik Oct 10 '20 at 22:10
• I would like to have a start and end point for the input block, as in the updated outline. – Adam Oct 10 '20 at 22:36

If what you want to include are common definitions for a project of yours, I recommend you make a package file, say, adampics.sty:

% Next line is optional
\newcommand{\picA}{...}
\newcommand{\picZ}{...}


then just do \usepackage{adampics} in your documents.

If, for some reason, you do need to read in the complete file and extract a part of it, then you can do something like this. In the file to be input, add two anchors \csname adamSTARTinput\endcsname and \csname adamSTOPinput\endcsname:

% loadedfile.tex
\documentclass[11pt]{amsart}
\title{My Tikz Pictures}
\begin{document}
\maketitle

\newcommand{\picA}{...}
\newcommand{\picZ}{...}

\begin{document}
....
\end{document}


being a \csname ...\endcsname-generated macro, they will do nothing as long as they aren't defined, so running the document above standalone will proceed normally.

Then in the caller document, do:

\documentclass{article}

\makeatletter
\def\inputfilepart#1{%
}
\makeatother

\begin{document}

\texttt{\meaning\picZ} % undefined