使用@nameuse从原始文件输出结果文件

使用@nameuse从原始文件输出结果文件

我有一个文件 a.tex

\documentclass{article}

\begin{document}

\makeatletter

\gdef\wbsinput@files{,\jobname}
\gdef\wbsinput@current{\jobname}

\long\def\wbs#1#2{
   {\expandafter\ifx\csname wbsinput@\wbsinput@current @#1\endcsname \relax
      \global\@namedef{wbsinput@\wbsinput@current @#1}{#2\par}%
      \else
    \global\expandafter\addto\csname wbsinput@\wbsinput@current
      @#1\endcsname{#2\par}\fi}
}

\long\def\addto#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}}



\newcommand{\print}[1]{%
    \print@all{#1}
}

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

\newcommand{\print@one}[2]{%
  \@nameuse{wbsinput@#1@#2}\par
}


\makeatother

\wbs{1.1}{This is a line 1.}

\wbs{1.1}{This is a line 2.}

\print{1.1}

\end{document}

我想要得到一个结果 b.tex 类似


\documentclass{article}

\begin{document}

This is a line 1.

This is a line 2.

\end{document}

我应该怎么办 ?

答案1

您可以使用以下功能创建并写入文件:newfile。以下是最简单的例子:

\documentclass{article}

\usepackage{newfile}

\newoutputstream{myout}
\openoutputfile{b.tex}{myout}
\begin{writeverbatim}{myout}
\documentclass{article}

\begin{document}
\end{writeverbatim}

\begin{document}

test

\addtostream{myout}{This is a line 1.}

\addtostream{myout}{This is a line 2.}

\addtostream{myout}{\string\end{document}}

\closeoutputstream{myout}

\end{document}

创建的文件b.tex包含以下内容:

\documentclass{article}

\begin{document}
This is a line 1.
This is a line 2.
\end{document}

相关内容