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

Is there any way to create conditional complex column types (or column types with more arguments)?

4

Your Z-column definition is malformed or incomplete. Since it seems like you only want to condition on whether the user supplies left, center or right, here's how you can achieve that:

enter image description here

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

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.