1

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} 
2
  • 1
    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) Commented Oct 10, 2020 at 22:10
  • I would like to have a start and end point for the input block, as in the updated outline.
    – Adam
    Commented Oct 10, 2020 at 22:36

1 Answer 1

6

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
\ProvidesPackage{adampics}[2020-10-10 v1.0 Common definitions for Adam's documents]
\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

\csname adamSTARTinput\endcsname

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

\csname adamSTOPinput\endcsname

\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{%
  \let\adamSTOPinput\endinput
  \long\def\loadfilepartaux##1%
    \csname adamSTARTinput\endcsname{}
  \expandafter\loadfilepartaux\@@input "#1"
}
\makeatother

\begin{document}

\texttt{\meaning\picZ} % undefined
\inputfilepart{loadedfile.tex}
\texttt{\meaning\picZ} % \long macro:->...

\end{document}

(but you have to make sure that the anchors do exist in the loaded file, and that the anchors are not within braces).

1
  • 1
    That's very smart, Phelype!
    – frougon
    Commented Oct 11, 2020 at 13:36

You must log in to answer this question.

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