I put together a macro to write the contents of an external file to the log output. Unfortunately, I always read out the trailing \par
that LaTeX adds when reading files. How do I test for this and remove it? I am interested in the "TeX way", but all answers are welcome, of course.
The example macro deals with the following conditions:
- File is empty
- File has contents
It does not cover the following condition:
- File does not exist
In my example I do the following while compiling:
- write to a file called
functions-\jobname.txt
(collects every mention of\myfunction
in body) - read the file later
functions-\jobname.txt
- use
typeout
to put the contents of this file at the end of the log output (used for collaborated content-related debugging)
Read to Log Macro
I use \typeout
to write to the log and terminal. I probably need to add some logic to my \readtolog
macro to test for the trailing \par
, so I extracted it from my working example below.
\newread\customfile\def\readtolog#1{% \openin\customfile=#1\relax \loop\unless\ifeof\file \read\customfile to\fileline \ifx\fileline\eolmarker \else \typeout{\fileline} \fi \repeat \closein\customfile}
Example Code
\listfiles% used to illustrate atveryend which does not have much to do with the question\documentclass{article}\usepackage{fontspec}% compiled with xelatex\usepackage{regexpatch}% use for \xapptocmd\usepackage{etoolbox}% use for \tracingpatches\usepackage{atveryend}% Add support for \AtEndAfterFileList\newenvironment{logmessage}[1]{% \typeout{^^J**************************************************^^J% #1% ^^J************************************************** }} {\typeout{**************************************************}}\def\myfunction#1{#1}% Macro appended to by \xapptocmd\newread\customfile\def\readtolog#1{% LOGIC NEEDED TO REMOVE TRAILING \par \openin\customfile=#1\relax \loop\unless\ifeof\customfile \read\customfile to\fileline \ifx\fileline\eolmarker \else \typeout{\fileline} \fi \repeat \closein\customfile}\def\checkfunctions{% Debug mode (enable writes and reads) \tracingpatches \newwrite\myfunctionsfile \AtBeginDocument{\immediate\openout\myfunctionsfile=functions-\jobname.txt} \AtEndDocument{\immediate\closeout\myfunctionsfile} \AtEndAfterFileList{\begin{logmessage}{Functions}\readtolog{functions-\jobname.txt}\end{logmessage}} \xapptocmd{\myfunction}{\immediate\write\myfunctionsfile{\unexpanded{##1}}}{}{}}\checkfunctions\begin{document}\myfunction{A function worth noting}\end{document}
Log Output Snippet
What is the deal with \par
? Is this related? How to trim the end of line character for each line read from an external file?
**************************************************Functions************************************************** A function worth noting \par ************************************************** )