带条件行的表格

带条件行的表格

我正在尝试根据命令的值制作一个包含条件行的表格。问题是我收到以下错误:

Misplaced alignment tab character &. \TableFunctionConf

以下是我想做的事情:

\documentclass[
  10pt,
  paper=a4,
  twoside=false,
  openany,
  ]{scrbook}

\usepackage{tabularray}

\newcommand{\SignalName}{My name}
\newcommand{\SignalA}{1} 
\newcommand{\SignalB}{1} 
\newcommand{\SignalC}{1}   
\newcommand{\SignalD}{1}  

\newcommand{\TableFunctionConf}
{
  \begin{tblr}
    {
      colspec   = cX,
      hlines,
      vlines,
      width     = {0.8\textwidth},
      measure   = vbox,
      row{1}    = {font=\bfseries, bg=brown9}
    }
    Function config.          & Description \\
    Not Used                  & The \SignalName\ will not be used in this application        \\
    \ifnum\SignalA  = 1 {Type A & Bla bla AAAA  \\  } \fi
    \ifnum\SignalB  = 1 {Type B & Bla bla BBBB  \\  } \fi
    \ifnum\SignalC  = 1 {Type C & Bla bla CCCC  \\  } \fi
    \ifnum\SignalD  = 1 {Type D & Bla bla DDDD  \\  } \fi
  \end{tblr}
}

\begin{document}
  bla bla bla
  \TableFunctionConf
\end{document}

有人知道如何解决这个问题吗?谢谢,


编辑:我按照@Cicada 的建议尝试了以下操作,但仍然收到相同的错误消息(当然希望我正确理解了该评论)。

\documentclass[
  10pt,
  paper=a4,
  twoside=false,
  openany,
  ]{scrbook}

\usepackage{tabularray}

\newcommand{\SignalName}{My name}
\newcommand{\RowA}{Type A & Bla bla AAAA  \\ }

\newcommand{\TableFunctionConf}
{
  \begin{tblr}
    {
      colspec   = cX,
      hlines,
      vlines,
      width     = {0.8\textwidth},
      measure   = vbox,
      row{1}    = {font=\bfseries, bg=brown9}
    }
    Function config.          & Description \\
    Not Used                  & The \SignalName\ will not be used in this application        \\
    \RowA
  \end{tblr}
}

\begin{document}
  bla bla bla
  \TableFunctionConf
\end{document}

所以我猜测错误不在于条件本身,而在于使用一行内容的命令......

答案1

由于tabularray读取环境内容的方式tblr,您必须已经解决了\ifnum测试并拥有&支撑组的外部。

下面使用\expanded来扩展内容,然后才能完成其工作。请注意,您必须使用或tblr来保护您不想扩展的宏。\noexpand\<macro>\unexpanded{<stuff>}

\documentclass{article}

\usepackage{xcolor}
\usepackage{tabularray}
\UseTblrLibrary{varwidth}

\newcommand{\SignalName}{My name}
\newcommand{\SignalA}{0} 
\newcommand{\SignalB}{1} 
\newcommand{\SignalC}{1}   
\newcommand{\SignalD}{1}  
\newcommand{\RowA}{Type A & Bla bla AAAA  \\ }

\newcommand\userow[2]{#2\unexpanded{#1}}
\newcommand{\TableFunctionConf}
  {%
    \expanded
      {%
        \noexpand\begin{tblr}
          {
            colspec   = cX,
            hlines,
            vlines,
            width     = {0.8\noexpand\textwidth},
            measure   = vbox,
            row{1}    = {font=\noexpand\bfseries, bg=brown9}
          }
          Function config. & Description \noexpand\\
          Not Used         & The \noexpand\SignalName\noexpand\ will not be used
                             in this application \noexpand\\
          \ifnum\SignalA=1 \userow{Type A & Bla bla AAAA\\}\fi
          \ifnum\SignalB=1 \userow{Type B & Bla bla BBBB\\}\fi
          \ifnum\SignalC=1 \userow{Type C & Bla bla CCCC\\}\fi
          \ifnum\SignalD=1 \userow{Type D & Bla bla DDDD\\}\fi
        \noexpand\end{tblr}%
      }%
  }

\begin{document}
  bla bla bla
  \TableFunctionConf
\end{document}

在此处输入图片描述

答案2

可以在多个地方进行过滤。

这里,在构建表之前对传入的数据流(将成为行数据)进行过滤。

内容和形式

expl3 序列用于存储一组逗号分隔的记录(行)。然后迭代该序列以识别分号分隔的字段(列)。条件过滤将记录结构中的占位符设置为空({}),或设置为有效的记录格式。分隔符是任意选择的。

colspec=为了方便更改列规格,将环境值设为tblr参数。

它看起来像是tabularray使用了一些临时 expl3 变量,因此为这个 MWE 定义了一个独立集。

平均能量损失

\documentclass{article}
\usepackage{xcolor}
\usepackage{tabularray}
\newcommand{\SignalName}{My name}


\ExplSyntaxOn


        \cs_generate_variant:Nn 
                    \seq_set_split:Nnn 
                { cnx }
        \cs_generate_variant:Nn 
                    \seq_gset_split:Nnn 
                { cnx }


\seq_new:N \g_kr_tmpa_seq
\seq_new:N \l_kr_tmpb_seq
\tl_new:N \l_kr_tmpa_tl
\tl_new:N \l_kr_tmpb_tl
\tl_new:N \l_kr_tmpc_tl
\tl_new:N \g_kr_tmpd_tl
\int_new:N \l_kr_numrows_int

\NewDocumentCommand { \tableload } { +m } { 
    \seq_gclear:c
            { g_kr_tmpa_seq } 
    \seq_gset_split:cnx
            { g_kr_tmpa_seq } 
            { , } 
            { #1 }
%   \seq_show:c
%           { g_kr_tmpa_seq } 
}


\NewDocumentCommand { \tablebuild } { } { 

    \int_set:Nn 
            \l_kr_numrows_int 
            { 
                    \seq_count:N \g_kr_tmpa_seq 
            }

  % table contents holder
    \tl_gclear:N \g_kr_tmpd_tl 
    
% for each row
        \int_step_variable:nnnNn 
                { 1 } 
                { 1 } 
                { \l_kr_numrows_int } 
                \l_kr_tmpa_tl %row number
                {
                        \seq_set_split:cnx
                             { l_kr_tmpb_seq }
                            { ; } % separator
                            { \seq_item:Nn \g_kr_tmpa_seq { \l_kr_tmpa_tl } }

% for each column
                            \int_step_variable:nnnNn 
                                    { 1 } 
                                    { 1 } 
                                    { \seq_count:N \l_kr_tmpb_seq } 
                                    \l_kr_tmpb_tl %column number
                                    {
                                            \tl_set:Nx 
                                                    \l_kr_tmpc_tl 
                                { 
                                                            \seq_item:Nn 
                                                                        \l_kr_tmpb_seq 
                                                                        { \l_kr_tmpb_tl } 
                                   }
                                   
                % cell contents:                   
                \tl_gput_right:NV \g_kr_tmpd_tl \l_kr_tmpc_tl 
                
                                            \int_compare:nNnT 
                                                    { \l_kr_tmpb_tl } 
                                                    < 
                                                    { \seq_count:N \l_kr_tmpb_seq }
                    { \tl_gput_right:Nn \g_kr_tmpd_tl { & } }
                    
                             } %end column code
                             
                                            \int_compare:nNnT 
                                                    { \l_kr_tmpa_tl } 
                                                    < 
                                                    { \seq_count:N \g_kr_tmpa_seq }
                    { \tl_gput_right:Nn \g_kr_tmpd_tl { \\ } }
                    
            } % end row code

%   \tl_show:N \g_kr_tmpd_tl 

}

\NewDocumentCommand \myTable { O{Q[c]X[-1]} } %1=colspec
  {
        \tablebuild

     \tl_set:Nx \l_my_tabular_tl { \g_kr_tmpd_tl }

    \use:x
      {
         \tablea[#1]{ \l_my_tabular_tl }
      }

  }


\NewDocumentCommand\tablea { O{Q[c]X[-1]} m } {
  
  \begin{tblr}
    {
      width     = {0.8\textwidth},
      colspec   = { #1 },
      hlines,
      vlines,
      measure   = vbox,
      row{1}    = {font=\bfseries, bg=brown9},
    }
        #2
        \end{tblr}
}



\ExplSyntaxOff


\newcommand{\comma}{,}
\newcommand{\SignalA}{1}
\newcommand{\signalaph}{}

\newcommand{\checkSignalA}{\ifnum\SignalA=1 \renewcommand{\signalaph}{\comma Type A; Bla bla AAAA}\else\renewcommand{\signalaph}{}\fi}

\newcommand\loadsignaltable{\tableload{%
    Function config. ; Description 
,   Not Used  ; The \SignalName\ will not be used in this application
,x;y
,abc;def
\signalaph
}
}          


\begin{document}
  bla bla bla

\checkSignalA
\loadsignaltable
SignalA on.

\myTable[rr]

abc


\bigskip
\renewcommand{\SignalA}{0}
\checkSignalA
\loadsignaltable
Repeat: same contents; different colspec. SignalA off.

\myTable

ooo

\bigskip
Different contents; different colspec.

\tableload{%
Gnus;Gnats;Letter
,Legs-4;Legs-6;A
,Tail-1;Tail-0;B
,Hide-leather;Hide-exoskeleton;C
,Joints-internal;Joints-external;D
,Albedo-dull;Albedo-bright;E
}
\myTable[Q[l]Q[l]Q[c,blue!8]]

xyz

zse

xdr

cft
\end{document}

答案3

感谢@samcarter_is_at_topanswers.xyz,我找到了解决方案。

也感谢所有评论和提出的解决方案。

我创建了一个包含表格内容的临时文件,然后使用 tabularray 函数将文件内容作为表格数据包含在内。结果如下:

\documentclass[
  10pt,
  paper=a4,
  twoside=false,
  openany,
  ]{scrbook}

\usepackage{tabularray}

\newcommand{\SignalName}{My name}
\newcommand{\SignalA}{1} 
\newcommand{\SignalB}{1} 
\newcommand{\SignalC}{1}   
\newcommand{\SignalD}{1}  

\newcommand{\TableFunctionConf}
{
  \begin{tblr}[evaluate=\fileInput]
    {
      colspec   = cX,
      hlines,
      vlines,
      width     = {0.8\textwidth},
      measure   = vbox,
      row{1}    = {font=\bfseries, bg=brown9}
    }
    Function config.          & Description \\
    Not Used                  & The \SignalName\ will not be used in this application        \\
          \fileInput{buffer.tmp}
  \end{tblr}
}

\begin{document}
  bla bla bla

  \newwrite\tempfile
  \immediate\openout\tempfile=buffer.tmp

  \ifnum\SignalA = 1 {\immediate\write\tempfile{Type A & Bla bla AAAA \unexpanded{\\}}} \fi
  \ifnum\SignalB = 1 {\immediate\write\tempfile{Type B & Bla bla BBBB \unexpanded{\\}}} \fi
  \ifnum\SignalC = 1 {\immediate\write\tempfile{Type C & Bla bla CCCC \unexpanded{\\}}} \fi
  \ifnum\SignalD = 1 {\immediate\write\tempfile{Type D & Bla bla DDDD \unexpanded{\\}}} \fi

  \immediate\closeout\tempfile

  \TableFunctionConf
\end{document}

相关内容