# Convert macro with one argument to switch

With the package array, I want to define a new column type for my tables with the command

\newcolumntype{U}{>{\switchon}{l}<{\switchoff}}


However, the command I’d like to use is not a switch but a plain, unary macro like \emph or \textsc, so I must define \switchon such that

{\switchon some text}


will be equivalent to

\macro{some text}


(if it must be, I could live with \switchon some text \switchoff but I think this is not necessary.)

Converting a switch to a macro (e.g. \em to \emph) is pretty simple but how would I do it the other way round?

Edit

It seems, I also have to fight this problem which does not seem to allow for an explicit \switchoff (because I need to support the X column type of tabularx and it does not seem to work well together with newcolumntype):

\renewcommand{\tabularxcolumn}[1]{>{\theswitch}m{#1}}


Where \theswitch must be some (sensible) combination of \lowercase and other font macros. \lowercase has no ‘switch’ version (I think) which makes things a bit complicated.

-
Was exactly is a switch to you? I only know the term from if-switches. –  Martin Scharrer Mar 28 '11 at 9:50
They use that name in the l2-tabu manual. (They also use the term global macro.) I guess it means a macro whose effect is valid until the next \endgroup. –  Debilski Mar 28 '11 at 10:11
Ok, I see. But this macros normally don't have a \switchoff version, which got me confused. Note that all font macros have "switch" versions as well. –  Martin Scharrer Mar 28 '11 at 10:23
Similar question: How to apply a macro to each column of a table –  Martin Scharrer Mar 28 '11 at 11:13

You can use the collcell package to feed the cell content to a macro:

\usepackage{collcell}

\newcolumntype{U}{>{\collectcell\macro}{l}<{\endcollectcell}}


Then \macro will receive the cell content as only argument, i.e. #1.

If the macro should be simply \emph or \textsc you can use the "switch" versions directly. For \emph it would be \em and for \textsc it is \scshape. See e.g. http://www.cl.cam.ac.uk/~rf10/pstex/latexcommands.htm for a table of such macros.

-
collcell is at the moment too new for our uses, unfortunately. –  Debilski Mar 28 '11 at 10:12
@Debilski: You can easily install it locally if your distribution doesn't provide it. See also my updated answer. –  Martin Scharrer Mar 28 '11 at 10:25
Bad thing is, I need to use \lowercase which has no ‘switch’ version. –  Debilski Mar 28 '11 at 10:33
@Debilski: with collcell, you can use any macro, and \lowercase as well. –  Bruno Le Floch Mar 28 '11 at 11:05
@Debilski: If you don't want/can't use collcell you can use a macro like in TH. answer, but it wont work in the last column. See also the very similar question How to apply a macro to each column of a table for alternatives like using \halign directly. –  Martin Scharrer Mar 28 '11 at 11:16

Define a macro like

\def\switchon#1\switchoff{\foo{#1}}


perhaps.

-
I’m a bit ashamed of myself now, because I’ve already had this in the code but because a spurious \textnormal confused me, I did not notice it. Anyway, is it possible to have that without the explicit \switchoff? –  Debilski Mar 28 '11 at 9:48
This wont work in the last column of array because the \switchoff is "hidden" by the \\. –  Martin Scharrer Mar 28 '11 at 9:49
@Debilski: You need the \switchoff to mark the end of the argument. But you can hide it like the collcell commands in a \newcolumntype: \newcolumntype{V}{>{\switchon}l<{\switchoff}} –  Ulrike Fischer Mar 28 '11 at 11:55
@Martin: Good point! –  TH. Mar 29 '11 at 17:34