生成表格行的命令或环境

生成表格行的命令或环境

我正在使用 hyperref 包创建一个 PDF 表单,该包具有调查格式。经过查看,我将调查声明分组到表格中,如下所示:

    \begin{Form}
      \begin{tabularx}{\textwidth}{Xcccc}
        \toprule
        \large \textbf{Sozialverhalten} & A & B & C & D \\
        \midrule
        This is the first survey statement & 
          \rule[6pt]{0pt}{6pt}\hbox{\CheckBox[print,name=2, width=0.7em, height=0.7em]{}} &
          \rule[6pt]{0pt}{6pt}\hbox{\CheckBox[print,name=2, width=0.7em, height=0.7em]{}} &
          \rule[6pt]{0pt}{6pt}\hbox{\CheckBox[print,name=2, width=0.7em, height=0.7em]{}} &
          \rule[6pt]{0pt}{6pt}\hbox{\CheckBox[print,name=2, width=0.7em, height=0.7em]{}} \\
        \midrule
        This is the second survey statement &
          \rule[6pt]{0pt}{6pt}\hbox{\CheckBox[print,name=2, width=0.7em, height=0.7em]{}} &
          \rule[6pt]{0pt}{6pt}\hbox{\CheckBox[print,name=2, width=0.7em, height=0.7em]{}} &
          \rule[6pt]{0pt}{6pt}\hbox{\CheckBox[print,name=2, width=0.7em, height=0.7em]{}} &
          \rule[6pt]{0pt}{6pt}\hbox{\CheckBox[print,name=2, width=0.7em, height=0.7em]{}} \\
        \midrule
                  [ ... ]
    \end{Form}

我想对表格的每一行使用一个宏/命令,它将第一列的文本作为参数,并自动生成每行相同的复选框(第 2-4 列)。或者编写一个可以生成复选框的命令。我该怎么做?我需要新命令还是环境?

答案1

您只需将必要的位放入新命令中:

\documentclass{article}
\usepackage{booktabs,tabularx}
\usepackage{hyperref}
%\usepackage{showframe}

\newcounter{row}

\newcommand{\makerow}[1]{%
 % #1 = text field
 #1 &
 \stepcounter{row}%
 \mbox{\CheckBox[print,name=A\therow, width=0.7em, height=0.7em]{}} &
 \mbox{\CheckBox[print,name=B\therow, width=0.7em, height=0.7em]{}} &
 \mbox{\CheckBox[print,name=C\therow, width=0.7em, height=0.7em]{}} &
 \mbox{\CheckBox[print,name=D\therow, width=0.7em, height=0.7em]{}} \\
}

\begin{document}

\begin{Form}
\noindent
\begin{tabularx}{\textwidth}{Xcccc}
\toprule
\textbf{Sozialverhalten} & A & B & C & D \\
\midrule
\makerow{This is the first survey statement}
\midrule
\makerow{This is the second survey statement}
\midrule
\makerow{This is the third survey statement, quite long to produce a line break}
\bottomrule
\end{tabularx}
\end{Form}

\end{document}

在此处输入图片描述

答案2

我通过使用and来避免使用tabularxor的问题。您可以使用on a ,但我这样做不太舒服。tabular\parbox\makebox\settowidth\CheckBox

有趣的是,没有了,每次我把a 放大,\hbox它都会变大。另外,如果你把 a 放在a 里面,你会发现它不在区域的中心。\Checkbox\mystrut\Checkbox\fbox

\documentclass{article}
\usepackage{hyperref}
%\usepackage{showframe}

\newcounter{row}
\newlength{\Xwidth}
\newlength{\Bwidth}

\newcommand{\makerow}[1]% #1 = textfield
{\hrule\stepcounter{row}%
  \mystrut\hspace{2\tabcolsep}%
  \parbox[t]{\Xwidth}{#1}\hfil
  \hbox{\CheckBox[name=A\therow, width=0.7em, height=0.7em]{}}\hfil
  \hbox{\CheckBox[name=B\therow, width=0.7em, height=0.7em]{}}\hfil
  \hbox{\CheckBox[name=C\therow, width=0.7em, height=0.7em]{}}\hfil
  \hbox{\CheckBox[name=D\therow, width=0.7em, height=0.7em]{}}%
\par}

\begin{document}
\begin{Form}
\parindent=0pt
\large% changes size of em
\setlength{\Bwidth}{\dimexpr 0.7em+4pt}% width of \CheckBox
\setlength{\Xwidth}{\dimexpr \textwidth-12\tabcolsep-4\Bwidth}% width of parbox
\def\mystrut{\rule{0pt}{\dimexpr 0.7em + \tabcolsep}\rule[-\tabcolsep]{0pt}{0pt}}%

\hrule height1pt
\mystrut\hspace{2\tabcolsep}%
 \parbox[t]{\Xwidth}{\textbf{Sozialverhalten}}\hspace{2\tabcolsep}%
 \makebox[\Bwidth][r]{A}\hspace{2\tabcolsep}%
 \makebox[\Bwidth][r]{B}\hspace{2\tabcolsep}%
 \makebox[\Bwidth][r]{C}\hspace{2\tabcolsep}%
 \makebox[\Bwidth][r]{D}\hspace*{2\tabcolsep}%

\makerow{This is the first survey statement}
\makerow{This is the second survey statement}
\hrule height1pt
\end{Form}
\end{document}

演示

相关内容