8

I have a table defined as follows where the first column is right-justified:

\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{array}
\begin{document}
\begin{table}
\centering
\begin{tabular}{>{\hfill}p{3.50cm}p{5.50cm}p{5.50cm}}
\hline\noalign{\smallskip}
\textbf{Column 1} & \textbf{Column 2} & \textbf{Column 3} \\ [0.5ex]
\hline\noalign{\smallskip}
1 & text & text \\
2 & text & text \\
\hline\noalign{\smallskip}
\end{tabular}
\caption{thisTable.}
\label{tab:thisTable}
\end{table}
\end{document}

However, the column heading of the first column is also right-justified.

Can I change the column heading to be left-justified but leave the values contents of the data part of the column right-aligned?

  • \hfill does not right justify a paragraph, it just distorts the first line. use \raggedright or \raggedleft or in the final column use \raggedright\arraybackslash – David Carlisle Sep 5 '14 at 21:17
  • for the heading use \multicolumn{1}{c}{\bfseries column 1} {or l) – David Carlisle Sep 5 '14 at 21:18
  • Sorry, I'm not good with LaTex tables. How precisely do I implement what you are suggesting? – Mr Morgan Sep 5 '14 at 21:21
  • Your code was incomplete and contained a couple of errors. I've taken the liberty of fixing it up so that it's compilable. – Mico Sep 5 '14 at 21:21
  • Thanks Mico. I see your changes but how can I get the title of the first column to left-align? – Mr Morgan Sep 5 '14 at 21:26
14

Can I change the column heading to be left-justified but leave the values contents of the data part of the column right-aligned?

Yes. Just place the cell's contents in a \multicolumn{1}{l}{...} "wrapper".

Incidentally, instead of using the somewhat kludgy \hline\noalign{\smallskip} directives, I suggest you load the booktabs package and use its macros \toprule, \midrule, and \botttomrule to get well-spaced horizontal lines.

And, instead of the >{\hfill}p{3.50cm} specification of the left-most column, I suggest you load the array and ragged2e packages and define a raggedleft (flushright) column type that takes a width parameter, as is done in the example below.

enter image description here

\documentclass{article}
\usepackage[margin=1in,a4paper]{geometry}  % choose margins appropriately
\usepackage{array,booktabs,ragged2e}
\newcolumntype{R}[1]{>{\RaggedLeft\arraybackslash}p{#1}}
\begin{document}
\begin{table}
\centering
\begin{tabular}{ R{3.50cm} p{5.50cm} p{5.50cm} }
\toprule
\multicolumn{1}{l}{\textbf{Column 1}} & \textbf{Column 2} & \textbf{Column 3} \\ 
\midrule
1 & text & text \\
2 & text & text \\
\bottomrule
\end{tabular}
\caption{thisTable.}
\label{tab:thisTable}
\end{table}
\end{document}

Addendum to address the OP's follow-up question: If the cell in question has a lot of text that needs to span several lines, you can still use \multicolumn, but not with the l column type specifier. E.g., you might type

\newcolumntype{L}[1]{>{\RaggedRight\arraybackslash}p{#1}}

in the preamble and then write

\multicolumn{1}{L{3.50cm}}{An incredibly long header that occupies several lines.}

instead of the earlier

\multicolumn{1}{l}{\textbf{Column 1}}

In short, \multicolumn is a very powerful and flexible tool.

  • Thanks Mico. I'm just trying to get the relevant .sty files now. – Mr Morgan Sep 5 '14 at 21:40
  • Just one issue that has arisen: if the text of the first column heading is quite long, I notice that it doesn't wrap. Can this be done? – Mr Morgan Sep 5 '14 at 22:08
  • 1
    @MrMorgan If you want multiline then you don't want \multicolumn just \raggedright Column1 will work (or \RaggedRight ) – David Carlisle Sep 5 '14 at 22:11
  • @MrMorgan - I've provided an addendum to address your follow-up question. – Mico Sep 6 '14 at 2:43
  • Works great. But how to ensure a vertical alignment of the text in the cells above? My text is on the bottom as other cells grow in height. – delrocco Jan 16 at 2:15
2

A variation on @Mico's solution, that uses the makecell package: it's designed to have a common formatting for columnheads in one command (\thead), thus saving some typing and, most importantly in my opinion, it's better semantically. I also allowed myself to put the caption on top, as is the typographical norm for tables and added the caption package so as to ensure a correct vertical spacing between caption and table.

\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{array, booktabs, caption}
\usepackage{ragged2e}
\usepackage{makecell}
\renewcommand\theadalign{lc}
\renewcommand\theadfont{\bfseries}

\begin{document}
\begin{table}
\centering
\caption{thisTable.}
\label{tab:thisTable}
\begin{tabular}{>{\RaggedLeft}p{3.50cm}p{5.50cm}p{5.50cm}}
\toprule
\thead{Column 1} & \thead{Column 2} & \thead{Column 3} \\
\midrule
1 & text & text \\[0.5ex]
2 & text & text \\
\bottomrule
\end{tabular}
\end{table}
\end{document} 

enter image description here

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.