如何将 \StrGobbleLeft 输出写入外部文件?

如何将 \StrGobbleLeft 输出写入外部文件?

我还没有学会 TeX 扩展在后台是如何工作的。简而言之,如何将\StrGobbleLeft输出写入外部文件?

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{rawdata.txt}
beamerthemeAnnArbor.sty
beamerthemeAntibes.sty
beamerthemeBergen.sty
beamerthemeBerkeley.sty
beamerthemeBerlin.sty
beamerthemeBoadilla.sty
beamerthemeboxes.sty
beamerthemeCambridgeUS.sty
beamerthemeCopenhagen.sty
beamerthemeDarmstadt.sty
beamerthemedefault.sty
beamerthemeDresden.sty
beamerthemeEastLansing.sty
beamerthemeFrankfurt.sty
beamerthemeGoettingen.sty
beamerthemeHannover.sty
beamerthemeIlmenau.sty
beamerthemeJuanLesPins.sty
beamerthemeLuebeck.sty
beamerthemeMadrid.sty
beamerthemeMalmoe.sty
beamerthemeMarburg.sty
beamerthemeMontpellier.sty
beamerthemePaloAlto.sty
beamerthemePittsburgh.sty
beamerthemeRochester.sty
beamerthemeSingapore.sty
beamerthemeSzeged.sty
beamerthemeWarsaw.sty
beamerthemebars.sty
beamerthemeclassic.sty
beamerthemecompatibility.sty
beamerthemelined.sty
beamerthemeplain.sty
beamerthemeshadow.sty
beamerthemesidebar.sty
beamerthemesplit.sty
beamerthemetree.sty
\end{filecontents*}

\usepackage{xstring}

\begin{document}
% writer
\newwrite\writer
\immediate\openout\writer=list.txt\relax
% reader
\newread\reader
\openin\reader=rawdata.txt\relax
\makeatletter
\loop
    \read\reader to \x
    \unless\ifeof\reader
        \filename@parse{\x}
        \immediate\write\writer{\StrGobbleLeft{\filename@base}{11}}
\repeat
\makeatother
\closein\reader
\immediate\closeout\writer
\end{document}

答案1

\StrGobbleLeft不可扩展,因此它无法在 内部工作\write。但该包xstring非常贴心地在最后提供了一个可选参数。如果存在,它将采用存储结果的宏名。

\StrGobbleLeft{\filename@base}{11}[\Result]
\immediate\write\writer{\Result}

答案2

如果您想将其放置在写入中,则需要一个通过扩展起作用的宏,例如:

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{rawdata.txt}
beamerthemeAnnArbor.sty
beamerthemeAntibes.sty
beamerthemeBergen.sty
beamerthemeBerkeley.sty
beamerthemeBerlin.sty
beamerthemeBoadilla.sty
beamerthemeboxes.sty
beamerthemeCambridgeUS.sty
beamerthemeCopenhagen.sty
beamerthemeDarmstadt.sty
beamerthemedefault.sty
beamerthemeDresden.sty
beamerthemeEastLansing.sty
beamerthemeFrankfurt.sty
beamerthemeGoettingen.sty
beamerthemeHannover.sty
beamerthemeIlmenau.sty
beamerthemeJuanLesPins.sty
beamerthemeLuebeck.sty
beamerthemeMadrid.sty
beamerthemeMalmoe.sty
beamerthemeMarburg.sty
beamerthemeMontpellier.sty
beamerthemePaloAlto.sty
beamerthemePittsburgh.sty
beamerthemeRochester.sty
beamerthemeSingapore.sty
beamerthemeSzeged.sty
beamerthemeWarsaw.sty
beamerthemebars.sty
beamerthemeclassic.sty
beamerthemecompatibility.sty
beamerthemelined.sty
beamerthemeplain.sty
beamerthemeshadow.sty
beamerthemesidebar.sty
beamerthemesplit.sty
beamerthemetree.sty
\end{filecontents*}

\usepackage{xstring}

\begin{document}
% writer
\newwrite\writer
\immediate\openout\writer=list.txt\relax
% reader
\newread\reader
\openin\reader=rawdata.txt\relax
\makeatletter
\loop
    \read\reader to \x
    \unless\ifeof\reader
        \filename@parse{\x}%
        \def\zaptheme beamertheme{}%
        \immediate\write\writer{\expandafter\zaptheme \filename@base}
\repeat
\makeatother
\closein\reader
\immediate\closeout\writer
\end{document}

相关内容