# Force catchfilebetweentags to fail upon missing tag

When moving code around different files during refactoring it would be nice if catchfilebetweentags could raise a compilation error when the target tag does not exist.

At the moment it silently succeeds without inserting anything in the target document which makes it hard to track down the errors.

MWE: empty aux.tex and the following main.tex file:

\documentclass{article}

\usepackage{catchfilebetweentags}

\begin{document}

\end{document}


HAVE: latexmk -pdf main.tex silently succeeds

WANT: failure, mentioning missing missingtag in target file aux.tex.

One way to approach this issue is to do a 'dry run' of \ExecuteMetaData, i.e., run a modified version of the command that stores the code between the tags in a macro and test if this macro is empty. If that is the case you can issue an error message. After the dry run you should do a 'real' run with the original version of \ExecuteMetaData. It would be nice to store the token register in a command and insert it to the input stream at the same time, but I could not make that work unfortunately, so two passes are used in this approach.

MWE (command definitions copied from the catchfilebetweentags source):

\documentclass{article}
\usepackage{catchfilebetweentags}

\makeatletter

\CatchFileBetweenTags\CatchFBT@tok{#1}{#2}%
\global\expandafter\CatchFBT@tok\expandafter{%
\expandafter}\the\CatchFBT@tok

\CatchFileBetweenTags\CatchFBT@tok{#1}{#2}%
\edef\mytokens{\detokenize\expandafter{\the\CatchFBT@tok}}
\ifx\mytokens\empty\PackageError{catchfilebetweentags}{the tag #2 is not found\MessageBreak in file #1 \MessageBreak called from \jobname.tex}{use a different tag}\fi%

}

\makeatother

\begin{document}

\end{document}


taggedaux.tex:

abc

%<*firsttag>
this is the \textbf{contents} of the first tag
%</firsttag>

xyz


Result in terminal:

! Package catchfilebetweentags Error: the tag xfirsttag is not found
(catchfilebetweentags)                in file taggedaux.tex
(catchfilebetweentags)                called from betweentagserror.tex.

See the catchfilebetweentags package documentation for explanation.
Type  H <return>  for immediate help.
...


Result with \ExecuteMetaData[taggedaux.tex]{firsttag}: