用于排版困难表格的宏

用于排版困难表格的宏

下表是使用以下 LaTeX 代码 (mwe) 创建的:

桌子

\documentclass{article}
\usepackage{tabu}
\usepackage{multirow}
\begin{document}
\newtabulinestyle{stip=on 1.5pt off 2pt}
\begin{tabu} to \linewidth {X[0.8l]X[2.5l]X[c]X[c]X[c]X[c]X[c]} \hline
\rowfont{\bfseries} \multicolumn{2}{c}{\multirow{2}{*} {NUMBERS}} &
\multicolumn{2}{c}{Type} & \multicolumn{2}{c}{Size} &
 \multirow{2}{*}{All} \\
 & & A & B & Big & Small & \\ \hline
\multicolumn{2}{l}{Good}  & 1 & 4 & 2 & 3 & 5\\ \tabucline[stip]{-}
 \multirow{3}{*}{Ugly} & Cow & 7  & 1 & 4 & 4 & 8 \\ \tabucline[stip]{2-}
 & Goat & 1 & 2 & 0 & 3 & 3 \\ \tabucline[stip]{2-}
 & Horse & 2& 1 & 3 & 0 & 3 \\ \tabucline[stip]{-}
\multicolumn{2}{l}{Bad}& 12 & 1 & 6 & 7 & 13  \\ \hline
 \rowfont{\bfseries} \multicolumn{2}{l}{TOTAL} & 23 & 9 & 15 & 17 & 32 \\ \hline
\end{tabu}
\end{document}

是否可以创建一个宏,根据以下输入来制作这样的表格?

 \begin{mytable}[NUMBERS]
 1 & 4 & 2 & 3 & 5 \\ 
 7 & 1 & 4 & 4 & 8 \\
 1 & 2 & 0 & 3 & 3 \\
 2 & 1 & 3 & 0 & 3 \\
 12 & 1 & 6 & 7 & 13 \\
 23 & 9 & 15 & 17 & 32
 \end{mytable}

我已经使用 pgfplotstable 有一段时间了,但是多行尤其让我头疼。

(理想情况下,甚至会自动计算总数,如果数字不匹配,则会返回错误。(A + B = 大 + 小)。但这是奢侈的,而不是必要的。)

答案1

更新后的答案显示了两个版本,一个是没有计算和检查的简单版本,另一个是进行计算的版本。

在此处输入图片描述

\documentclass{article}
\usepackage{tabu}
\usepackage{multirow}
\begin{document}
\newtabulinestyle{stip=on 1.5pt off 2pt}
\begin{tabu} to \linewidth {X[0.8l]X[2.5l]X[c]X[c]X[c]X[c]X[c]} \hline
\rowfont{\bfseries} \multicolumn{2}{c}{\multirow{2}{*} {NUMBERS}} &
\multicolumn{2}{c}{Type} & \multicolumn{2}{c}{Size} &
 \multirow{2}{*}{All} \\
 & & A & B & Big & Small & \\ \hline
\multicolumn{2}{l}{Good}  & 1 & 4 & 2 & 3 & 5\\ \tabucline[stip]{-}
 \multirow{3}{*}{Ugly} & Cow & 7  & 1 & 4 & 4 & 8 \\ \tabucline[stip]{2-}
 & Goat & 1 & 2 & 0 & 3 & 3 \\ \tabucline[stip]{2-}
 & Horse & 2& 1 & 3 & 0 & 3 \\ \tabucline[stip]{-}
\multicolumn{2}{l}{Bad}& 12 & 1 & 6 & 7 & 13  \\ \hline
 \rowfont{\bfseries} \multicolumn{2}{l}{TOTAL} & 23 & 9 & 15 & 17 & 32 \\ \hline
\end{tabu}


\def\mytable[#1]#2\\#3\\#4\\#5\\#6\\#7\end#8{%
\begin{tabu} to \linewidth {X[0.8l]X[2.5l]X[c]X[c]X[c]X[c]X[c]} \hline
\rowfont{\bfseries} \multicolumn{2}{c}{\multirow{2}{*} {#1}} &
\multicolumn{2}{c}{Type} & \multicolumn{2}{c}{Size} &
 \multirow{2}{*}{All} \\
 & & A & B & Big & Small & \\ \hline
\multicolumn{2}{l}{Good}  &#2\\\tabucline[stip]{-}
 \multirow{3}{*}{Ugly} & Cow & #3 \\ \tabucline[stip]{2-}
 & Goat & #4\\ \tabucline[stip]{2-}
 & Horse & #5 \\ \tabucline[stip]{-}
\multicolumn{2}{l}{Bad}& #6 \\ \hline
 \rowfont{\bfseries} \multicolumn{2}{l}{TOTAL} & #7 \\ \hline
\end{tabu}%
\end{mytable}}

 \begin{mytable}[NUMBERS]
 1 & 4 & 2 & 3 & 5 \\ 
 7 & 1 & 4 & 4 & 8 \\
 1 & 2 & 0 & 3 & 3 \\
 2 & 1 & 3 & 0 & 3 \\
 12 & 1 & 6 & 7 & 13 \\
 23 & 9 & 15 & 17 & 32
 \end{mytable}


\def\testrowone#1&#2&#3&#4&#5\\{%
\ifnum\numexpr#1+#2\relax=#5\else \typeout{A+B != All}\fi
\ifnum\numexpr#3+#4\relax=#5\else \typeout{big+small != All}\fi
}

\def\cella#1&#2&#3&#4&#5\\{#1}
\def\cellb#1&#2&#3&#4&#5\\{#2}
\def\cellc#1&#2&#3&#4&#5\\{#3}
\def\celld#1&#2&#3&#4&#5\\{#4}
\def\celle#1&#2&#3&#4&#5\\{#5}

\def\mytableb[#1]#2\\#3\\#4\\#5\\#6\\#7\end#8{%
\testrowone#2\\
\begin{tabu} to \linewidth {X[0.8l]X[2.5l]X[c]X[c]X[c]X[c]X[c]} \hline
\rowfont{\bfseries} \multicolumn{2}{c}{\multirow{2}{*} {#1}} &
\multicolumn{2}{c}{Type} & \multicolumn{2}{c}{Size} &
 \multirow{2}{*}{All} \\
 & & A & B & Big & Small & \\ \hline
\multicolumn{2}{l}{Good}  &#2\\\tabucline[stip]{-}
 \multirow{3}{*}{Ugly} & Cow & #3 \\ \tabucline[stip]{2-}
 & Goat & #4\\ \tabucline[stip]{2-}
 & Horse & #5 \\ \tabucline[stip]{-}
\multicolumn{2}{l}{Bad}& #6 \\ \hline
 \rowfont{\bfseries} \multicolumn{2}{l}{TOTAL} &
\the\numexpr \cella#2\\+\cella#3\\+\cella#4\\+\cella#5\\+\cella#6\\ \relax &
\the\numexpr \cellb#2\\+\cellb#3\\+\cellb#4\\+\cellb#5\\+\cellb#6\\ \relax &
\the\numexpr \cellc#2\\+\cellc#3\\+\cellc#4\\+\cellc#5\\+\cellc#6\\ \relax &
\the\numexpr \celld#2\\+\celld#3\\+\celld#4\\+\celld#5\\+\celld#6\\ \relax &
\the\numexpr \celle#2\\+\celle#3\\+\celle#4\\+\celle#5\\+\celle#6\\ \relax 
\end{tabu}%
\end{mytableb}}

 \begin{mytableb}[NUMBERS]
 1 & 4 & 2 & 3 & 5 \\ 
 7 & 1 & 4 & 4 & 8 \\
 1 & 2 & 0 & 3 & 3 \\
 2 & 1 & 3 & 0 & 3 \\
 12 & 1 & 6 & 7 & 13 \\
 \end{mytableb}


\end{document}

答案2

半生不熟pgfplotstable解决方案。缺少列总和,但对行总和进行了完整性检查(如果存在差异,则在屏幕截图中给出愚蠢的错误)。此外,左上角的文本是可选的,您可以在输入表格时更改它。这样,您就不需要以表格格式输入数据了。

\documentclass{article}
\usepackage{pgfplotstable,multirow,booktabs}

\pgfplotstableset{toughtable/.style={
    every head row/.style={before row={\toprule
      \multicolumn{2}{c}{\multirow{2}{*}{\textbf{#1}}} &\multicolumn{2}{c}{Type} &\multicolumn{2}{c}{Size} &\multirow{2}{*}{All}\\
                           },
                                   after row=\midrule
                               },
    every last row/.style={after row=\bottomrule},
        create on use/myfirstcol/.style ={create col/set list={Good,Ugly,,,Bad,\textbf{TOTAL}}},%A dummy column
        create on use/mysecondcol/.style={create col/set list={,Cow,Goat,Horse,}},%A dummy column
        create on use/summationcol/.style={
                  create col/expr={\thisrowno{0}+\thisrowno{1}-\thisrowno{2}-\thisrowno{3}==0?\thisrowno{0}+\thisrowno{1}:0}
                            },
        columns={myfirstcol,mysecondcol,[index]0,[index]1,[index]2,[index]3,summationcol},
        columns/myfirstcol/.style={string type,column name=,
            assign cell content/.code={%
            \ifnum\pgfplotstablerow=1\pgfkeyssetvalue{/pgfplots/table/@cell content}{\multirow{3}{*}{Ugly}}\fi
            }
        },
        columns/mysecondcol/.style={string type,column name=},
        columns/summationcol/.style={string type,column name=,
        assign cell content/.code={
            \pgfmathparse{\pgfkeysvalueof{/pgfplots/table/@preprocessed cell content}==0?int(1):int(0)}
                \ifnum\pgfmathresult=1\relax
                \pgfkeyssetvalue{/pgfplots/table/@cell content}{Chicken Run!!}%
                \else
                \pgfmathparse{int(\pgfkeysvalueof{/pgfplots/table/@unprocessed cell content})}
                \edef\temp{\noexpand\pgfkeyssetvalue{/pgfplots/table/@cell content}{\pgfmathresult}}\temp
                \fi
                }
      },
        display columns/2/.style={column name=A},
        display columns/3/.style={column name=B},
        display columns/4/.style={column name=Big},
        display columns/5/.style={column name=Small},
        every row no 1/.style={before row=\hline,after row=\cmidrule{2-7}},
        every row no 2/.style={after row=\cmidrule{2-7}},
        every row no 3/.style={after row=\hline},
        every row no 4/.style={after row=\hline},
        }
}
\begin{document}

\pgfplotstabletypeset[toughtable=NUMBERS]{
 1   4  2   3 
 7   1  3   4
 1   2  0   3
 2   1  3   0
 12  1  6   7
}

\end{document}

在此处输入图片描述

我希望很快就能得到列总数。

相关内容