无法写入文件并输入相同内容

无法写入文件并输入相同内容
\documentclass{minimal}
\usepackage{etoolbox}

\newwrite\tempfile

\makeatletter
\let\features\@gobble
\makeatother
\newcommand{\feature}[1]{%
  \expandafter\def\expandafter\features\expandafter{\features,#1}%
}

\newcommand{\newText}[1]{\expandafter\newcommand\csname features#1\endcsname{}}

\newcommand{\addText}[2]{%
 \expandafter\ifdefempty\expandafter{\csname features#1\endcsname}
 {\expandafter\appto\csname features#1\endcsname{#2}}
 {\expandafter\appto\csname features#1\endcsname{,#2}}%
}

\def\lstText#1{\csname features#1\endcsname}

\makeatletter
\newcommand{\outText}[1]{%
 \immediate\openout\tempfile=\jobname_#1.tex
 \immediate\write\tempfile{\lstText{#1}}
 \immediate\closeout\tempfile
}
\makeatother

\makeatletter
\let\inputIFE\@input
\makeatother

\begin{document}

\inputIFE{test_A}

\newText{A}
\addText{A}{One}
\addText{A}{Two}
\addText{A}{Three

Birds and \textbf{bears}
}
\addText{A}{Four}
\lstText{A}
\outText{A}

\end{document}

答案1

您不希望 全面扩张\featuresA

\documentclass{article}
\usepackage{etoolbox}

\newwrite\tempfile

\newcommand{\newText}[1]{\expandafter\newcommand\csname features#1\endcsname{}}
\newcommand{\addText}[2]{%
 \ifcsempty{features#1}{\csappto{features#1}{#2}}{\csappto{features#1}{,#2}}%
}
\providecommand{\expandtwice}{\unexpanded\expandafter\expandafter\expandafter}

\newcommand{\outText}[1]{%
 \immediate\openout\tempfile=\jobname_#1.tex
 \immediate\write\tempfile{\expandtwice{\csname features#1\endcsname}}%
 \immediate\closeout\tempfile
}

\newcommand\lstText[1]{\csname features#1\endcsname}

\makeatletter
\let\inputIFE\@input
\makeatother

\begin{document}

With \verb|\inputIFE|

\inputIFE{\jobname_A}

\newText{A}
\addText{A}{One}
\addText{A}{Two}
\addText{A}{Three

Birds and \textbf{bears}
}
\addText{A}{Four}

\bigskip

With \verb|\lstText|

\lstText{A}

\outText{A}

\end{document}

在此处输入图片描述

另一种实现方式是expl3

\documentclass{article}

\ExplSyntaxOn

\iow_new:N \g_example_out_iow

\NewDocumentCommand{\newText}{m}
  {
    \clist_clear_new:c { l_example_#1_clist }
  }
\NewDocumentCommand{\addText}{m +m}
  {
    \clist_put_right:cn { l_example_#1_clist } { #2 }
  }

\NewDocumentCommand{\outText}{m}
  {
    \iow_open:Nn \g_example_out_iow { \c_sys_jobname_str #1 }
    \iow_now:Nx \g_example_out_iow
      {
        \exp_not:v { l_example_#1_clist }
      }
  }

\NewDocumentCommand\lstText{m}
  {
    \clist_use:cn { l_example_#1_clist } { , }
  }

\ExplSyntaxOff

\makeatletter
\let\inputIFE\@input
\makeatother

\begin{document}

With \verb|\inputIFE|

\inputIFE{\jobname_A}

\newText{A}
\addText{A}{One}
\addText{A}{Two}
\addText{A}{Three

Birds and \textbf{bears}
}
\addText{A}{Four}

\bigskip

With \verb|\lstText|

\lstText{A}

\outText{A}

\end{document}

答案2

编译示例时会出现undefined control sequence: \@nil错误。\write带有\edef类似 - 的扩展。错误来自 的完全扩展\textbf。因此,应注意扩展。

假设您想将命令的一步扩展结果写入\features<#1>文件\jobname_#1.tex\outText,请尝试以下操作:

\newcommand{\outText}[1]{%
  \immediate\openout\tempfile=\jobname_#1.tex
  % write out content of \feature<#1>
  \immediate\write\tempfile{%
    \expandafter\expandafter\expandafter\unexpanded
    \expandafter\expandafter\expandafter{\csname features#1\endcsname}}%
  \immediate\closeout\tempfile
}

相关内容