3

I place the same table multiple times in my main document. For that, the content of the table is placed in a file called model-values.tex. On every \input, I alter the part of the table to be displayed by hiding columns with a \newcolumntype. I use this solution because I work with a huge Gnumeric table (TeX exporting) and I don’t want to split it only for formatting reasons.

Hiding does not work anymore if I have \multicolumns in the table. The text of these is still displayed, no matter if the columns are hidden. I have already tried different solutions for the H type column (see TeX SE link in the comments) but the problem persists.

One solution: take the header out of the file and put it into the several occurences of the table. However, if I add or drop columns in model-values.tex then, the whole head of the table needs to be changed. This is 2 more rows to be changed vs. only 1 'row' (= column definition) when the header is included in the external file.

Is it worth it or should I stuck with changing-the-whole-head solution? I think the problem is because of the column type c in the \multicolumn. It should 'inherit' the table’s one or something.

\documentclass{scrartcl}
\usepackage{booktabs}
\usepackage{tabularx}

% Hidden column type - different solutions on http://tex.stackexchange.com/questions/16604/easiest-way-to-delete-a-column
\newcolumntype{H}{>{\setbox0=\hbox\bgroup}c<{\egroup}@{}}

\begin{filecontents}{model-values.tex}
    % Header
        Model
        & Param a
        & \multicolumn{3}{c}{Param set A}
        & \multicolumn{2}{c}{Param set B}
    \\\cmidrule{3-5}\cmidrule{6-7}
        &
        & Param A1
        & Param A2
        & Param A3
        & Param B1
        & Param B2
    \\\midrule
    % Data
        1 & 5 & 6 & 9 & 11 & 2 & 4\\
\end{filecontents}

\begin{document}

\section{Comparison of param set A}
Here, param set A of all models are compared, refer the table.

\begin{table}[t]
\begin{tabular}{
    l % Model
    c % Param a
    c % Param set A: A1
    c % Param set A: A2
    c % Param set A: A3
    H % Param set B: B1
    H % Param set B: B2
    }
\toprule 
\input{model-values.tex}
\bottomrule
\end{tabular}
\end{table}

\clearpage
\section{Comparison of param set B}
Here, param set B of all models are compared, refer the table.

\begin{table}[t]
\begin{tabular}{
    l % Model
    c % Param a
    H % Param set A: A1
    H % Param set A: A2
    H % Param set A: A3
    c % Param set B: B1
    c % Param set B: B2
    }
\toprule 
\input{model-values.tex}
\bottomrule
\end{tabular}
\end{table}

\end{document}
1
  • 2
    Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. Well done first question, I hope you'll get the answer soon :)
    – yo'
    Oct 3 '14 at 16:52
2

As multicolumn in generally spans more than one column the idea that it should inherit "the" column type doesn't make much sense. And for one column it is explicitly meant to overwrite the existing column type.

You can hide a multicolumn with the help of some dummy columns and switches. But it needs some fiddling to get the spaces between columns and the lines correctly. And one should be careful to reset the booleans to false at the start:

\documentclass{scrartcl}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{filecontents}
% Hidden column type - different solutions on http://tex.stackexchange.com/questions/16604/easiest-way-to-delete-a-column
\newcolumntype{H}{@{}>{\setbox0=\hbox\bgroup}c<{\egroup}@{}}
\newif\ifhidden
\begin{filecontents}{model-values.tex}
    % Header
        Model
        & Param a
        & \ifhidden\multicolumn{3}{@{}c@{}}{}\else\multicolumn{3}{c}{Param set A}\fi
        &
        & \ifhidden\multicolumn{2}{@{}c@{}}{}\else\multicolumn{2}{c}{Param set B}\fi
    \\\cmidrule{3-5}\cmidrule{7-8}
        &
        & Param A1
        & Param A2
        & Param A3
        &
        & Param B1
        & Param B2
    \\\midrule
    % Data
        1 & 5 & 6 & 9 & 11 && 2 & 4\\
        1 & 5 & 6 & 9 & 11 && 2 & 4\\
\end{filecontents}

\begin{document}

\section{Comparison of param set A}
Here, param set A of all models are compared, refer the table.

\begin{table}[t]
\begin{tabular}{
    l<{\global\hiddenfalse} % Model
    c % Param a
    c % Param set A: A1
    c % Param set A: A2
    c % Param set A: A3
    @{}l<{\global\hiddentrue}%dummy column to hold the switch
    H % Param set B: B1
    H % Param set B: B2
    }
\toprule
\input{model-values.tex}
\bottomrule
\end{tabular}
\end{table}

\section{Comparison of param set B}
Here, param set B of all models are compared, refer the table.

\begin{table}[t]
\begin{tabular}{
    l <{\global\hiddenfalse} % Model% Model
    c <{\global\hiddentrue}% Param a
    H % Param set A: A1
    H % Param set A: A2
    H % Param set A: A3
    @{}l <{\global\hiddenfalse}
    c % Param set B: B1
    c % Param set B: B2
    }
\toprule
\input{model-values.tex}
\bottomrule
\end{tabular}
\end{table}

\end{document}
4
  • If all param B’s should be hidden, it seems that it does not work. Have you tried it?
    – unndreay
    Oct 3 '14 at 19:45
  • Sorry I seem to have copied yesterday only a part of the example. Also I forgot to reset the switches to false in the next tabular. (One has to use \global as tabular cells are groups, but this naturally means that one tabular can affect the next.) Oct 4 '14 at 8:40
  • Is it possible to include toggling the hidden switch in the newcolumntype definition? \newcolumntype{H}{@{}>{\global\hiddentrue\setbox0=\hbox\bgroup}c<{\egroup\global\hiddenfalse}@{}} does not work. Any ideas?
    – unndreay
    Oct 4 '14 at 10:21
  • No. multicolumn replaces the H columntype, so it is not seen. The switches must be in columns not affected by multicolumn (that's why I added a dummy column between the two multicolumns). But you can naturally define more columntypes. Oct 4 '14 at 10:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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