Is it possible to create a \newcolumntype-like macro accepting more than one arguments where one of them is an optional argument?

Herbert has explained how to do mandatory arguments. Optional arguments is doable but requires some low-level hackery, auxiliary functions and is not for the faint-hearted. You can see two versions of the same approach in numprint and siunitx (the later is an adaptation of the former).

\makeatletter
\newtoks\mytoks
\mytoks\expandafter{\the\NC@list}
\newcolumntype{A}{}
\NC@list\expandafter{\the\mytoks \NC@do A}
\renewcommand*{\NC@rewrite@A}[3][]{%
  \@temptokena\expandafter{%
    \the\@temptokena 
      % INSERT DEFINITION HERE!
  }%
  \NC@find
}
\makeatother

not with an optional argument, only with mandatory ones:

\documentclass{article}
\usepackage{array} 

\newcolumntype{A}[3]{@{\vrule\kern#1\vrule}p{#2}@{\vrule\kern#3\vrule}}
\begin{document} 

\begin{tabular}{|A{2cm}{1cm}{5cm}|c|l|r|}
foo & bar & baz & foobar
\end{tabular}

\end{document}

Tags:

Macros