Column types for tables can be defined and overwritten with \newcolumntype
.
I would like to know if a column type is already defined for an example which shall only be executed in the case where the columntype is defined.
EDIT:
Similar to the answer of egreg I had also created this code:
\newcommand\isColumntypeDefined[1]{%
\providebool{\tpl@coltype@#1}
\ifcsdef{NC@find@\string#1}{
\setboolean{\tpl@coltype@#1}{true}%
}{%
\setboolean{\tpl@coltype@#1}{false}
}%
\boolean{\tpl@coltype@#1}%
}
but that fails because \tpl@coltype@#1
does not create a valid command. This problem is related to latex @temps(wa) commands.
It would be great to know how to make this code compileable.
EDIT2: I changed the whole code to etoolbox definitions and finally ended with this
% Create list of predefined columntypes
\expandafter\let\csname columntype@l\endcsname\@empty
\expandafter\let\csname columntype@c\endcsname\@empty
\expandafter\let\csname columntype@r\endcsname\@empty
\expandafter\let\csname columntype@p\endcsname\@empty
\expandafter\let\csname columntype@m\endcsname\@empty
\expandafter\let\csname columntype@b\endcsname\@empty
\newcommand\CheckIfColumntypeDefined[1]{%
\providebool{tpl@coltype@#1}
\ifcsdef{NC@find@\string#1}%
{\setbool{tpl@coltype@#1}{true}}%
{\ifcsdef{columntype@\string#1}
{\setbool{tpl@coltype@#1}{true}}%
{\setbool{tpl@coltype@#1}{false}}%
}%
}
\newcommand\isColumntypeDefined[1]{tpl@coltype@#1}
\newcommand\IfColumntypeDefined[3]{%
\CheckIfColumntypeDefined{#1}
\ifboolexpr{ bool{\isColumntypeDefined{#1}} }{#2}{#3}%
}
Best Answer
The instruction
defines the command
\NC@find@N
. You can check it withA
\safenewcolumntype
command can be defined byIf the column type is already defined we put before the rest of the token list the command
\@gobbleoptandone
that will gobble the optional argument and the mandatory one:\safenewcolumntype{l}[1]{...}
would becomeand all is good.
Otherwise we put
\newcolumntype{#1}
that will perform its duty. Of course it would be better to patch the original\newcolumntype
command.Patching the original definition
With the code between
\makeatletter
and\makeatother
, we redefine the\newcolumntype
command in such a way that it checks whether the column type is already defined or not and, in the former case, it disallows redefining the column type. One could define also a\renewcolumntype
command, but it doesn't seem this important.When
\newcolumntype{x}
is scanned, the routine sets the scratch conditional to false and checks ifx
is among the primitive column types and, if it is, issues an error message and sets the temporary conditional to true. Then it checks whetherx
is among the "derived" column types, that is, defined via\newcolumntype
itself. If it is, the scratch conditional is again set to false. Finally, as in the code presented before, the proper action is taken: if the conditional is false the following part of the code for\newcolumntype
is gobbled, otherwise the definition is performed.