2

Thanks to various threads on this site I have almost managed to construct a readable table! In particular I have added \newcolumntype to allow me to insert \newline into my tables, and I have constructed a table heading format \head to allow me to easily use a different format for the heading cells of the table.

I am slightly confused by the behaviour of my head format. The headings seem to align as desired only when I use the \newcolumntype to define my columns (i.e. L or R rather than l or r). Example below.

\documentclass[11pt, a4paper, twoside, fleqn]{article}

\usepackage{xspace}

\usepackage{array}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcommand{\head}[1]{\centering\textbf{#1}}

\newcommand{\COtwo}{CO$_{2}$\xspace}

\begin{document}

\begin{table}
  \centering 
  \footnotesize
  \begin{tabular}{ | R{2cm} | r | r | R{2cm} | }
  \hline
  \head{\COtwo} & \head{\COtwo} & \head{Ozone} & \head{Water\newline Vapour} \tabularnewline [0.5ex]
  \hline
  1.2 & 1.55555555 & 2.44444444444444444 & 1.3\\ 
  \hline
  \end{tabular}
  \caption{Blah} 
\end{table}

\end{document}

Ends up looking like this:

screenshot of my example code

I can't see why this should be - why can't my \head command override the default column alignment when using r? It's not such a big deal to use R instead of r, but I am curious as I am relatively new to LaTeX.

1 Answer 1

3

This is just a workaround to avoid using R when you want to use r.

Use

\multicolumn{1}{c|}{\head{\COtwo}}

in the cell where you want the contents centered.

MWE

\documentclass[11pt, a4paper, twoside, fleqn]{article}

\usepackage{xspace}

\usepackage{array}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcommand{\head}[1]{\centering\textbf{#1}}

\newcommand{\COtwo}{CO$_{2}$\xspace}

\begin{document}

\begin{table}
  \centering
  \footnotesize
  \begin{tabular}{ | R{2cm} | r | R{2cm} | R{2cm} | }
  \hline
  \head{\COtwo} & \multicolumn{1}{c|}{\head{\COtwo}} & \head{Ozone} & \head{Water\newline Vapour} \tabularnewline
  \hline
  1.2  & 1.55555555  &   2.4   &     1.3\\
  \hline
  \end{tabular}
  \caption{Blah}
\end{table}

\end{document} 

Output:

enter image description here

3
  • you're totally right, I didn't spot the problem in the normal text cells because the text tends to be longer and thus the header defines the column width so they look centred even when not. I had used this answer to help me with my headings but removed the multicolumn bit because it seemed to end up making my vertical lines bold too and overriding double verticals ||. I'll have another go, it looks from your answer that perhaps the bold and the \centercell would be better the other way round. Thanks.
    – FionaSmith
    Oct 15, 2013 at 15:20
  • PS: I don't know what the etiquette is: can I edit the original question to remove the math-mode "red-herring"?
    – FionaSmith
    Oct 15, 2013 at 15:21
  • Yes, you can edit the question to remove that. I'll edit my answer accordingly after that. Oct 15, 2013 at 15:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .