Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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


I want to create a column type A that can accept more than 1 argument such that I can create a tabular as follows:

\begin{tabular}{|A[2cm]{1cm}{5cm}|c|l|r|}
...
\end{tabular}

Note: the column type A accepts 3 arguments in this example.

share|improve this question
    
Perhaps a bit more detail on what is wanted might be useful! I'm not sure if it is the macro \newcolumntype itself or the resulting letter that you want to alter. –  Joseph Wright Dec 30 '10 at 18:51
    
@Joseph, please see my new edit. –  xport Dec 30 '10 at 19:00
add comment

2 Answers 2

up vote 11 down vote accepted

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
share|improve this answer
    
Thanks. I works. –  xport Dec 30 '10 at 19:44
    
Is there a reason for using \csname NC@rewrite@A\endcsname (and the \expandafters) rather than \NC@rewrite@A? –  cgnieder Jan 26 '13 at 21:28
    
@cgnieder Not here, but there is where I've copied it from! I'll edit. –  Joseph Wright Jan 26 '13 at 21:59
add comment

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}
share|improve this answer
    
first of all thanks. However, if I define an optional argument as follow \newcolumntype{A}[3][1cm] and \begin{tabular}{|A[2cm]{1cm}{5cm}|c|l|r|}, I got compilation errors. –  xport Dec 30 '10 at 19:12
3  
I wrote "not" with an optional argument! –  Herbert Dec 30 '10 at 19:18
    
ok. I have not read before. Thanks. –  xport Dec 30 '10 at 19:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.