No poder escribir en el archivo e ingresar lo mismo

No poder escribir en el archivo e ingresar lo mismo
\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}

Respuesta1

No desea la expansión completa de \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}

ingrese la descripción de la imagen aquí

Una implementación alternativa con 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}

Respuesta2

Al compilar su ejemplo se produce un undefined control sequence: \@nilerror. \writelleva una \edefexpansión similar a -. El error proviene de la expansión completa de \textbf. Por tanto, se debe prestar cierta atención a la ampliación.

Supongamos que desea escribir el resultado de la expansión de un paso del comando en \features<#1>un archivo , intente esto:\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
}

información relacionada