Quantcast
Channel: How can I remove trailing \par when reading a file and writing the output to the log? - TeX - LaTeX Stack Exchange
Viewing all articles
Browse latest Browse all 3

Answer by egreg for How can I remove trailing \par when reading a file and writing the output to the log?

$
0
0

A simple solution is to use catchfile:

\listfiles\documentclass{article}\usepackage{regexpatch}% use for \xapptocmd\usepackage{atveryend}% Add support for \AtEndAfterFileList\usepackage{catchfile}\newenvironment{logmessage}[1]{%  \typeout{^^J**************************************************^^J%  #1%  ^^J************************************************** }}  {\typeout{**************************************************}}\def\myfunction#1{#1}% Macro appended to by \xapptocmd\long\def\readtolog#1{%  \IfFileExists{#1}{%    \CatchFileDef\tologfile{#1}{\endlinechar=`\^^J}%    \typeout{\unexpanded\expandafter{\tologfile}}%  }{\typeout{No file #1}}%}\newwrite\myfunctionsfile\def\checkfunctions{% Debug mode (enable writes and reads)  \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} -- line \the\inputlineno}}    {}{}%}\checkfunctions\begin{document}\myfunction{A function worth noting}\myfunction{A function worth noting again}\end{document}

This produces a blank line before the closing asterisks, but I can't see it as a real problem.

 *File List* article.cls    2014/09/29 v1.4h Standard LaTeX document class  size10.clo    2014/09/29 v1.4h Standard LaTeX file (size option)regexpatch.sty    2015/05/20 v0.2a Extending etoolbox patching commands   expl3.sty    2016/03/28 v6468 L3 programming layer (loader) expl3-code.tex    2016/03/28 v6468 L3 programming layer l3pdfmode.def    2016/03/26 v6465 L3 Experimental driver: PDF mode  xparse.sty    2016/03/28 v6468 L3 Experimental document command parser l3regex.sty    2016/03/26 v6466 L3 Experimental regular expressionsl3tl-build.sty    2016/03/26 v6466 L3 Experimental token list constructionl3tl-analysis.sty    2016/03/24 v6443 L3 Experimental token lists analysis  l3flag.sty    2016/03/26 v6466 L3 Experimental flagsl3str-convert.sty    2016/03/24 v6443 L3 Experimental string encoding conversionsatveryend.sty    2011/06/30 v1.8 Hooks at the very end of document (HO)catchfile.sty    2011/03/01 v1.6 Catch the contents of a file (HO)infwarerr.sty    2010/04/08 v1.3 Providing info/warning/error messages (HO) ltxcmds.sty    2011/11/09 v1.22 LaTeX kernel commands for general use (HO)etexcmds.sty    2011/02/16 v1.5 Avoid name clashes with e-TeX commands (HO)ifluatex.sty    2010/03/01 v1.3 Provides the ifluatex switch (HO) *************************************************************Functions************************************************** (./functions-readtolog.txt)A function worth noting -- line 38A function worth noting again -- line 39**************************************************

A very simple routine for reading a file line by line without the final empty line problem is obtained with expl3. Just redefine your \readtolog macro in the following way:

\ExplSyntaxOn\ior_new:N \g_macmadness_logfile_stream\cs_new_protected:Npn \readtolog #1 {  \file_if_exist:nTF { #1 }   {    \macmadness_logfile_typeout:n { #1 }   }   {    \typeout{No~file~#1}   } }\cs_new_protected:Nn \macmadness_logfile_typeout:n {  \ior_open:Nn \g_macmadness_logfile_stream { #1 }  \ior_map_inline:Nn \g_macmadness_logfile_stream   {    \typeout{ \exp_not:n { ##1 } }   } }\ExplSyntaxOff

Full example:

\listfiles\documentclass{article}\usepackage{regexpatch}% use for \xapptocmd; already loads expl3\usepackage{atveryend}% Add support for \AtEndAfterFileList\ExplSyntaxOn\ior_new:N \g_macmadness_logfile_stream\cs_new_protected:Npn \readtolog #1 {  \file_if_exist:nTF { #1 }   {    \macmadness_logfile_typeout:n { #1 }   }   {    \typeout{No~file~#1}   } }\cs_new_protected:Nn \macmadness_logfile_typeout:n {  \ior_open:Nn \g_macmadness_logfile_stream { #1 }  \ior_map_inline:Nn \g_macmadness_logfile_stream   {    \typeout{ \exp_not:n { ##1 } }   } }\ExplSyntaxOff\newenvironment{logmessage}[1]{%  \typeout{^^J**************************************************^^J%  #1%  ^^J************************************************** }}  {\typeout{**************************************************}}\def\myfunction#1{#1}% Macro appended to by \xapptocmd\newwrite\myfunctionsfile\def\checkfunctions{% Debug mode (enable writes and reads)  \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} -- line \the\inputlineno}}    {}{}%}\checkfunctions\begin{document}\myfunction{A function worth noting}\myfunction{A function worth noting again}\end{document}

Output:

 *File List* article.cls    2014/09/29 v1.4h Standard LaTeX document class  size10.clo    2014/09/29 v1.4h Standard LaTeX file (size option)regexpatch.sty    2015/05/20 v0.2a Extending etoolbox patching commands   expl3.sty    2016/03/28 v6468 L3 programming layer (loader) expl3-code.tex    2016/03/28 v6468 L3 programming layer l3pdfmode.def    2016/03/26 v6465 L3 Experimental driver: PDF mode  xparse.sty    2016/03/28 v6468 L3 Experimental document command parser l3regex.sty    2016/03/26 v6466 L3 Experimental regular expressionsl3tl-build.sty    2016/03/26 v6466 L3 Experimental token list constructionl3tl-analysis.sty    2016/03/24 v6443 L3 Experimental token lists analysis  l3flag.sty    2016/03/26 v6466 L3 Experimental flagsl3str-convert.sty    2016/03/24 v6443 L3 Experimental string encoding conversionsatveryend.sty    2011/06/30 v1.8 Hooks at the very end of document (HO) *************************************************************Functions************************************************** A function worth noting -- line 52 A function worth noting again -- line 53 **************************************************

Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>