Text FormattingExplanation for new Column Type Syntax

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
InquisitorMo
Posts: 44
Joined: Mon Jul 15, 2013 12:22 am

Explanation for new Column Type Syntax

Post by InquisitorMo »

Chugging along in LaTeX. I've seen two different forms of the \newcolumtype command, and I'm quite confused as to the syntactic construction of these commands - I'd like to know a step-by-step breakdown of the command?

I've seen/used for instance, both of these, and I'm not really sure what the difference is, or why I would use one or the other, or indeed, what's going on in the second command.
  • Command 1: Here I basically get what's happening - a new column is defined with the name R - the > sign indicates to apply the parameter before the first cell, in this case a ragged left margin, the \arraybackslash is just required to cancel out the backslash as many Windows users are probably familiar with, and then I believe the X is indicating the width of the column should be based on the text within it.

    Code: Select all

    \newcolumntype{R}{>{\raggedleft\arraybackslash}X}
  • Command 2: By contrast, these types of multi-parameter commands I'm confused, because there is a [1] and {#1} added, plus there are additional commands, which simply are written one after the other with backslashes (\) with no spaces - just "command{parameter}\command{parameter}".

    I do understand the flipped caret < indicating to perform that action after the cell has been dealt with.
  • [code]\newcolumntype{I}[1]{>{\begin{SomeEnvironment}\raggedright\arraybackslash}p{#1}<{\end{SomeEnvironment}}}%[/code]
I looked at some documentation about new column type, and while it mentions some of this, it just was not explicit about what each parameter in the command means, when you would use one or the other, and didn't give a step-by-step breakdown of it. I believe this may have something to do with arrays as well.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Explanation for new Column Type Syntax

Post by cgnieder »

Well, as you've noticed \newcolumntype defines new columns. Those columns may get arguments. The number of arguments is given as optional argument to \newcolumntype and the arguments themselves are referred to with #1, #2 and so on, just like with \newcommand.

Inside the definitions one usually uses an existing column type and that's where probably you're confusion stems from:

Code: Select all

\newcolumntype{R}{>{\raggedleft\arraybackslash}X}
The column type R is equivalent to using >{\raggedleft\arraybackslash}X, i.e., an X-type column (from tabularx or tabu probably) where \raggedleft\arraybackslash is inserted before each cell of this X column.

Code: Select all

\newcolumntype{I}[1]{%
  >{\begin{SomeEnvironment}\raggedright\arraybackslash}
  p{#1}
  <{\end{SomeEnvironment}}
}%
This creates a column type I{} with one mandatory argument. This argument is passed on to a p{}-type column that also has a mandatory argument (the width of the column). Each cell in this p{} column is both preceded and followed by some stuff.

Regards
site moderator & package author
InquisitorMo
Posts: 44
Joined: Mon Jul 15, 2013 12:22 am

Re: Explanation for new Column Type Syntax

Post by InquisitorMo »

Thanks for your reply.

Ok so are you saying that "R" is a pre-defined system-type of column (like "X" is?) and so it's protected - as in, it's already a type of column, so you can't actually define a new type of column as "R" (just like, I assume, you can define a new column name as "X" for the same reason?)
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Explanation for new Column Type Syntax

Post by cgnieder »

InquisitorMo wrote:Ok so are you saying that "R" is a pre-defined system-type of column (like "X" is?) and so it's protected - as in, it's already a type of column, so you can't actually define a new type of column as "R" (just like, I assume, you can define a new column name as "X" for the same reason?)
No, the code

Code: Select all

\newcolumntype{R}{>{\raggedleft\arraybackslash}X}
defines the new column type R and it is using an existing column type X. In standard LaTeX and also using the array package X is not defined but there are packages that do like tabularx.

The p{} column is defined by the array package.

Regards
site moderator & package author
InquisitorMo
Posts: 44
Joined: Mon Jul 15, 2013 12:22 am

Re: Explanation for new Column Type Syntax

Post by InquisitorMo »

Ahh... ok I think I understand.

So INSTEAD OF USING an existing column type (like X as in the first example), one can redefine that by using the more complicated code with the <> as in the second example, so you can specify the width, and add other parameters, such as where a new environment is used.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Explanation for new Column Type Syntax

Post by cgnieder »

I'll give an example. Instead of

Code: Select all

\documentclass{article}
\usepackage{array}
\usepackage{tabularx}
\usepackage{lipsum}% dummy text
\begin{document}
\noindent
\begin{tabularx}{\textwidth}{*{2}{>{\raggedright\arraybackslash}X}}
 \lipsum[1] & \lipsum[2] 
\end{tabularx}
\end{document}
you can say

Code: Select all

\documentclass{article}
\usepackage{array}
\usepackage{tabularx}
\newcolumntype{R}{>{\raggedright\arraybackslash}X}
\usepackage{lipsum}% dummy text
\begin{document}
\noindent
\begin{tabularx}{\textwidth}{*{2}{R}}
 \lipsum[1] & \lipsum[2] 
\end{tabularx}
\end{document}
Regards
site moderator & package author
Post Reply