隱藏\多列的

隱藏\多列的

我在主文檔中多次放置同一個表格。為此,表的內容被放置在名為model-values.tex.在每一個上\input,我透過隱藏帶有 的列來更改要顯示的表格部分\newcolumntype。我使用這個解決方案是因為我使用一個巨大的 Gnumeric 表(TeX 匯出),並且我不想僅出於格式原因而拆分它。

\multicolumn如果表中有 s,則隱藏不再起作用。無論列是否隱藏,這些文字仍然顯示。我已經為 H 類型列嘗試了不同的解決方案(請參閱評論中的 TeX SE 連結),但問題仍然存在。

一種解決方案:從文件中取出標頭並將其放入表的多次出現中。但是,如果我在其中新增或刪除列model-values.tex,則需要更改表的整個頭部。當標題包含在外部文件中時,與僅 1 行(= 列定義)相比,這又需要更改 2 行。

值得還是我應該堅持改變整個頭部的解決方案?我認為問題是c因為\multicolumn.它應該“繼承”表的一個或某物。

\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

由於多列通常跨越多個列,因此它應該繼承“該”列類型的想法沒有多大意義。對於一列,它明確意味著覆蓋現有的列類型。

您可以藉助一些虛擬列和開關來隱藏多列。但它需要一些擺弄才能正確獲得列和行之間的空間。一開始就應該小心地將布林值重設為 false:

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

相關內容