TransWikia.com

Partial file input

TeX - LaTeX Asked on July 20, 2021

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} 

One Answer

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 adamSTARTinputendcsname and csname adamSTOPinputendcsname:

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

csname adamSTARTinputendcsname

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

csname adamSTOPinputendcsname

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
definputfilepart#1{%
  letadamSTOPinputendinput
  longdefloadfilepartaux##1%
    csname adamSTARTinputendcsname{}
  expandafterloadfilepartaux@@input "#1"
}
makeatother

begin{document}

texttt{meaningpicZ} % undefined
inputfilepart{loadedfile.tex}
texttt{meaningpicZ} % 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).

Correct answer by Phelype Oleinik on July 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP