\documentclass{article}
\usepackage{array}
\usepackage{ragged2e}
% \newcolumntype{M}[1]{>{\hspace{0pt}\raggedright\arraybackslash}m{#1}}
% \newcolumntype{B}[1]{>{\hspace{0pt}\centering\arraybackslash}m{#1}}
\newcolumntype{L}[1]{>{\hspace{0pt}\raggedright\arraybackslash}p{#1}} % linksbündig mit Breitenangabe
\newcolumntype{C}[1]{>{\hspace{0pt}\centering\arraybackslash}p{#1}} % zentriert mit Breitenangabe
\newcolumntype{R}[1]{>{\hspace{0pt}\raggedleft\arraybackslash}p{#1}} % rechtsbündig mit Breitenangabe
% non working columntype pseudocode
\newcolumntype{Z}[5]{%
\ifthenelse{\equal{#2}{left}{!{\vrule width #4}>{\columncolor{#3}\raggedright\arraybackslash}p{#1}{\vrule width #5}}%
{\ifthenelse{\equal{#2}{center}{!{\vrule width #4}>{\columncolor{#3}\centering\arraybackslash}p{#1}{\vrule width #5}}%
{!{\vrule width #4}>{\columncolor{#3}\raggedleft\arraybackslash}p{#1}{\vrule width #5}}%
}%
\begin{document}
% desired results
\begin{tabular}{|L{2cm}|C{2cm}|R{2cm}}
first row & A & CC\\
second row& B& CA
\end{tabular}
% pseudocode
\begin{tabular}{|Z{2cm}{left}{white}{0}{0}|Z{2cm}{center}{white}{0}{0}|Z{2cm}{right}{white}{0}{0}|}
first row & A & CC\\
second row& B& CA
\end{tabular}
\end{document}
有没有办法创建条件复杂列类型(或具有更多参数的列类型)?
答案1
您的Z
-column 定义格式不正确或不完整。由于您似乎只想判断用户是否提供了left
或center
,right
因此您可以按照以下方法实现此目的:
\documentclass{article}
\usepackage{array,colortbl}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1}}
\newcolumntype{Z}[5]{%
!{\vrule width #4}
>{\ifnum\pdfstrcmp{#2}{left}=0 \raggedright\arraybackslash\fi
\ifnum\pdfstrcmp{#2}{center}=0 \centering\arraybackslash\fi
\ifnum\pdfstrcmp{#2}{right}=0 \raggedleft\arraybackslash\fi
\columncolor{#3}}
p{#1}
!{\vrule width #5}
}
\begin{document}
\begin{tabular}{|L{2cm}|C{2cm}|R{2cm}}
first row & A & CC \\
second row & B & CA
\end{tabular}
\bigskip
\begin{tabular}{|Z{2cm}{left}{red}{2pt}{2pt}|Z{2cm}{center}{blue}{1pt}{1pt}|Z{2cm}{right}{green}{0pt}{0pt}|}
first row & A & CC \\
second row & B & CA
\end{tabular}
\end{document}