缺少标签时强制 catchfilebetweentags 失败

缺少标签时强制 catchfilebetweentags 失败

当重构期间在不同的文件中移动代码时,如果 catchfilebetweentags 可以在目标标签不存在时引发编译错误,那就太好了。

目前,它默默地成功了,没有在目标文档中插入任何内容,这使得很难追踪错误。

MWE:空aux.tex且包含以下main.tex文件:

\documentclass{article}

\usepackage{catchfilebetweentags}

\begin{document}

\section{Please grab this non-existing tag}

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

拥有:latexmk -pdf main.tex默默地成功

想要:失败,提到missingtag目标文件丢失aux.tex

答案1

解决此问题的一种方法是进行 的“试运行” \ExecuteMetaData,即运行修改后的命令版本,该命令将代码存储在宏中的标记之间,并测试此宏是否为空。如果是这种情况,您可以发出错误消息。试运行后,您应该使用 的原始版本进行“实际”运行\ExecuteMetaData。将令牌寄存器存储在命令中并同时将其插入输入流会很好,但不幸的是我无法做到这一点,因此在这种方法中使用了两次传递。

MWE(从源复制的命令定义catchfilebetweentags):

\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

终端结果:

! 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}

结果为\ExecuteMetaData[taggedaux.tex]{firsttag}

在此处输入图片描述

相关内容