# How to capture a subset of a Latex document? [duplicate]

I would like to programmatically capture a subset of a latex document, something like:

doc = new Doc("foo.tex")
node = doc.getNode("bar")
print node.text


where node "bar" would be delimited by some tags, put for example in comments:

%% <bar>
This the \textbf{node content}
%% </bar>


How to do this?

• This is not really a TeX question but depends on the programming language you want to use as well as on the external tools. There are many ways for opening a text file and selecting a substring. – gernot Dec 8 '16 at 12:34
• I agree, but TeX files have many specificities to be leveraged to do this elegantly and efficiently. – Martin Monperrus Dec 8 '16 at 12:38
• In a Unix environment nothing is simpler than an awk one-liner that removes everything outside of the tags. But whether this is adequate depends on the environment in which you want to use it. Using LaTeX to extract the fragments is certainly an overkill since you need a TeX installation for that. But let's see whether someone comes up with a TeX solution to the problem; I wouldn't hold my breath, though. – gernot Dec 8 '16 at 12:44
• May be ctan.org/pkg/clipboard ? – Fran Dec 8 '16 at 12:51
• clipboard looks very much appropriate, thanks a lot. I'll give it a try. – Martin Monperrus Dec 9 '16 at 8:02

Sounds like a good job for the catchfilebetweentags package:

\documentclass{article}

\usepackage{filecontents}
\begin{filecontents*}{input.tex}
not this text

%<*bar>
This the \textbf{node content}
%</bar>

not this text either
\end{filecontents*}

\usepackage{catchfilebetweentags}

\begin{document}


• @Sigur If they are explicitly given with \par they will work, empty lines are allowed, just will not have an effect. – user36296 Jan 2 '18 at 15:47