7

I have a lot of content to assemble in several different ways and would like to use excerpts from one file in another. I have specified:

\usepackage{catchfilebetweentags}

I then have an external file with tagged sections something like this in it:

%<*tag>
\section{section: tag}

paragraph one

paragraph two
%</tag>

I can suck these into another document easily:

\ExecuteMetaData[bits.tex]{tag}

The problem is that when it comes over, there are no longer any pargraphs. The \section is properly formatted, but after that I can't make normal paragraphs work for love or money. Even adding \para has no effect.

Stragely enough if I just input the file it works fine:

\input{bits.tex}

...works as expected. The only problem is this gets the whole file, and I only want select portions of it. I looked at organizing the bits file with \inputs of their own but the system quickly gets messy and hard for my copy-writers to work on.

How do I get a normal paragraph environment back in content pulled over via \ExecuteMetaData?

  • It's much worse: endlines are discarded by \ExecuteMetaData, so you even get no space between one and paragraph. The package sets \endlinechar=-1 when reading the file. – egreg Mar 6 '14 at 19:12
  • 1
    Take a look at the clipboard package. – Fran Mar 6 '14 at 19:15
  • @Fran That is an interesting package, but it requires you to run tex an all the possible source files before you run it on the destination. That would rather complicate the life of my project rather that simplify it. It seems much more useful for back-referencing repeated content within the same document rather than for handling externals. – Caleb Mar 7 '14 at 6:50
  • @egreg So you are saying not only is it a witch, but it's the Wicked Witch of the West™? Is there no way to make it behave? Or a sane alternative that accomplishes the same task with less mucking about in my whitespace? – Caleb Mar 7 '14 at 6:53
  • @Caleb For a complicated life, use a simple script :) In Linux could be some as simple as for file in *.tex; do pdflatex "$file"; done – Fran Mar 7 '14 at 7:26
3

You can provide your own "tag" system via conditionals. Let me explain:

For each tag <tag>, define a conditional \if<tag> via \newif\if<tag>. Then, inside your document, brace the tagged group using \if<tag>...\fi. Now you can use \inputbetweentags{<tag>}{<filename>} to insert the file contents between the newly generated "tags".

Here is a minimal example:

enter image description here

\documentclass{article}
\setlength{\parindent}{0pt}% Just for this example
\usepackage{filecontents}% http://ctan.org/pkg/filecontents

\newif\iftagA% <tagA>
\newif\iftagB% <tagB>

\begin{filecontents*}{filename.tex}
\iftagA% <tagA>
\section{Section A}
A paragraph one

A paragraph two
\fi% </tagA>
\iftagB% <tagB>
\section{Section B}
B paragraph one

B paragraph two
\fi% </tagB>
\end{filecontents*}

\newcommand{\inputbetweentag}[2]{{% \inputbetweentag{<tag>}{<filename>}
  \expandafter\let\csname if#1\endcsname\iftrue% Make "tag" true
  \input{#2}}}% Input file
\begin{document}
Number 1

\inputbetweentag{tagA}{filename.tex}

Number 2

\inputbetweentag{tagB}{filename.tex}

Number 3
\end{document}
  • 1
    If I'm understanding this correctly filecontents is only for the purpose of showing this example as one file instead of two, but I would have to have the \newif\iftagX bits defined in the source file, not the target that I am importing to correct? And the % <tag> comments in this setup really are just comments, not functional hooks like they were with catchfilebetweentags, right` – Caleb Mar 7 '14 at 7:08
  • @Caleb: Yes filecontents was just for this example, and so was the use of commented <tags>. Yes, you would define the \newif\iftagX in your source file, since their values have to be set before including the file. – Werner Mar 7 '14 at 7:13
6

Here is a workaround, that avoids setting \endlinechar to an unwanted value. I don't know whether there are side effects.

\begin{filecontents*}{calebits.tex}
%<*tag>
\section{section: tag}

paragraph one

paragraph two
%</tag>
\end{filecontents*}

\documentclass{article}
\usepackage{catchfilebetweentags}
\makeatletter
\def\CatchFBT@sanitize{%
   \@sanitize
   \@makeother\{%
   \@makeother\}%
%   \endlinechar=`\^^J% <--- This line modified
}% \CatchFBT@sanitize
\makeatother

\begin{document}

\ExecuteMetaData[calebits.tex]{tag}

\end{document}

enter image description here

  • This causes an error if there is a comment in the catched part, giving Runaway text? ... ! File ended while scanning text of \CatchFBT@tok. <inserted text> } – Jasper Habicht Feb 14 '18 at 9:04
  • @JasperHabicht I’m not sure whether comments are supported at all inn the tagged file. – egreg Feb 14 '18 at 9:06
  • @egreg They are. The doc (page 2) says, that in-line comments are normally ignored, and non line-comments are always ignored. – Jasper Habicht Feb 14 '18 at 9:22
  • 1
    @JasperHabicht Well, you’ve found a side effect.😏 – egreg Feb 14 '18 at 9:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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