I have a document with some complex tables, defined by a command called \Rule
with named parameters (using the keyval
package).
The individual rows of the table are first stored into a token list defined via \newtoks
because some of the named parameters correspond to multiple cells of the table and simultaneously must be enveloped inside an \ifcsname
condition.
I'd like to force \Rule
to store a fully expanded copy of the resulting table (with the particular values for each named parameter) so that I can list all rules that appeared anywhere in the document in the appendix.
But I was not able to force the token list to fully expand.
I compile with xetex
and cannot change that.
EDIT:
Each table corresponds to a single call of \Rule
; the named parameters contain the information, and the definition of \Rule
provides the formatting for each kind of named parameter (most are optional).
I want exactly the same tables in main text and in the appendix: the output should be the same as if I manually copy-pasted all \Rule
commands from the document into the appendix.
\documentclass{book}
\usepackage{xparse,keyval}
\newcounter{rule}
\renewcommand{\therule}[0]{G\arabic{rule}}
\makeatletter
\newtoks\@tabtoks
\newcommand\addtabtoks[1]{\@tabtoks\expandafter{\the\@tabtoks#1}}
\newcommand*\resettabtoks{\@tabtoks{}}
\newcommand*\printtabtoks{\the\@tabtoks}
\edef\allrules{}
\edef\oldallrules{\allrules}
\newcommand*\addtoallrules[1]{\g@addto@macro{\allrules}{#1}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\define@key{myKeys}{nonnumbered}{\def\mm@nonnumbered{#1}}
\define@key{myKeys}{label}{\def\mm@label{#1}}
\define@key{myKeys}{header}{\def\mm@header{#1}}
\define@key{myKeys}{FUNKTORY}{\def\mm@FUNKTORY{#1}}
%% one \ifcsname cannot directly generate more than a single table cell (problems with & and \\ characters)
%% so instead of spitting out the rows of the table immediately,
%% the content is added to @tabtoks via the command \addtabtoks
%% and printed when all parameters are read
\DeclareDocumentCommand{\Rule}{m}{
\begingroup%
\setkeys{myKeys}{#1}%reads the key=value pairs
\medskip\noindent
\begin{table}
\addtabtoks{\begin{tabular}{|p{5cm}|l|}
\hline}
\ifcsname mm@nonnumbered\endcsname
\addtabtoks{\multicolumn{2}{|l|}{{\bf\mm@header}}\\}
\else \refstepcounter{rule}
\addtabtoks{\multicolumn{2}{|l|}{{\bf\mm@header}\hfill Rule \therule} \\}
\fi
\addtabtoks{\hline}
\ifcsname mm@FUNKTORY\endcsname \addtabtoks{\mm@FUNKTORY \\}\fi
\ifcsname mm@OBLIG\endcsname \addtabtoks{Obligatory & \mm@OBLIG \\}\fi
\addtabtoks{\hline
\end{tabular}}
\printtabtoks
% %%%%%%%% BEFORE RESETTING \@tabtoks, I'D LIKE TO STORE ITS FULLY EXPANDED VALUE
% \expandafter\long\expandafter\edef\expandafter\tmp{\expandafter\write\expandafter\the\@tabtoks}
% \show\tmp
% \expandafter\addtoallrules\expandafter{\tmp}
% \show\allrules
\resettabtoks
\ifcsname mm@label\endcsname \label{\mm@label}\fi
\end{table}
\smallskip
\endgroup%
}
\makeatother
\begin{document}
\Rule{
nonnumbered = {yes},
header = {Rules in general},
FUNKTORY = {VALUE 1 & VALUE 2},
}
\Rule{
header = {Rule 1},
FUNKTORY = {X & Y},
label = {rule:1},
}
\appendix
\allrules
\end{document}
\begin{tabular}
or\bf
(which should never be used in latex) do not work by expansion so can not be completely expanded in an edef in any meaningful way. What is the actual top level requirement that you want to implement? – David Carlisle Jul 1 '19 at 7:06table
creates a float, why do you put\medskip\noindent
before\begin{table}
and\smallskip
after\end{table}
? Are you sure you wanttable
environments? Without any caption? Do you want the labels to point to the rules in the main text or in the appendix? – frougon Jul 1 '19 at 8:47