可扩展读取文件

可扩展读取文件

这个问题接着这个问题应用存储在文件中的 TikZ 选项。我只是想知道为什么以下内容无法读取文件。请忽略 filecontents 的内容;虽然它可以工作,但它只是为了开发 MNWE。问题出在我使用的\input

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents}{myaux.tex}
\documentclass{article}
\makeatletter
\def\chapno{1}
\def\exno{1.1}
\chardef\other=12
\newwrite\ans
\immediate\openout\ans=answers
\def\answer{%
  \par\medbreak
  \immediate\write\ans{}%
  \immediate\write\ans{\string\ansno\chapno.\exno:}%
  \copytoblankline
}
\def\copytoblankline{\begingroup\setupcopy\copyans}
\def\setupcopy{%
  \def\do##1{\catcode`##1=\other}%
  \dospecials
  \catcode`\|=\other
  \obeylines%
}
\def\ifnextisspace{%
  \expandafter\futurelet\expandafter\next
  \expandafter\ifn@xtisspace\string
}
\def\ifn@xtisspace{%
  \ifx\next\@sptoken\expandafter\gobblespace
    \else\expandafter\writetofile\fi
}
\@namedef{gobblespace} {\ifnextisspace}
\def\writetofile#1\@nil{\immediate\write\ans{#1}}

{\obeylines %
  \gdef\copyans#1
  {\def\next{#1}%
    \ifx\next\@empty %
      \let\next=\endgroup %
    \else %
      \expandafter\ifnextisspace\next\@nil %
      \let\next=\copyans %
    \fi %
    \next %
  }%
}

\answer xxx
yyy \ifx xx
\fi zzz

\begin{document}
x
\end{document}
\end{filecontents}

\makeatletter
\begingroup
\lccode`\A=1 \lccode`\B=1 \catcode`\A=7
\lowercase{\endgroup\def\catch@marker{AB}}
\edef\reserved@a{%
  \long\def\noexpand\catchfile##1\catch@marker{%
    \noexpand\unexpanded\noexpand\expandafter{##1}%
  }%
}
\reserved@a
\def\catchfilecontent#1#2{%
  \begingroup
  \everyeof\expandafter{\catch@marker\noexpand}%
  \makeatletter
  \edef\reserved@a{\endgroup
    \long\def\noexpand#1{%
      \expandafter\catchfile\expandafter\@empty
      \@@input #2
    }%
  }%
  \reserved@a
}
\makeatother

% Test
\catchfilecontent\myauxfile{myaux.tex}

\begin{document}
x
\end{document}

答案1

我还没有看过捕获文件包,但逐字逐句地#解决问题。我还没有想到其他解决方法。

\def\catchfilecontent#1#2{%
  \begingroup
  \def\do##1{\catcode`##1=12}%
  \dospecials
  \catcode`\|=12
  \obeylines%
  \everyeof\expandafter{\catch@marker\noexpand}%
  \makeatletter%
  \edef\reserved@a{\endgroup%
    \long\def\noexpand#1{%
      \expandafter\catchfile\expandafter\@empty%
      \@@input #2 %
    }%
  }%
  \reserved@a%
}

相关内容