4

I have a created a command with an option, such as (just an example):

\newcommand{\form}[1]{\textcolor{blue}{\textit{#1}}}

I want to apply this format to a full column in a tabular environment. With a simple color format I would usually do as follow :

\begin{tabular}{|>{\color{red}}c|c|}

How can I do something similar with my "form" command. I tried the following codes without any results:

\begin{tabular}{|>{\form{}}c|c|}   %compiles with no changes
\begin{tabular}{|>{\form}c|c|}     %compiles with no changes
\begin{tabular}{|>{\form{#1}}c|c|} %does not compile

Thanks

  • Would it help if you define a new column type like \usepackage{array} \newcolumntype{C}{>{\color{red}\itshape}c} and use C instead of c in your table? – Arash Esbati Mar 8 '18 at 9:39
4

In this particular case, >{\color{red}\itshape}c is sufficient, but for more general situations you can use collcell:

\documentclass{article}
\usepackage{xcolor}
\usepackage{array}
\usepackage{collcell}

\newcommand{\form}[1]{\textcolor{red}{\textit{#1}}}

\begin{document}

\begin{tabular}{
  >{\collectcell\form}c<{\endcollectcell} % red italic
  >{\color{red}\itshape}c % red italic as well
}
abc & defghi \\
defghi & abc
\end{tabular}

\end{document}

enter image description here

  • @Mico , thanks to both of you, it works perfectly using the collectcell call. My actual form command is much more complex, that's why i need a command with an argument. Cheers. – Thomas Lesgourgues Mar 12 '18 at 10:09
1

The expression \begin{tabular}{|>{\form{#1}}c|c|} can't work because LaTeX is expecting not a formal argument ("#1") but a specific argument, say, "blurb". For instance, \begin{tabular}{|>{\form{blurb}}c|c|} works fine.

A full MWE (minimum working example):

\documentclass{article}
\usepackage{array,xcolor}
\newcommand{\form}[1]{\textcolor{blue}{\textit{#1}}}
\begin{document}

\begin{tabular}{|>{\form{blurb}}c|c|}
aaa & bbb \\
ccc & ddd
\end{tabular} 
\end{document}

Every cell in the first column of the table will receive the prefix "blurb", in blue and italics.


If, however, the objective is to make all cells in the first column blue and italic, it suffices to define \form as follows:

\newcommand{\form}{\color{blue}\itshape}

and to start the tabular environment as follows: \begin{tabular}{|>{\form}c|c|}.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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