7

Package array has a \newcolumntype but seemingly no \renewcolumntype. But it makes a lot of sense to have a column type X whose meaning could be changed depending on context. When one does \newcolumntype again, it works, the only thing is that an innocent warning is written to the log. Is there some official way I missed from reading the doc which would allow of genuinely renewing an array-declared column type?

lockstep
  • 250,273
  • Related: http://tex.stackexchange.com/questions/35527/how-to-check-if-a-column-type-is-defined – egreg Nov 11 '13 at 17:06
  • The intention was you'd just use \newcolumntype is it that you want to suppress the warning? – David Carlisle Nov 11 '13 at 17:27
  • @DavidCarlisle suppressing the warning would be just fine indeed. The idea is that the user employs some access macro which is converted by the (would-be) package into the use of \newcolumntype, where the identifier is one and the same, private to the package, and to not worry the user, suppressing the warning would be just fine. –  Nov 11 '13 at 17:39
  • There is a silence package that can turn off \PackageWarning in general or (I think) for specific packages – David Carlisle Nov 11 '13 at 17:40
  • or just use some command like \let\NC@find@X\relax to undefine the column before defining it. – David Carlisle Nov 11 '13 at 17:42
  • @DavidCarlisle ok, thanks. I got into this from my code on matrices. The \MATdisplay macro sets up a simple *{N}c array preamble, where N is the variable number of columns, and I wanted to make it easy for the user to replace c by r for example. So currently the type is called X and renewed with the resulting warning. –  Nov 11 '13 at 17:44
  • @DavidCarlisle this will be ok with the \NC@list ? –  Nov 11 '13 at 17:44
  • If the change is occasional, you might want to use a 1-column multicolumn instead of redefinitions: \begin{tabular}{|l|l|l|} column1 & column2 & column 3\\hline A & B & \multicolumn{1}{|r|}{C}\\end{tabular} – masu Nov 11 '13 at 17:45
  • @masu thanks, but the contents are not to be modified, only the preamble. –  Nov 11 '13 at 17:47
  • @DavidCarlisle here is the current code I use \newcolumntype\MATdisplaycoltype {c} \newcolumntype\MATdisplaypreamble [1]{@{}*{#1[J]}\MATdisplaycoltype@{}}. I use the possibility of having a column type stand for an entire preamble, and furthermore to have a parameter (the #1[J] will expand to a number of columns). The user should be able to easily switch \MATdisplaycoltype from c to r. But redefining it issues the warning. –  Nov 11 '13 at 17:51
  • Using command names for columns rather than letters sort of works by accident but was never the intention:-) Just patch \newcolumntype to undefine \csname NC@find@\string#1\endcsname as noted above. You ask if it is OK to do that, just because I wrote that code a quarter of a century ago doesn't necessarily mean I've forgotten how it works:-) – David Carlisle Nov 11 '13 at 17:56
  • @DavidCarlisle I am in awe because I quickly forget code I wrote only two weeks earlier :) and also because you write code which works in extended contexts! I will undefine the corresponding \NC@find@\stuff, thanks. –  Nov 11 '13 at 18:04

1 Answers1

11

Elaborating on David's idea:

\newcommand\undefcolumntype[1]{\expandafter\let\csname NC@find@#1\endcsname\relax}
\newcommand\forcenewcolumntype[1]{\undefcolumntype{#1}\newcolumntype{#1}}

I don't call it \renewcolumntype since it works even if the column type is not defined (which is a different behaviour from \renewcommand). Whence the name \forcenewcolumntype.

Thanks to jfbu for spotting out a crucial typo in the code.

yo'
  • 51,322