使用多个 \input 从 .tex 文件构建一个单独的 .tex 文件

使用多个 \input 从 .tex 文件构建一个单独的 .tex 文件

编辑: 我认为这个问题与这个问题因为我特别要求 TeX 本身内的方法,即不单独运行某些 Perl 脚本来执行此操作,而是在编译源代码时通过某些 TeX 魔法自动执行此操作。

编辑2: 为了给这个问题提供一些动机,我在序言中一直使用将\embedfile{jobname.tex}源代码嵌入 PDF,但当你使用\includes 或\inputs 时,这不起作用。有\usepackage{embedall}(然后),但这意味着你的源文件是单独嵌入的。我假设,在编译和制作 PDF 时,TeX 会在某个时候生成一个包含所有s 和s 扩展\embedinput的单个 .tex 文件,我希望能够使用它来嵌入\include\input\embedfile单个文件转换为 PDF。

我似乎无法在任何地方找到这个问题的答案,如果之前有人问过的话,我很抱歉。

假设我有一些main.tex包含序言等的文件,并且我的所有章节都在某个chapters目录中,并且我使用\input{chapter1}main.tex那么有没有办法将这个结构“编译”成一个.tex文件,即每个文件\input实际上都展开并替换为内容chapters/chapter1.tex、、chapters/chapter2.tex等等?

我确信使用某些 bash 脚本来做这件事不会太麻烦,但我希望有一些 (La)TeX 方式可以做到这一点 — — 也许在编译过程中会生成一些这样的文件?


main.tex

[preamble]

\input{chapters/chapter1.tex}

\input{chapters/chapter2.tex}

...

chapters/chapter1.tex

\chapter{First chapter}

Lorem ipsum

chapters/chapter2.tex

\chapter{Second chapter}

Dolor sit amet

应该成为

main_single.tex

[preamble]

\chapter{First chapter}

Lorem ipsum

\chapter{Second chapter}

Dolor sit amet

...

答案1

例如,当涉及文件的条件输入/包含时,我发现从源文件自动构建单个文件的方法存在问题。

还要注意,如果 pdf 文件的某些内容由源创建的随机性起着作用,则单独嵌入源可能无法提供重新创建具有完全相同外观的 pdf 文件所需的所有方法。

源可能包含如下指令:

   如果遇到该指令时是早上,请包括早上好.tex
   如果遇到该指令时是下午,请包括下午好.tex
   如果遇到该指令时是晚上,请包括晚安.tex

或者在源中,可能已经加载 pgf 包来生成和显示随机数。

源代码可能包含显示编译开始时的当前系统日期和时间的指令。(在其他时间进行编译时,情况会有所不同。)

该源可能包含从练习池中随机提供练习组合的指令。

...

是因为它可能。

我建议往另一个方向走:

结合你的主文本和您的输入文件“手动”在一个源文件中,从该源文件中可以(半)自动地通过文档条包裹。 (这文档条包裹是标准 LaTeX 安装的一部分。)

文档条包裹是一种从一个(一组)主文件派生出不同 .tex 输入文件的方法:
在主文件中,您可以放置​​所谓的标签。这些标签用于确定派生文件的内容,因为主文件中带标签的文本部分将被复制到派生文件中。

例如,在主主文件的头部,您可以调用 docstrip 并使用所谓的\generate指令来创建那些要从主文件派生的文件。

有关更多信息文档条可以在 CTAN 找到:https://ctan.org/pkg/docstrip

如果将以下示例保存为文件,并且文件名不是主文本并且除此以外第一章.tex并且除此以外第二章.tex 并通过从命令行/shell 调用或或来编译它
tex ⟨file name⟩
pdftex ⟨file name⟩
latex ⟨file name⟩
pdflatex ⟨file name⟩
(通过cd-command 或chdir-command 或其他)将工作目录更改为保存该文件的目录,系统会要求您创建子目录“chapters”。
完成此操作后,将创建三个新文件:
文件主文本位于当前目录中。
文件第一章.tex第二章.tex将在子目录中章节

%<*ignore>
% Section with docstrip directives for generating different 
% .tex-input-files:
\input docstrip
% Patch docstrip's \makeOther so that it also makes horizontal
% tab-char other (code-point 9 in ASCII and UTF 8)
% when making space-char other - if you don't apply this patch,
% docstrip will kill horizontal tabs at the beginnings of lines
% and will turn horizontal-tabs within lines into spaces:
\def\makeOther#1{%
  \ifnum`\ =`#1 \catcode9=12\relax\fi
  \catcode`#1=12\relax
}%
%
\nopreamble
\nopostamble
\askforoverwritetrue
\begingroup
\obeyspaces
\message{%
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!^^J%
!! Attention 1:                                            !!^^J%
!! ------------                                            !!^^J%
!! Make sure to have changed the working directory to the  !!^^J%
!! directory where the file that is currently compiled is  !!^^J%
!! stored.                                                 !!^^J%
!! ------------------------------------------------------- !!^^J%
!! Attention 2:                                            !!^^J%
!! ------------                                            !!^^J%
!! Make sure you have created the sub-directory "chapters" !!^^J%
!! within the directory where the file that is currently   !!^^J%
!! compiled is stored.                                     !!^^J%
!! You need to do this by hand as TeX won't do it for you. !!^^J%
!! If that sub-directory does not yet exist, you can do it !!^^J%
!! now.                                                    !!^^J%
!!                                                         !!^^J%
!! Press <return> after creating that sub-directory/when   !!^^J%
!! that sub-directory exists.                              !!^^J%
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!^^J%
}%
\endgroup
\immediate\read-1 to \mytempmacro
\generate{%
  \file{main.tex}{\from{\jobname.tex}{main}}%
  \file{./chapters/chapter1.tex}{\from{\jobname.tex}{chapter1}}%
  \file{./chapters/chapter2.tex}{\from{\jobname.tex}{chapter2}}%
}%
% Here you can probably insert \write18-calls suitable for your platform
% for calling pdflatex from console for compiling main.tex
%
%    \immediate\write18{pdflatex main.tex}
%    \immediate\write18{pdftexify main.tex}
%    \immediate\write18{latexmk -pdf main.tex}
%
\csname stop\endcsname % <- end this TeX-run in case you used (pdf)LaTeX
                       %    for generating the files.
\bye % <- end this TeX-run in case you used (pdf)TeX for generating the 
%    %    files.
%
%</ignore>
%
% In verbatim mode docstrip does not strip comments.
%
% Section containing main.tex
%
%<*main>
%<<This weird line does put DocStrip into and out of verbatim mode.
\documentclass{book}
% [preamble]
\begin{document}
\input{chapters/chapter1.tex}
\input{chapters/chapter2.tex}
\end{document}
%This weird line does put DocStrip into and out of verbatim mode.
%</main>
%
% Section containing chapter1.tex
%
%<*chapter1>
%<<This weird line does put DocStrip into and out of verbatim mode.
\chapter{First chapter}

Lorem ipsum
%This weird line does put DocStrip into and out of verbatim mode.
%</chapter1>
%
% Section containing chapter2.tex
%
%<*chapter2>
%<<This weird line does put DocStrip into and out of verbatim mode.
\chapter{Second chapter}

Dolor sit amet
%This weird line does put DocStrip into and out of verbatim mode.
%</chapter2>


如果你实在不喜欢朝另一个方向走:

如果没有嵌套的输入/包含,即如果结构是简单的模式,主文本包含序言、文档环境和一些\include包含单个章节的文件的指令(而这些文件又不包含这样的\include/\input指令),可能在文件中添加一些通常情况下会被 LaTeX 用作注释的内容,就足以将您的文件集转换为可以通过调用以下小脚本在单个文件中组合的内容文档条

即如果你有文件主文本

%<*BeforeChapterInput>
%<<This weird line does put DocStrip into and out of verbatim mode.
\documentclass{book}
% [preamble]
\begin{document}
%This weird line does put DocStrip into and out of verbatim mode.
%</BeforeChapterInput>
%<*ChapterInput>
\input{chapters/chapter1.tex}
\input{chapters/chapter2.tex}
%</ChapterInput>
%<*AfterChapterInput>
%<<This weird line does put DocStrip into and out of verbatim mode.
\end{document}
%This weird line does put DocStrip into and out of verbatim mode.
%</AfterChapterInput>

并在子目录中章节文件第一章.tex

%<<This weird line does put DocStrip into and out of verbatim mode.
\chapter{First chapter}

Lorem ipsum
%This weird line does put DocStrip into and out of verbatim mode.

和文件第二章.tex

%<<This weird line does put DocStrip into and out of verbatim mode.
\chapter{Second chapter}

Dolor sit amet
%This weird line does put DocStrip into and out of verbatim mode.

, 一份文件合并.tex可以创建如下内容,

\documentclass{book}
% [preamble]
\begin{document}
\chapter{First chapter}

Lorem ipsum
\chapter{Second chapter}

Dolor sit amet
\end{document}

,通过以下脚本 - 它必须存储在与主文本 并且需要通过 从命令行/从 shell调用或或来进行编译
tex ⟨file name⟩
pdftex ⟨file name⟩
latex ⟨file name⟩
pdflatex ⟨file name⟩
(通过cd-command 或chdir-command 或其他)将工作目录更改为保存 main.tex 和该脚本的目录。

% Section with docstrip directives for generating different 
% .tex-input-files:
\input docstrip
% Patch docstrip's \makeOther so that it also makes horizontal
% tab-char other (code-point 9 in ASCII and UTF 8)
% when making space-char other - if you don't apply this patch,
% docstrip will kill horizontal tabs at the beginnings of lines
% and will turn horizontal-tabs within lines into spaces:
\def\makeOther#1{%
  \ifnum`\ =`#1 \catcode9=12\relax\fi
  \catcode`#1=12\relax
}%
%
\nopreamble
\nopostamble
\askforoverwritetrue
\begingroup
\obeyspaces
\message{%
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!^^J%
!! Attention 1:                                            !!^^J%
!! ------------                                            !!^^J%
!! Make sure to have changed the working directory to the  !!^^J%
!! directory where the file that is currently compiled is  !!^^J%
!! stored.                                                 !!^^J%
!! ------------------------------------------------------- !!^^J%
!! Attention 2:                                            !!^^J%
!! ------------                                            !!^^J%
!! Make sure the file main.tex is in the same directory as !!^^J%
!! the file that is currently compiled.                    !!^^J%
!! Also make sure that that directory has a subdirectory   !!^^J%
!! "chapters" where the files "chapter1.tex" and           !!^^J%
!! "chapter2.tex" can be found.                            !!^^J%
!!                                                         !!^^J%
!! Press <return> to continue.                             !!^^J%
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!^^J%
}%
\endgroup
\immediate\read-1 to \mytempmacro
\generate{%
  \file{combined.tex}{%
    \from{main.tex}{BeforeChapterInput}%
    \from{./chapters/chapter1.tex}{}%
    \from{./chapters/chapter2.tex}{}%
    \from{main.tex}{AfterChapterInput}%
  }%
}%
% Here you can probably insert \write18-calls suitable for your platform
% for calling pdflatex from console for compiling combined.tex
%
%    \immediate\write18{pdflatex combined.tex}
%    \immediate\write18{pdftexify combined.tex}
%    \immediate\write18{latexmk -pdf combined.tex}
%
\csname stop\endcsname % <- end this TeX-run in case you used (pdf)LaTeX
                       %    for generating the files.
\bye % <- end this TeX-run in case you used (pdf)TeX for generating the 
%    %    files.

答案2

我发现这latexpand对我有用,用引用的文件内容替换所有我的\input和命令。\include

latexpandCTAN 包装:https://ctan.org/pkg/lateexpand

相关内容