通用表定义

通用表定义

根据主题如何在表规范中使用命令,并且仍然支持数组包?我已经尝试强化创建表格和定义的方式。

\documentclass[fontsize=11pt, % Document font size
                             paper=a4, % Document paper type
                             twoside=false, % Shifts odd pages to the left for easier reading when printed, can be changed to oneside
                             captions=tableheading,
                             index=totoc,
                             % paper=landscape,
                             hyperref]{scrreprt}

\usepackage[T1]{fontenc} % Use 8-bit encoding that has 256 glyphs
\usepackage[utf8]{inputenc} % Uses the utf8 input encoding

\usepackage{graphicx} % Required for including images
\usepackage{booktabs,array,ragged2e,multirow}
    \newcolumntype{M}[1]{>{\hspace{0pt}\raggedright\arraybackslash}m{#1}}
\usepackage{xcolor}
\usepackage{longtable} % can be commented out, approach is an very generic method of tablespec
\usepackage{tabu}   % can be commented out, approach is an very generic method of tablespec                      
\usepackage{xspace}

\begin{document}

\providecommand{\hlttextacs}{3} 
\providecommand{\hlttextbcs}{4}
\providecommand{\tablecolumndira}{M} 
\providecommand{\tablecolumndirb}{M} 

\providecommand{\commandatablecolumnaligna}{M}
\providecommand{\commandatablecolumndesignaa}{!{\color{white}}
\providecommand{\commandatablecolumndesignba}{\vrule width 0.2em}}
\providecommand{\commandatablecolumnalignb}{M}
\providecommand{\commandatablecolumndesignab}{!{\color{white}}
\providecommand{\commandatablecolumndesignbb}{\vrule width 0.2em}}

\def\mytablespec{\commandatablecolumnaligna\commandatablecolumndesignaa\commandatablecolumndesignba\commandatablecolumnalignb\commandatablecolumndesignab\commandatablecolumndesignbb}
\def\mytabletype{longtable}

\expandafter\csname\expandafter\mytabletype\expandafter\endcsname\expandafter{\mytablespec}
1 & 2 \\\hline
3 & 4 \\
\csname end\mytabletype\endcsname

\end{document}

它应该是在命令期间制定表格规范的更通用的方法。

也许现在更加清楚采用什么方法了。

更新:

我定义了一些命令,例如

\callmytable{A,B,C}{3,4,5}{M,M,M}

目前它将产生 3,4,5(每个)的命令并且我的表的标题是......

M{\hlttextacs cm}!{\color{white}\vrule width 0.2em}M{\hlttextbcs cm}!{\color{white}\vrule width 0.2em}M{\hlttextccs cm}

使用文档中的此命令,我可以操作我的表格,我将决定我的表格有多少列,但不是从文档的开头开始。应在此命令期间进行定义。使用 datatool,我的表格加载 csv 文件,有时我想在表 a 上进行另一种对齐,有时在表 b 上进行另一种对齐。

目前,我为每个 csv 文件结构都有一个单独的命令。

\twocolumnstable....
\threecolumstable....

如果我可以处理这个表格规范,那么将这些命令减少为一个的方法就非常接近了。

\myowntablecommand{A,B,C}{2,3,4}{M,M,M}{pathtocsvfile}....

得到漂亮的表格。

答案1

评论有点长:

你的例子太长了,包含的问题太多了。关于你链接的帖子,我不明白为什么所有这些\expandafter都被使用。我只会使用\newcolumntype

\documentclass{article}
\usepackage{array}


\newcolumntype\specA{l}
\newcolumntype\specB{l}
\newcolumntype\mytablespec{\specA|\specB}

\def\mytabletype{tabular}

\begin{document}
\begin{\mytabletype}{\mytablespec}
1 & 2 \\\hline
3 & 4 \\
\end{\mytabletype}
\end{document}

请注意,中的命令\newcolumntype只是用于保存定义的“标记”,它们不会被执行。

答案2

您可以在宏中隐藏令人讨厌的细节。请注意,您对和的定义\commandatablecolumndesignaa\ commandatablecolumndesignab错误的:您不能{在定义主体中留下未完成的内容,并且您忘记了列的参数M

\documentclass[
  fontsize=11pt, % Document font size
  paper=a4, % Document paper type
  twoside=false, % Shifts odd pages to the left for easier reading when printed, can be changed to oneside
  captions=tableheading,
  index=totoc,
  % paper=landscape,
  hyperref
]{scrreprt}

\usepackage[T1]{fontenc} % Use 8-bit encoding that has 256 glyphs
\usepackage[utf8]{inputenc} % Uses the utf8 input encoding

\usepackage{graphicx} % Required for including images
\usepackage{booktabs,array,ragged2e,multirow}
\usepackage{xcolor}
\usepackage{longtable} % can be commented out, approach is an very generic method of tablespec
\usepackage{tabu}   % can be commented out, approach is an very generic method of tablespec
\usepackage{xspace}

\newcolumntype{M}[1]{>{\hspace{0pt}\raggedright\arraybackslash}m{#1}}

\makeatletter
\newcommand{\settablespec}[2]{%
  \toks@={}%
  \@tfor\next:=#2\do{%
    \toks@=\expandafter{\the\expandafter\expandafter\expandafter\toks@\next}% \next must be expanded twice
  }%
  \edef#1{\the\toks@}%
}
\newcommand{\tablebegin}[2]{%
  \def\next{\begin{#1}}%
  \expandafter\next\expandafter{#2}%
}
\makeatother
\begin{document}

\providecommand{\hlttextacs}{3} 
\providecommand{\hlttextbcs}{4}
\providecommand{\tablecolumndira}{M} 
\providecommand{\tablecolumndirb}{M} 

\providecommand{\commandatablecolumnaligna}{M{4cm}}
\providecommand{\commandatablecolumndesigna}{!{\color{white}\vrule width 0.2em}}
\providecommand{\commandatablecolumnalignb}{M{3cm}}
\providecommand{\commandatablecolumndesignb}{!{\color{white}\vrule width 0.2em}}

\settablespec{\mytablespec}{%
  \commandatablecolumnaligna
  \commandatablecolumndesigna
  \commandatablecolumnalignb
  \commandatablecolumndesignb
}
\def\mytabletype{longtable}

\tablebegin{\mytabletype}{\mytablespec}
1 & 2 \\\hline
3 & 4 \\
\end{\mytabletype}

\end{document}

在此处输入图片描述

相关内容