Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have a relatively complex table that looks like this:

enter image description here

The same table structure is going to be repeated so many times. I already have the tables in MS Word. My question is if there is an easy method to fill the data in the table code?

Here's the code:

\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}

I know that I shouldn't be asking two questions but this one is small: How do I make an individual cell transparent?

Edit: I made it MWE and here's another table with the same structure but different data:

enter image description here

share|improve this question
1  
Please post a complete document, i.e. an example that can be actually compiled (a MWE). –  Sašo Živanović Sep 5 '12 at 21:26
1  
The \g@addto@macro bit is wrong; remove [ht] from it as soon as possible. –  egreg Sep 5 '12 at 22:19
    
I got this bit from this site, I've no idea how it works!. Yes, there wasn't [ht] in it but adding it makes everything work. When I remove it: 1- The word [ht] comes at the top of every table (because my tables are always begin{table}[ht]. 2- The font does NOT change 3- The positioning of the float doesn't work anymore –  pythonista Sep 6 '12 at 3:05
    
This kind of table format which has both column-, and row-spanning is interesting. If my memory serves, I think @egreg once mentioned he had once used intertwined \haligns and \valigns, which could accomplish this kind of format. –  morbusg Sep 29 '12 at 17:56

1 Answer 1

up vote 15 down vote accepted

The biggest thing to figure out really is exactly how do you want to specify the data. Here I have defined a \Data macro that accepts four mandatory parameters:

  • the text of the question,
  • the label for the first group along with it's data,
  • the label for the second group along with it's data, and
  • the comparison data

To simplify things I decided to go with & separator as that is what tabular needs (see Further Enhancements below). So, for instance the first set of data is defined as:

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

and as you can see from this I defined macros for the labels \UGroup and \SGroup. Specifying the data in this manner within the custom MyTabular environment yields:

enter image description here

Notes:

  • The collcell package is used to automatically place the last three columns within a \multirow{2}{*}{<data>}.
  • To ensure that the above mentioned \multirow is only applied after the header is completed, I used newtoggle from the etoolbox package as I prefer that syntax versus the \newif syntax. But if you don't want to include an additional package it should be pretty straightforward to adapt this to use \newif or some other conditional methods
  • Defining macros \UGroup and \SGroup for labels not only makes it more readable but provides additional flexibility in case you decide to tweak it later.
  • I moved your customizations of \arraystretch and \fontsize to be within the MyTabular environment, so that the adjustments are only have an effect within MyTabular.
  • Two counters QuestionCount and QuestionSubCount are used to automate the labeling of the tables and each row of that data. You can adjust the formatting of these as desired.
  • The MyTabular environment accepts an optional (i.e., within square brackets) first parameter to override the label for each row. Defaults to Q<n> where <n> takes on increasing integer values starting at 1.

References:

Further Enhancements:

  • Perhaps instead of using & as a delimiter, a comma separated list would be preferred. However it should be noted that this is not at all as simple as it seems.

Code:

\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}
share|improve this answer
1  
Wow :D, I wish I can up vote the answer many times. Thanks a lot –  pythonista Sep 30 '12 at 23:42
    
Wow indeed. SE needs this quality of reply. –  Forkrul Assail Oct 1 '12 at 5:17
    
@ForkrulAssail and Abdulhaq Elhouderi: Thanks for the kind words. –  Peter Grill Oct 1 '12 at 5:22
1  
@AbdulhaqElhouderi: Have updated to fix a bug, prior version was not resetting one of the counters. –  Peter Grill Oct 1 '12 at 20:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.