进一步增强:

进一步增强:

我有一个相对复杂的表格,如下所示:

在此处输入图片描述

相同的表格结构会重复很多次。我已经在 MS Word 中有了表格。我的问题是,是否有一种简单的方法可以在表格代码中填充数据?

代码如下:

\documentclass[12pt]{report}

\usepackage{color}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{multirow}

%This will change the font for all the tables in the document.
\makeatletter
\g@addto@macro{\table[ht]}{\fontsize{9pt}{10.8pt}\selectfont}
\makeatother

\begin{document}
\begin{table}[ht]
\newcolumntype{Z}{>{\centering\arraybackslash}m{4.11in}}
\newcolumntype{Y}{>{\centering\arraybackslash\columncolor{Gainsboro}}c}
\renewcommand*{\arraystretch}{1.2}
\centering
\caption{Experiences in practical Physics work}
  \begin{tabular}{|c|Y|c|c|c|c|c|c|}
    \hline
               &            & \multicolumn{3}{c}{\emph{\textbf{\%}}} & \multicolumn{3}{|c|}{\emph{\textbf{Comparisons}}}  \\
    \hline  \rowcolor{Gainsboro}  {\textbf{Group}}& {\textbf{N}} & \textbf{positive} & \textbf{neutral} & \textbf{negative} & \textbf{$\chi^2$} & \textbf{df} & \textbf{p} \\
    \hline
    \rowcolor{Gainsboro}{\emph{Q1 (a)}} & \multicolumn{7}{Z|}{I prefer to have written instructions for experiments}  \\
    \hline
    \cellcolor{PowderBlue}\emph{University group} & {150} & 73         & 7          & 20         & \multirow{2}{*}{0.9} & \multirow{2}{*}{4} & \multirow{2}{*}{ns} \\
    \cline{1-5}
    \cellcolor{YellowGreen}\emph{School group} & {150} & 70         & 8          & 22         &            &            &  \\
    \hline
    \rowcolor{Gainsboro}{\emph{Q1 (b)}} & \multicolumn{7}{Z|}{Practical work helps my understanding of Physics topics}                   \\
    \hline
    \cellcolor{PowderBlue}\emph{University group} & {150} & 34         & 11         & 55         & \multirow{2}{*}{3.9} & \multirow{2}{*}{4} & \multirow{2}{*}{ns} \\
    \cline{1-5}
    \cellcolor{YellowGreen}\emph{School group} & {150} & 38         & 7          & 55         &            &            &  \\
    \hline
    \rowcolor{Gainsboro}{\emph{Q1 (c)}} & \multicolumn{7}{Z|}{Discussions in the laboratory enhance my understanding of the subject}                   \\
    \hline
    \cellcolor{PowderBlue}\emph{University group} & {150} & 70         & 8          & 22         & \multirow{2}{*}{2.4} & \multirow{2}{*}{4} & \multirow{2}{*}{ns} \\
    \cline{1-5}
    \cellcolor{YellowGreen}\emph{School group} & {150} & 65         & 10         & 25         &            &            &  \\
    \hline
    \rowcolor{Gainsboro}{\emph{Q1 (d)}} & \multicolumn{7}{Z|}{I felt confident in carrying out the experiments in Physics}                   \\
    \hline
    \cellcolor{PowderBlue}\emph{University group} & {150} & 52         & 12         & 36         & \multirow{2}{*}{13.7} & \multirow{2}{*}{4} & \multirow{2}{*}{< 0.05} \\
    \cline{1-5}
    \cellcolor{YellowGreen}\emph{School group} & {150} & 44         & 15         & 41         &            &            &  \\
    \hline
    \rowcolor{Gainsboro}{\emph{Q1 (e)}} & \multicolumn{7}{Z|}{The experimental procedure was clearly explained in the instructions given}                   \\
    \hline
    \cellcolor{PowderBlue}\emph{University group} & {150} & 31         & 14         & 55         & \multirow{2}{*}{14.8} & \multirow{2}{*}{4} & \multirow{2}{*}{< 0.01} \\
    \cline{1-5}
    \cellcolor{YellowGreen}\emph{School group} & {150} & 46         & 11         & 43         &            &            &  \\
    \hline
    \rowcolor{Gainsboro}{\emph{Q1 (f)}} & \multicolumn{7}{Z|}{I was so confused in the laboratory that I ended up following the instructions without understanding what I was doing}                   \\
    \hline
    \cellcolor{PowderBlue}\emph{University group} & {150} & 63         & 12         & 25         & \multirow{2}{*}{1.1} & \multirow{2}{*}{4} & \multirow{2}{*}{ns} \\
    \cline{1-5}
    \cellcolor{YellowGreen}\emph{School group} & {150} & 65         & 9          & 26         &            &            &  \\
    \hline
    \rowcolor{Gainsboro}{\emph{Q1 (g)}} & \multicolumn{7}{Z|}{There was good linkage between experiments and the relevant theory}                   \\
    \hline
    \cellcolor{PowderBlue}\emph{University group} & {150} & 36         & 15         & 49         & \multirow{2}{*}{94.4} & \multirow{2}{*}{4} & \multirow{2}{*}{<0.001} \\
   \cline{1-5}
    \cellcolor{YellowGreen}\emph{School group} & {150} & 68         & 11         & 21         &            &            &  \\
    \hline
    \end{tabular}
\label{ch7:tab:experiencesinpracticalphysics}
\end{table}

\end{document}

我知道我不应该问两个问题,但这个问题很小:如何使单个单元格透明?

编辑:我将其设为 MWE,这里是另一个具有相同结构但不同数据的表:

在此处输入图片描述

答案1

真正需要弄清楚的最大问题是如何是否要指定数据。这里我定义了一个\Data接受四个必需参数的宏:

  • 问题内容,
  • 第一组的标签及其数据,
  • 第二组的标签及其数据,以及
  • 对比数据

为了简化事情,我决定使用&分隔符,因为这就是tabular需要的(参见进一步增强下文)。因此,例如第一组数据定义为:

\Data{I prefer to have written instructions for experiments}
    {\UGroup & 150 & 73 & 7 & 20}
    {\SGroup & 150 & 70 & 8 & 22}
    {0.9 & 4 & \NS}

从中可以看出,我为标签\UGroup和定义了宏\SGroup。在自定义环境中以这种方式指定数据MyTabular可得出:

在此处输入图片描述

笔记:

  • 包裹collcell用于自动将最后三列放置在内\multirow{2}{*}{<data>}
  • 为了确保上述内容\multirow仅在标题完成后才应用,我使用newtoggle包裹etoolbox因为我更喜欢那个语法而不是\newif语法。但如果你不想包含额外的包,那么调整它以使用\newif其他一些条件方法
  • 定义宏\UGroup\SGroup标签不仅使其更具可读性,而且还提供了额外的灵活性,以防您决定稍后对其进行调整。
  • 我将您的自定义\arraystretch\fontsize移至MyTabular环境内,以便调整仅在内产生影响MyTabular
  • 两个计数器QuestionCountQuestionSubCount用于自动标记表格和每行数据。您可以根据需要调整这些的格式。
  • 环境MyTabular接受可选的(即在方括号内)第一个参数来覆盖每行的标签。默认为Q<n><n>1 开始增加整数值。

参考:

进一步增强:

  • 也许最好不要使用&逗号分隔列表作为分隔符。但应该注意的是,这是不是根本没有看上去那么简单。

代码:

\documentclass[12pt]{report}

\usepackage{color}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{multirow}
\usepackage{pgffor}
\usepackage{collcell}

\newtoggle{DoneWithHeader}
\newcommand{\MyMultiRow}[1]{%
    \iftoggle{DoneWithHeader}{%
        \multirow{2}{*}{#1}% no longer in header
    }{%
        #1% still in header so do not apply \multirow
    }%
}
\newcolumntype{M}{>{\collectcell\MyMultiRow}{l}<{\endcollectcell}}


\newcolumntype{Z}{>{\centering\arraybackslash}m{4.11in}}
\newcolumntype{Y}{>{\centering\arraybackslash\columncolor{Gainsboro}}c}


\newcounter{QuestionCount}
\newcommand*{\QuestionGroup}{}
\newenvironment{MyTabular}[2][Q\arabic{QuestionCount}]{%
    % #1 = Question Group: Defaults to "Q<n>"
    % #2 = usual tabular column specification
    \stepcounter{QuestionCount}% increment for subsequent tables
    \setcounter{QuestionSubCount}{0}% ensure we start at (a) for subsequent tables
    \xdef\QuestionGroup{#1}
    \renewcommand*{\arraystretch}{1.2}
    \fontsize{9pt}{10.8pt}\selectfont
    \global\togglefalse{DoneWithHeader}% not yet done with header
    \begin{tabular}{#2}
        \hline% Print header rows
        & & \multicolumn{3}{c}{\emph{\textbf{\%}}} & 
                \multicolumn{3}{|c|}{\emph{\textbf{Comparisons}}}  \\
        \hline  
        \rowcolor{Gainsboro}  {\textbf{Group}}& {\textbf{N}} & \textbf{positive} & 
               \textbf{neutral} & \textbf{negative} & \textbf{$\chi^2$} & \textbf{df} & 
               \textbf{p} \global\toggletrue{DoneWithHeader}\\
        \hline
}{%
    \end{tabular}%
}


\newcounter{QuestionSubCount}
\newcommand*{\Data}[4]{%
    % #1 = Question Text
    % #2 = Univerity group numbers
    % #3 = School Group Numbers
    % #4 = Comparrisons
    %
    \hline
    \rowcolor{Gainsboro}{%
        \stepcounter{QuestionSubCount}
        \emph{\QuestionGroup~(\alph{QuestionSubCount})}%
    } &
    \multicolumn{7}{Z|}{#1}  \\
    \hline
    #2 & #4\\
    \cline{1-5}
    #3 &&&\\\hline
}%

\newcommand{\UGroup}{{\cellcolor{PowderBlue}\emph{University group}}}%
\newcommand{\SGroup}{{\cellcolor{YellowGreen}\emph{School group}}}%

\newcommand{\NS}{ns}%

\begin{document}
\begin{MyTabular}{|c|Y|c|c|c|M|M|M|}
    \Data{I prefer to have written instructions for experiments}
        {\UGroup & 150 & 73 & 7 & 20}
        {\SGroup & 150 & 70 & 8 & 22}
        {0.9 & 4 & \NS}
    \Data{Practical work helps my understanding of Physics topics}
        {\UGroup&150&34&11&55}
        {\SGroup&150&38&7&55}
        {3.9&4&\NS}
    \Data{Discussions in the laboratory enhance my understanding of the subject}
        {\UGroup&150&70&8&22}
        {\SGroup&150&65&10&25}
        {2.4&4&\NS}
    \Data{I felt confident in carrying out the experiments in Physics}
        {\UGroup&150&52&12&36}
        {\SGroup&150&44&15&41}
        {13.7&4&\NS}
    \Data{The experimental procedure was clearly explained in the instructions given}
        {\UGroup&150&31&14&55}
        {\SGroup&150&46&11&43}
        {14.8&4&\NS}
    \Data{I was so confused in the laboratory that I ended up following the instructions without understanding what I was doing}
        {\UGroup&150&63&12&25}
        {\SGroup&150&65&9&26}
        {1.1&4&\NS}
    \Data{There was good linkage between experiments and the relevant theory}
        {\UGroup&150&36&15&49}
        {\SGroup&150&68&11&21}
        {94.4&4&$<0.001$}
\end{MyTabular}

\bigskip\bigskip

\begin{MyTabular}{|c|Y|c|c|c|M|M|M|}
    \Data{Is practucal work useful}
        {\UGroup&150&41&31&28}
        {\SGroup&150&41&32&27}
        {14.8&4&\NS}
    \Data{Is practucal work helpful}
        {\UGroup&150&45&25&30}
        {\SGroup&150&47&23&30}
        {14.8&4&\NS}
\end{MyTabular}
\end{document}

相关内容