2

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}

\section{Please grab this non-existing tag}

\ExecuteMetaData[aux.tex]{missingtag}
\end{document}

HAVE: latexmk -pdf main.tex silently succeeds

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

3

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

\newrobustcmd*\OrigExecuteMetaData[2][\jobname]{%
\CatchFileBetweenTags\CatchFBT@tok{#1}{#2}%
\global\expandafter\CatchFBT@tok\expandafter{%
\expandafter}\the\CatchFBT@tok
}%\OrigExecuteMetaData

\newrobustcmd*\ChkExecuteMetaData[2][\jobname]{%
\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%
}%\ChkExecuteMetaData

\renewrobustcmd*\ExecuteMetaData[2][\jobname]{%
\ChkExecuteMetaData[#1]{#2}%
\OrigExecuteMetaData[#1]{#2}%
}

\makeatother

\begin{document}

\section{Please grab this non-existing tag}

\ExecuteMetaData[taggedaux.tex]{xfirsttag}

\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.
 ...                                              

l.29 \ExecuteMetaData[taggedaux.tex]{xfirsttag}

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

enter image description here

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.