如何制作行数可变的表格

如何制作行数可变的表格

这是我从自定义类文件中提取的代码。

\makeatletter
\let\@tmemi\@empty\let\@tmemii\@empty
\def\tmemi#1{\gdef\@tmemi{#1}}                   
\def\tmemii#1{\gdef\@tmemii{#1}}

\newcommand{\tablex}{%
\begin{tabular}{r@{ : }ll}
Paticipant         & John Doe  &   Mr.      \\[1ex]
Consultant         & A. Prof   &   PhD      \\[1ex]
\ifx\@tmemi\empty  &           &
\else Team         & \@tmemi   & Manager\fi \\[1ex] 
\ifx\@tmemii\empty &           &
\else              & \@tmemii  & Oganizer\fi
\end{tabular}
}
\makeatother

% Case 1
\tmemi{B. Member}
\tmemii{C. Member}
\tablex

% Case 2
\tmemi{}
\tmemii{}
\tablex

我的结果和问题如下图所示(为了简化,我只显示成员参数。)

在此处输入图片描述

答案1

{r@{ : }ll}

连接:到第一列,因此要创建一个没有 的单元格,:您需要

\multicolumn{1}{r}{}&& 

因此第一个单元格使用r而不是r@{ : }


如果您想省略空白框中的整行,那么:

\documentclass{article}
\usepackage{blindtext}

\begin{document}


\makeatletter
\let\@tmemi\@empty\let\@tmemii\@empty
\def\tmemi#1{\gdef\@tmemi{#1}}                   
\def\tmemii#1{\gdef\@tmemii{#1}}

\newcommand{\tablex}{%
\begin{tabular}{r@{ : }ll}
Paticipant         & John Doe  &   Mr.      \\[1ex]
Consultant         & A. Prof   &   PhD   
\ifx\@tmemi\empty\expandafter\@gobble
\else\expandafter\@firstofone
\fi
{\\[1ex] Team         & \@tmemi   & Manager}
\ifx\@tmemii\empty\expandafter\@gobble
\else\expandafter\@firstofone
\fi
{\\[1ex]         & \@tmemii  & Oganizer}
\end{tabular}
}
\makeatother

% Case 1
\tmemi{B. Member}
\tmemii{C. Member}
\tablex

\bigskip
\hrule
\bigskip

% Case 2
\tmemi{}
\tmemii{}
\tablex

\end{document}

答案2

下面将输出进行划分,以便更好地管理各个部分:

在此处输入图片描述

\documentclass{article}

\makeatletter
\newcommand{\@printtmemi}{Team & \@tmemi  & Manager}
\newcommand{\@printtmemii}{    & \@tmemii & Organizer}
\newcommand{\@nextline}{\\[1ex]}
\def\@emptytmem{}
\let\@tmemi\@emptytmem\let\@tmemii\@emptytmem
\def\tmemi#1{\gdef\@tmemi{#1}}                   
\def\tmemii#1{\gdef\@tmemii{#1}}

\newcommand{\tablex}{%
  \begin{tabular}{r@{ : }ll}
    Paticipant             & John Doe  &   Mr.  \@nextline
    Consultant             & A.\ Prof  &   PhD      
    \setbox8=\hbox{\@tmemi}\ifdim\wd8>\z@%
      \@nextline\@printtmemi\fi
    \setbox8=\hbox{\@tmemii}\ifdim\wd8>\z@%
      \@nextline\@printtmemii\fi
  \end{tabular}
}
\makeatother

\begin{document}

% Case 1
\tmemi{B.\ Member}
\tmemii{C.\ Member}
\tablex

\bigskip

% Case 2
\tmemi{}
\tmemii{}
\tablex

\bigskip

% Case 3
\tmemi{D.\ Member}
\tmemii{}
\tablex

\end{document}

相关内容