Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am trying to put part of a header of a table on a new line.

This is my code:

\documentclass[a4paper,11pt]{article}
\begin{document}
\title{}
\author{My name}
\date{\today}
\maketitle

\section{}


\begin{table}[!htbp] \centering 
  \caption{This is a table} 
  \label{} 
\begin{tabular}{@{\extracolsep{5pt}} lll} 
\\[-1.8ex]\hline 
\hline \\[-1.8ex]
\textbf{Rownames} & \textbf{Quite a long}\\\textbf{header} &  \textbf{Short header} \\ 
\hline \\[-1.8ex] 
value & $1.330$ & $1.080$ \\ 
value & $0.440$ & $0.360$ \\ 
value & $0.730$ & $$-$0.380$ \\ 
\normalsize 
\end{tabular} 
\end{table} 


\end{document}

This is output:

enter image description here

The output table I want should look like:

enter image description here

Note how in my desired output, only the word "header" of "Quite a long header" is on a new line. How can I achieve this table?

share|improve this question
    
If you pay attention to Mico's answer, his way is more robust. You may want to consider reading it. –  azetina Jan 10 at 21:52
add comment

4 Answers

up vote 5 down vote accepted

We adjust the column type to p{<length>} so that we can introduce paragraphs and call the array package to use the option >{\raggedright}. Here is the change in the code:

\begin{tabular}{@{\extracolsep{5pt}} l>{\raggedright}p{8em}l} 

enter image description here

Here is a MWE:

\documentclass[a4paper,11pt]{article}
\usepackage{array}
\begin{document}
\title{}
\author{My name}
\date{\today}
\maketitle

\section{}


\begin{table}[!htbp] \centering 
  \caption{This is a table} 
  \label{} 
\begin{tabular}{@{\extracolsep{5pt}} l>{\raggedright}p{8em}l} 
\\[-1.8ex]\hline 
\hline \\[-1.8ex]
\textbf{Rownames} & \textbf{Quite a long header} &  \textbf{Short header} \\ 
\hline \\[-1.8ex] 
value & $1.330$ & $1.080$ \\ 
value & $0.440$ & $0.360$ \\ 
value & $0.730$ & $-0.380$ \\ 
\normalsize 
\end{tabular} 
\end{table} 


\end{document}
share|improve this answer
1  
If you replace $$-$0.380$ with $-0.380$, LaTeX will use the typographically correct "minus" symbol instead of the "hyphen-dash" symbol. –  Mico Jan 10 at 21:41
    
@Mico Sorry I was just addressing the issue of the table headers. –  azetina Jan 10 at 21:47
add comment

You may want to do the following:

  • Use a tabularx environment instead of a tabular environment, and use the X column type for the headers of columns 2 and 3. In the example below I've chosen the X column type for the first row as well, but depending on the "real" contents of that column you may prefer to set its type to l. I came up with a total width of 9cm for the tabularx environment; depending on the size and other characteristics of the font you use, you may need to change that width a bit.

  • Use the dcolumn package for the numbers in columns 2 and 3, so that the numbers are aligned on their decimal points; the contents of a column of type D will by typeset in math mode automatically, hence no need to encase the numbers in pairs of $ symbols.

  • Use the booktabs package and its commands \toprule, \midrule, and \bottomrule instead of the basic \hline command.

enter image description here

\documentclass[a4paper,11pt]{article}
\usepackage{tabularx,dcolumn,booktabs}
\newcolumntype{Y}{>{\raggedright\arraybackslash}X}
\begin{document}
\begin{table}[!h] 
\centering 
  \caption{This is a table} \label{} 

\smallskip
\begin{tabularx}{9cm}{X*{2}{D{.}{.}{2.3}}}
\toprule
\textbf{Rownames} & 
\multicolumn{1}{Y}{\textbf{Quite a long header}} &  
\multicolumn{1}{Y}{\textbf{Short header}} \\ 
\midrule
value & 1.330 & 1.080 \\ 
value & 0.440 & 0.360 \\ 
value & 0.730 & -0.380 \\ 
\bottomrule
\end{tabularx} 
\end{table} 
\end{document}

Addendum: If, in addition, you wanted the numerical entries right-aligned and automatically rounded to two places after the decimal marker (both of these seem to be indicated from the sample table you've posted), I would recommend that you also load the siunitx package and provide suitable options to the package's S column type.

enter image description here

\documentclass[a4paper,11pt]{article}
\usepackage{tabularx,booktabs,siunitx}
\sisetup{round-mode=places,
         round-precision=2,
         table-format=2.2,
         table-number-alignment=right}
\newcolumntype{Y}{>{\raggedright\arraybackslash}X}
\begin{document}
\begin{table}[!h] 
\centering 
  \caption{This is another table} \label{} 
\smallskip
\begin{tabularx}{9cm}{ X *{2}{S} }
\toprule
\textbf{Rownames} & 
\multicolumn{1}{Y}{\textbf{Quite a long header}} &  
\multicolumn{1}{Y}{\textbf{Short header}} \\ 
\midrule
value & 1.330 & 1.080 \\ 
value & 0.440 & 0.360 \\ 
value & 0.730 & -0.380 \\ 
\bottomrule
\end{tabularx} 
\end{table} 
\end{document}
share|improve this answer
add comment

If you want to manually provide the line-breaking, you can use a macro like \Stack and friends:

enter image description here

\documentclass{article}

\usepackage{booktabs}% http://ctan.org/pkg/booktabs

\newcommand{\Stack}[3][]{#1\begin{tabular}[t]{@{}#2@{}}#3\end{tabular}}
\newcommand{\StackR}[2][]{\Stack[#1]{r}{#2}}
\newcommand{\StackL}[2][]{\Stack[#1]{l}{#2}}
\newcommand{\StackC}[2][]{\Stack[#1]{c}{#2}}

\begin{document}

\begin{tabular}{lrr} 
  \toprule
  \textbf{Rownames} & \StackC[\bfseries]{Quite a long \\ header} & \textbf{Short header} \\
  \midrule
  value & $1.330$ & $1.080$ \\ 
  value & $0.440$ & $0.360$ \\ 
  value & $0.730$ & $-0.380$ \\
  \bottomrule
\end{tabular}

\end{document}

The makecell package provides a similarly-named \makecell macro that does... something similar.

share|improve this answer
add comment

Here is a way to do things with makecell and booktabs:

\documentclass[a4paper, 11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{makecell, booktabs}

\begin{document}
\title{}
\author{My name}
\date{\today}
\maketitle

\renewcommand{\theadfont}{\bfseries}
\section{}

\begin{table}[!htbp] \centering
\caption{This is a table}
\label{}\sffamily
\begin{tabular}{ l@{\qquad} r @{\qquad} r}
\addlinespace
\toprule
\toprule
\thead{Rownames} & \thead{Quite a long\\ header} &  \thead{Short header} \\
\midrule
value & 1.330 &  1.080 \\
value & 0.340 &  0.360   \\
value & 0.730 & $ - $0.380 \\
\bottomrule
\normalsize
\end{tabular} 
\end{table}

\end{document} 

Result:

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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