
\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
오류가 발생합니다. -like 확장 \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
}