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?

  • 2
    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
  • 1
    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:


not this text

This the \textbf{node content}

not this text either




  • Are new paragraphs allowed within the tag? – Sigur Jan 2 '18 at 15:36
  • 1
    @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

Not the answer you're looking for? Browse other questions tagged or ask your own question.