我正在尝试做这样的事情(\read
是一个伪命令):
\typeout{The content of the file a.tex is: \read{a.tex}}
这有可能吗?
答案1
逐行读取文件并将其打印在终端上。
\documentclass{article}
\ExplSyntaxOn
\NewDocumentCommand{\typeoutfile}{m}
{
\yegor_typeout_file:n { #1 }
}
\ior_new:N \g_yegor_typeout_ior
\cs_new_protected:Nn \yegor_typeout_file:n
{
\ior_open:Nn \g_yegor_typeout_ior { #1 }
\ior_str_map_inline:Nn \g_yegor_typeout_ior
{
\iow_term:n { ##1 }
}
\ior_close:N \g_yegor_typeout_ior
}
\ExplSyntaxOff
\begin{document}
\typeoutfile{\jobname.tex}
\end{document}
这是终端上打印的内容。
This is pdfTeX, Version 3.141592653-2.6-1.40.24 (TeX Live 2022) (preloaded format=pdflatex)
restricted \write18 enabled.
entering extended mode
(./typeout.tex
LaTeX2e <2022-06-01> patch level 5
L3 programming layer <2022-09-28>
(/usr/local/texlive/2022/texmf-dist/tex/latex/base/article.cls
Document Class: article 2021/10/04 v1.4n Standard LaTeX document class
(/usr/local/texlive/2022/texmf-dist/tex/latex/base/size10.clo))
(/usr/local/texlive/2022/texmf-dist/tex/latex/l3backend/l3backend-pdftex.def)
(./typeout.aux)
\documentclass{article}
\ExplSyntaxOn
\NewDocumentCommand{\typeoutfile}{m}
{
\yegor_typeout_file:n { #1 }
}
\ior_new:N \g_yegor_typeout_ior
\cs_new_protected:Nn \yegor_typeout_file:n
{
\ior_open:Nn \g_yegor_typeout_ior { #1 }
\ior_str_map_inline:Nn \g_yegor_typeout_ior
{
\iow_term:n { ##1 }
}
\ior_close:N \g_yegor_typeout_ior
}
\ExplSyntaxOff
\begin{document}
\typeoutfile{\jobname.tex}
\end{document}
(./typeout.aux) )
No pages of output.
Transcript written on typeout.log.
答案2
我将通过中间宏变量来进行。
\documentclass{article}
\usepackage{catchfile}
\begin{document}
\IfFileExists{message.txt}
{\CatchFileEdef{\FILEmessage}{message.txt}{\endlinechar=-1 }}
{\def\FILEmessage{message file not found}}
\typeout{The message file says: \FILEmessage}
\end{document}
正如上面的评论中所提到的,有几种方法可以读取文件。