如何合并这样的两个文件?

如何合并这样的两个文件?

我有这些代码

特克斯

\tag{1.1}{this is 1.1 from file a.}
\tag{1.2}{this is 1.2 from file a.}

豐特克斯

\tag{1.1}{this is 1.1 from file b.}
\tag{1.2}{this is 1.2 from file b.}

主文本

\documentclass{article}
\somedef... ?
\begin{document}
  \input{a.tex}\\
  \input{b.tex}\\

  \print{a:1.1}\\ % result 1 line

  \print{b:1.2}\\ % result 1 line

  \printa{1.1}\\ % result 2 lines

  \printa{1.2}   % result 2 lines
\end{document}

我希望得到这个

this is 1.1 from file a. 

this is 1.2 from file b.

this is 1.1 from file a.
this is 1.1 from file b.

this is 1.2 from file a.
this is 1.2 from file b.

答案1

重要的提示:在下面的代码中,filecontents仅用于使示例自成一体。

我将每个标签存储在一个属性列表中,其键的形式为

<filename>@<tag>

和给定的值。

\print在冒号处拆分其参数;如果存在冒号,则打印对应的值<filename>@<tag>(前提是存在);否则对所有存储的文件名执行循环。

\begin{filecontents}{a.tex}
\tag{1.1}{this is 1.1 from file a.}
\tag{1.2}{this is 1.2 from file a.}
\end{filecontents}

\begin{filecontents}{b.tex}
\tag{1.1}{this is 1.1 from file b.}
\tag{1.2}{this is 1.2 from file b.}
\end{filecontents}

\documentclass{article}
\usepackage{xparse}

\NewDocumentCommand{\print}{>{\SplitArgument{1}{:}}m}{\printaux#1}

\ExplSyntaxOn

\NewDocumentCommand{\printaux}{mm}
 {
  \IfNoValueTF{#2}
   {
    \zeno_print_all:n { #1 }
   }
   {
    \zeno_print_one:nn { #1 } { #2 }
   }
 }

\NewDocumentCommand{\taginput}{m}
 {
  \clist_map_inline:nn { #1 }
   {
    \seq_gput_right:Nn \g_zeno_taginput_seq { ##1 }
    \zeno_taginput_file:n { ##1 }
   }
 }

\seq_new:N \g_zeno_taginput_seq
\prop_new:N \g_zeno_taginput_data_prop
\cs_generate_variant:Nn \prop_gput:Nnn { Nx }

\cs_new_protected:Nn \zeno_taginput_file:n
 {
  \tl_set:Nn \l__zeno_taginput_current_tl { #1 }
  \file_input:n { #1 }
 }

\cs_new_protected:Npn \tag #1 #2
 {
  \prop_gput:Nxn \g_zeno_taginput_data_prop
   { \l__zeno_taginput_current_tl @ #1 }
   { #2 }
 }

\cs_new:Nn \zeno_print_one:nn
 {
  \prop_item:Nn \g_zeno_taginput_data_prop {#1 @ #2 } \par
 }

\cs_new_protected:Nn \zeno_print_all:n
 {
  \seq_map_inline:Nn \g_zeno_taginput_seq
   {
    \zeno_print_one:nn { ##1 } { #1 }
   }
 }

\ExplSyntaxOff

\begin{document}

\taginput{a,b}

\print{a:1.1}   % result 1 line

\print{b:1.2}   % result 1 line

\bigskip

\print{1.1}    % result 2 lines

\bigskip

\print{1.2}    % result 2 lines

\end{document}

在此处输入图片描述

具有稍微不同语法的“经典”实现。

\begin{filecontents}{a.tex}
\tag{1.1}{this is 1.1 from file a.}
\tag{1.2}{this is 1.2 from file a.}
\end{filecontents}

\begin{filecontents}{b.tex}
\tag{1.1}{this is 1.1 from file b.}
\tag{1.2}{this is 1.2 from file b.}
\end{filecontents}

\documentclass{article}

\makeatletter
\newcommand{\print}[2][]{%
  \if\relax\detokenize{#1}\relax
    \print@all{#2}%
  \else
    \print@one{#1}{#2}%
  \fi
}

\newcommand{\taginput}[1]{%
  \ifx\taginput@files\@empty
    \gdef\taginput@files{#1}%
  \else
    \g@addto@macro\taginput@files{,#1}%
  \fi
  \def\taginput@current{#1}%
  \input{#1}%
}
\let\taginput@files\@empty

\newcommand{\tag}[2]{%
  \global\@namedef{taginput@\taginput@current @#1}{#2}%
}

\newcommand{\print@all}[1]{%
  \begingroup\edef\x{\endgroup
    \noexpand\@for\noexpand\next:=\taginput@files\noexpand\do
  }\x{\print@one{\next}{#1}}%
}
\newcommand{\print@one}[2]{%
  \@nameuse{taginput@#1@#2}\par
}
\makeatother

\begin{document}

\taginput{a}
\taginput{b}

\print[a]{1.1}   % result 1 line

\print[b]{1.2}   % result 1 line

\bigskip

\print{1.1}    % result 2 lines

\bigskip

\print{1.2}    % result 2 lines

\end{document}

相关内容