In the code below \Foo
evaluates inline code. \Baz
is a 'journal-ed' version, meaning it writes calls to \Foo
to a file. Reading the file fails (Hook 2) due to a "'macro parameter character #' in horizontal mode." issue. With the transformation ##1
->#1
(Hook 3) the file can be read and produces the desired output. Is there a way to solve this problem within LaTeX?
PS: How to use xparse
's 's' parameter? (Hook 1)
\documentclass{l3doc}
\usepackage{xparse}
\ExplSyntaxOn
\iow_new:N \__erw_write_stream
\AtEndDocument{\iow_close:N \__erw_write_stream}
\iow_open:Nn \__erw_write_stream{erwlog}
\cs_new_protected:Nn \__erw_write:n
{
\iow_now:Nn \__erw_write_stream{#1}
}
\NewDocumentCommand{\Foo}
{m}
{
\cs_gset_protected:Nn \__erw_foo:n {#1}
\__erw_foo:n{World}
}
\NewDocumentCommand{\Baz} % (journaled version of \Foo)
{
% s m % Hook 1.a
o m
}
{
\Foo{#2}
\IfValueT{#1}
{
(*)
% \__erw_write:n{\Foo{#2}} % Hook 2.a
\__erw_write:n{\Baz{#2}} % Hook 2.b
}
}
\NewDocumentCommand{\Qux} % recover calls to \Foo
{}
{
\file_input:n{erwlog}
}
\NewDocumentCommand\Test
{}
{
% \Baz*{Hello,~##1!} % Hook 1.b % Always (*)
\Baz[]{Hello,~##1!} % OK if Hook 2.c commented out
\iow_close:N \__erw_write_stream
% \Qux % Hook 2.c
%ERROR: You can't use `macro parameter character #' in horizontal mode.
%
%--- TeX said ---
%
%\__erw_foo:n #1->Hello, ##
% 1!
%l.1 \Baz {Hello, ##1!}
%
%--- HELP ---
%The special character # has appeared in ordinary text. You probably
%meant to type \#.
% erwlogbis: % Hook 3.a
% \Baz {Hello, #1!}
\file_input:n{erwlogbis}
}
\ExplSyntaxOff
\begin{document}
\Test
\end{document}