使表格中的行动态显示的更简洁的方法

使表格中的行动态显示的更简洁的方法

因此,我所拥有的方法有效,但我正在寻找一种更清洁/更好的方法。

我有一个标题页,需要\maketitle以 的形式table(实际上是表格)显示。但是,当缺少选项时,应将其从表格中删除。同样,如果没有给出选项,则不应打印表格。

例如

\def\@author{Jane doe}
\def\@date{\today}
\def\@verifier{Joe Doe}

应该产生

在此处输入图片描述

尽管

\def\@author{Jane doe}
\def\@date{}
\def\@verifier{}

变成

在此处输入图片描述

这是我目前的想法,以尽可能简洁的例子来呈现

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

\makeatletter
    \def\@author{}
    \def\@date{}
    \def\@verifier{}
    % Comment out the lines bellow to test
    \def\@author{Jane doe}
    \def\@date{\today}
    \def\@verifier{Joe Doe}

\newcommand{\maketitletable}{
%
\newcommand*{\authorname}{author}
\newcommand*{\datename}{date}
\newcommand*{\verifiername}{verifier}
%
\ifstrempty{\@author\@date\@verifier}{}{
    \noindent
    \begin{tabular}{p{0.2\textwidth} p{0.2\textwidth} p{\dimexpr 0.6\textwidth - 4\tabcolsep}}\toprule
        \ifdefempty{\@author}{}{& \authorname{} & \@author \\}
        \ifdefempty{\@date}{}{& \datename{} & \@date \\}
        \ifdefempty{\@verifier}{}{& \verifiername{} & \@verifier \\}
      \bottomrule
    \end{tabular}%
    }
}
\makeatother

\begin{document}
    \maketitletable
\end{document}

仅供参考,在我的实际代码中,我做了类似以下的事情(我还有很多)

\usepackage{xparse}
\ExplSyntaxOn
    % Provide keys.
    \keys_define:nn { babylonia }
      {
        author         .tl_set:N  = \@author,
        author         .initial:n = {},
        date           .tl_set:N  = \@date,
        date           .initial:n = {},
        verifier       .tl_set:N  = \@verifier,
      }
    % Provide key setting command.
    \NewDocumentCommand\OUSsetup{ m }{
      \keys_set:nn { babylonia } { #1 }
    }
\ExplSyntaxOff

进而

\makeatletter 

\renewcommand{\maketitle}{% 
...
minimal working example goes in here
...
}

\makeatother

在最终设定之前

\OUSsetup{
  author    = Jane Doe,
  date      = 01.12.2021,
  verifier  = Joe Doe,
}

\begin{document}

\maketitle

\end{document}

我猜这对于表的实现方式没有什么影响。

答案1

这是一个可能的解决方案tabularray包裹:

\documentclass{article}
\usepackage{etoolbox}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\makeatletter

\def\@author{}
\def\@date{}
\def\@verifier{}

\def\authorname{author}
\def\datename{date}
\def\verifiername{verifier}

\newcommand*{\mycheckempty}[1]{\ifcsempty{@#1}{}{\csuse{#1name}\strut}}

\newcommand{\maketitletable}{%
\ifstrempty{\@author\@date\@verifier}{}{%
    \noindent
    \begin{booktabs}{
      colspec={X[2]X[2]X[6]},
      stretch=0, rowsep=0pt, column{2}={cmd=\mycheckempty},
    }
      \toprule
        & author   & \@author   \\
        & date     & \@date     \\
        & verifier & \@verifier \\
      \bottomrule
    \end{booktabs}%
    }%
}
\makeatother

\begin{document}

\makeatletter

\def\@author{Jane doe}
\def\@date{\today}
\def\@verifier{Joe Doe}
\maketitletable
  
\bigskip

\def\@author{Jane doe}
\def\@date{}
\def\@verifier{}
\maketitletable

\makeatother

\end{document}

在此处输入图片描述

答案2

最简单的解决方案(对现有代码的修改最少)是简单地定义一个辅助宏(因为问题宏必须在外部定义,并且一些其他事宜使用 TikZ \foreach)

%! TEX program = lualatex
\documentclass{article}
\usepackage{etoolbox}
\usepackage{booktabs}

\makeatletter
\def\@author{}
\def\@date{}
\def\@verifier{}
% Comment out the lines bellow to test
\def\@author{Jane doe}
\def\@date{\today}
\def\@verifier{Joe Doe}

\newcommand\printrow[1]{%  ← this is the auxiliary macro ========
    \ifcsempty{@#1}{}{  & \csuse{#1name} & \csuse{@#1} \\ }%
    %                     ↑ ↑ ↑ ↑ ↑ ↑ ↑    might be replaced with simply #1 if you're sure
}

\newcommand{\maketitletable}{
%
\newcommand*{\authorname}{author}
\newcommand*{\datename}{date}
\newcommand*{\verifiername}{verifier}
%
\ifstrempty{\@author\@date\@verifier}{}{
    \noindent
    \begin{tabular}{p{0.2\textwidth} p{0.2\textwidth} p{\dimexpr 0.6\textwidth - 4\tabcolsep}}\toprule
        \printrow{author}  % ======== use it like this
        \printrow{date}
        \printrow{verifier}
      \bottomrule
    \end{tabular}%
    }
}
\makeatother

\begin{document}
    \maketitletable
\end{document}

使用 expl3 的替代解决方案。

%! TEX program = lualatex
\documentclass{article}
\usepackage{etoolbox}
\usepackage{booktabs}

\makeatletter
\def\@author{}
\def\@date{}
\def\@verifier{}
% Comment out the lines bellow to test
\def\@author{Jane doe}
\def\@date{\today}
\def\@verifier{Joe Doe}


\ExplSyntaxOn
\prg_generate_conditional_variant:Nnn \tl_if_empty:n {x} {F}

\cs_set_protected:Npn \__process_item:n #1 {
    \tl_if_empty:cF {@#1} { & \use:c{#1name} & \use:c {@#1} \\ }
}

\newcommand{\maketitletable}{
%
\newcommand*{\authorname}{author}
\newcommand*{\datename}{date}
\newcommand*{\verifiername}{verifier}
%
\tl_if_empty:xF{\@author\@date\@verifier}{
    \noindent
    \begin{tabular}{p{0.2\textwidth} p{0.2\textwidth} p{\dimexpr 0.6\textwidth - 4\tabcolsep}}\toprule
        \clist_map_function:nN {author, date, verifier} \__process_item:n
        \bottomrule
    \end{tabular}%
    }
}
\makeatother
\ExplSyntaxOff

\begin{document}
    \maketitletable
\end{document}

请注意,由于某些低级 TeX 原因,直接使用\clist_map_inline将不起作用( )(粗略地说,在高级版本中可以工作,而不起作用,并且在和之间做了一些不可扩展的事情)Misplaced \noalign.\\ \bottomrule\\ \relax \bottomrule\clist_map_inline\\\bottomrule

相关内容