使用 \typeout 和 \meaning 与 \midrule 时出现错误

使用 \typeout 和 \meaning 与 \midrule 时出现错误

为了方便调试一些实验代码,我创建了一个\typeout将其内容分成多行的版本:

%% dummy macro for simulating newlines in output to terminal
\def\my@linefeed{}
\def\mytypeout#1\my@linefeed#2\@nil{%%
  \typeout{\detokenize{#1}}%%
  % \typeout{\meaning#1}%%
  \expandafter\ifx\expandafter\relax\detokenize{#2}\relax\else
    \mytypeout#2\@nil
  \fi}

我会按如下方式使用它:

\mytypeout ==> A testing mytypeout on its own\my@linefeed
           ==> B testing mytypeout on its own\my@linefeed
           ==> C testing mytypeout on its own\my@linefeed
           ==> D testing mytypeout on its own\my@linefeed
           ==> E testing mytypeout on its own\my@linefeed
           ==> F testing mytypeout on its own\my@linefeed\@nil

我的\mytypeout{...}似乎工作正常,直到我开始将它与一起使用booktabs

以下MWE编译:

\documentclass{article}
\usepackage{booktabs}
\usepackage{etoolbox}
\usepackage{pgffor}

\makeatletter
%%\let\my@linefeed\relax%% --> raises errors!
%% dummy macro for simulating newlines in output to terminal
\def\my@linefeed{}
\def\mytypeout#1\my@linefeed#2\@nil{%%
  \typeout{\detokenize{#1}}%%
  % \typeout{\meaning#1}%%
  \expandafter\ifx\expandafter\relax\detokenize{#2}\relax\else
    \mytypeout#2\@nil
  \fi}

%% `\space`s added to make `\typeout` more readable
\def\my@row@of@table{%%
  First column matter \space 
  & 
  \expandonce\x \space 
  \noexpand\\
  %% Neither \meaning nor \detokenize raise an error
  %% if this next line is commented out
  % \noexpand\midrule
  \noexpand\my@linefeed
  }

\def\full@agenda{}
\def\build@agenda{%%
  \foreach \x in \list@of@agenda@items {%%
    \xdef\full@agenda{%%
      \expandonce\full@agenda
      \my@row@of@table}%%
    \expandafter\mytypeout\full@agenda\my@linefeed\@nil
    \typeout{---DONE----}%%
  }}

\def\setagenda#1{%%
  \bgroup
    \def\list@of@agenda@items{#1}%%
    \build@agenda
    \begin{tabular}{cl}
      \toprule
      \full@agenda          
      \bottomrule
    \end{tabular}%%
  \egroup
  \def\full@agenda{}}

\makeatother
\begin{document}

\setagenda{
    {A Line of random content},
    {B Not too involved math: $x\cdot y$ },
    {C Some more random text.},
    {D Random },
    {E Hello world},
    {F no more}
    }

\end{document}

如果我取消注释该行

  % \noexpand\midrule

然后为了让上面的 MWE 编译,我必须改变 的定义\mytypeout。也就是说,我必须改变

\typeout{\meaning#1}

\typeout{\detokenize{#1}}

我不太明白这里发生了什么。我尝试通过测试以下代码来找出错误

%% No problems with `\typeout` + `\meaning` in either of the next two examples:
%% example #1
\def\aetemp{\midrule}
\typeout{TEMP(A):\meaning\aetemp}
%% example #2
\def\aetemp{\noexpand\midrule}
\typeout{TEMP(B):\meaning\aetemp}

但我没有发现任何错误。

这里发生了什么?我错过了什么?

答案1

如果我在之前添加\showtokens{#1}的定义,那么对于第一次调用,我得到\mytypeout\typeout

> First column matter  & A Line of random content \\ .
\mytypeout ...@linefeed #2\@nil ->\showtokens {#1}
                                                  \typeout {\detokenize {#1}...
l.62     }

这显然意味着\meaning#1完全不合适,因为它只会作用于F,展开如下

The character F

所以\typeout{\meaning#1}这肯定不是你想要的。你使用\meaning,但喜欢

  \begingroup\def\next{#1}%
  \typeout{\expandafter\strip@prefix\meaning\next}%%
  \endgroup

这将给出输出

First column matter  & A Line of random content \\ 
First column matter  & B Not too involved math: $x\cdot y$  \\ 
First column matter  & C Some more random text. \\ 
First column matter  & D Random  \\ 
First column matter  & E Hello world \\ 
First column matter  & {F no more}  \\ 

这当然与您从该\detokenize版本中获得的没有什么不同。

相关内容