在嵌套文档中包含文件名

在嵌套文档中包含文件名

我有一个大型项目,分为多个文件。我想获取 pdf 中的每个子文件名来监控我的文档。

从该论坛上的不同帖子中,我了解到Currfile使用包可以提供帮助\currfilename。从这个帖子,我知道这太需要手动操作了,因为我可能有 100 个子文件。如果要添加\currfilename到每个子文件中,那将花费太长的时间。

有没有一种巧妙的方法来输入子文件的文件名,而\input不必Main.tex为每个子文件都输入文件名?换句话说,有没有办法在调用子文件\currfilename后立即包含?\input

\begin{filecontents}{File1.tex}
\currfilename
\lipsum
\end{filecontents}

\begin{filecontents}{File2.tex}
\currfilename
\lipsum
\end{filecontents}

\begin{filecontents}{File3.tex}
\currfilename
\lipsum
\end{filecontents}

\begin{filecontents}{File4.tex}
\currfilename
\lipsum
\end{filecontents}


\documentclass[10pt]{article}
\usepackage{currfile}
\usepackage{lipsum}

\begin{document}

\input{File1.tex}
\input{File2.tex}
\input{File3.tex}
\input{File4.tex}


\end{document}

答案1

您链接到了答案,但为了清楚起见,请在编辑器中尝试小心地找到 \input{ 并将其替换为 \inputf{

正如所查询的,这会影响编辑器从子文件跳转的能力,处理该问题最安全的方法是简单地在所有子文件前面添加行 \currfilename,下面我给出了两个更危险的替代解决方案。

在此处输入图片描述

\documentclass[10pt]{article}
%\usepackage{currfile} % not required
\usepackage{lipsum} % for demonstration each file contains \lipsum[1]

\newcommand\inputf[1]{\texttt{#1} \par\nobreak\input{#1}} % filename before
%\newcommand\inputf[1]{\input{#1}} % Per David's comment, uncomment this line and comment the line above to "resume normal service"

\begin{document}

\inputf{File1.tex}

\inputf{File2.tex}

\inputf{File3.tex}

\inputf{File4.tex}
\end{document}

我们可以坚持原来的要求,将 \currfile 添加到所有 \inputs 中(从而保留自然的工作流程。因此 main.tex 保持不变,但添加了一行)

\documentclass[10pt]{article}
\usepackage{currfile} %required for modified inputs
\usepackage{lipsum}  %for demo only

\begin{document}
\input{file1.tex} % note it is this name that the file has (lowercase file)
\input{File2.tex} % note in the screenshot this is the name that \input \currfilename uses, however the real file name is lowercase file..
\input{File3.tex}
\input{File4.tex}
\end{document}

在此处输入图片描述

每个 tex 文件前面都有一个文件头,存储在一个文件中curr文件包含

\texttt \currfilename \par

在 Windows 上,可以使用添加当前文件文件放在同一目录中。(您自行承担风险运行此操作我在第一行包含了一个安全备份但始终在复制文件夹上进行测试。)

for %%f in (file???.tex) do ren %%f %%~nf.bak
for %%f in (file???.bak) do copy currfile.txt/B+%%f/B %%~nf.tex/B

要将 tex 文件恢复到以前的行为,你可以编写删除/重命名脚本然而我确实说过保留一个没有更改的备份文件夹我没有吗?

最后可能是你正在寻找的解决方案但它有点危险,因为它修改了一个主要命令,因此可能会影响其他 \inputs,因此我无法控制你如何应用它。

将其添加到您的序言中,并在不需要时删除/注释掉。

% changing the syntax of a major command is not a good approach, so better to consider sticking with one of the two above solutions:
\makeatletter
\let\latex@input\input
\newcommand\current@input[1]{\texttt{#1} \par\nobreak\latex@input{#1}%
}
\AtBeginDocument{\let\input\current@input}
\makeatother

\begin{document}

相关内容