1

I'm trying to do something like this (\read is a pseudo command):

\typeout{The content of the file a.tex is: \read{a.tex}}

Is it at all possible?

1

2 Answers 2

3

Read the file line by line and print it on the terminal.

\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 what's printed on the terminal.

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.
0

I would go via an intermediate macro variable.

\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}

As mentioned in the comment above are there several ways to read in a file.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .