33

I'm a StackExchange and LaTeX newb trying to figure out tables and tabularx. Sometimes I copy code but don't actually know what I'm doing or why it works or fails.

Can someone explain to me how the \newcolumntype command works and what this one is doing? I got it from the official documentation for tabularx. Especially the ">" thing, which I don't understand.

Example: \newcolumntype{Y}{>{\small\raggedright\arraybackslash}X}

  • 1
    Everything included in >{} is prepended to all the cells in the column Y. More info can be found in the array documentation. – Johannes_B Jul 26 '15 at 19:04
  • 1
    run texdoc array and read page 2 – user2478 Jul 26 '15 at 19:54
  • As Herbert said, page 2 for the >{}, {}< syntax, and page 3 for \newcolumntype. – Gonzalo Medina Jul 26 '15 at 23:41
30

The array package documentation, on pages 2 and 3 has the explanations:

enter image description here

enter image description here

enter image description here

enter image description here

Additional explanation

To answer your specific question:

\newcolumntype{Y}{>{\small\raggedright\arraybackslash}X}

defines a new type of column called Y based on a X column (this column type is defined by the tabularx package and it is basically a p{ <width>} column, where <width> is calculated by the package) but typesets the content using \small font size and with ragged-right text.

To sum up, Y is now a p{<width>} column whose contents will be typeset in \small font size and ragged-right. And now, instead of the cumbersome

\begin{tabularx}{0.5\linewidth}{c>{\small\raggedright\arraybackslash}Xl>{\small\raggedright\arraybackslash}X}
 ...
\end{tabularx}

you can simply say

\begin{tabularx}{0.5\linewidth}{cYlY}
 ...
\end{tabularx}

\newcolumntype with arguments

Let's say, as an example, that we need a table with three p{} type columns with the following specifications:

  • First column: width=4cm, color=(shade of) blue; ragged-left content.
  • Second column: width=3cm, color=(shade of) red; ragged-left content.
  • First column: width=5cm, color=(shade of) green; ragged-left content.

The best thing here is to define a new column type; \newcolumntype also receives parameters (like \newcommand), so in this case we need a new type with two parameters: color and width:

\newcolumntype{C}[2]{>{\columncolor{#1}\raggedleft\arraybackslash}p{#2}}

And the table format would look like

\begin{tabular}{C{red!30}{4cm}C{blue!20}{3cm}C{green!20}{5cm}}
...
\end{tabular}

The complete example:

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{array}

\newcolumntype{C}[2]{>{\columncolor{#1}\raggedleft}p{#2}}

\begin{document}

\noindent
\begin{tabular}{C{red!30}{4cm}C{blue!20}{3cm}C{green!20}{5cm}}
Some test text to illustrate the new column type 
  & Some test text to illustrate the new column type 
  & Some test text to illustrate the new column type 
\end{tabular}

\end{document}

The result:

enter image description here

\arraybackslash

As for \arraybackslash, the \raggedright, \raggedleft, and \centering declarations redefine \\ in a way which conflicts with its use in a tabular or array environments. The command \arraybackslash (implemented in array and tabularx) restores the meaning of \\ for use in array and tabular (you would only need \arraybackslash for the last column).

  • 1
    How to define a new type that combine the features of math-mode and auto line break? I have tried \newcolumntype{Y}{>{$}X<{$}}, but it will not work. – Eden Harder Oct 4 '16 at 16:57

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.