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 have a long table spread over multiple pages that I code using the package longtable package. A sample of my code is this:

\documentclass[a4paper]{article}
\usepackage{longtable}

\begin{document}

\begin{longtable}{cccccccp{5cm}}   \hline
 Row & GO term & P-value & OR & Exp Count & Count & Size 
 \\  \hline
1 & GO:0009987 & 0.00 & 1.65 & 1811.17 & 2078 & 11765 & cellular process \\ 
2 & GO:0050794 & 0.00 & 1.57 & 648.88 & 857 & 4215 & regulation of cellular process \\ 
3 & GO:0080090 & 0.00 & 1.67 & 376.24 & 536 & 2444 & regulation of primary metabolic process \\ 
4 & GO:0009889 & 0.00 & 1.69 & 344.84 & 497 & 2240 & regulation of biosynthetic process \\ 
5 & GO:0051252 & 0.00 & 1.72 & 308.20 & 453 & 2002 & regulation of RNA metabolic process \\ 
6 & GO:0006355 & 0.00 & 1.71 & 306.35 & 449 & 1990 & regulation of transcription, DNA-dependent \\ 
7 & GO:2001141 & 0.00 & 1.71 & 306.35 & 449 & 1990 & regulation of RNA biosynthetic process \\ 
8 & GO:0031323 & 0.00 & 1.63 & 378.24 & 531 & 2457 & regulation of cellular metabolic process \\ 
9 & GO:0051171 & 0.00 & 1.66 & 332.52 & 476 & 2160 & regulation of nitrogen compound metabolic process \\ 
10 & GO:0019219 & 0.00 & 1.66 & 328.52 & 469 & 2134 & regulation of nucleobase-containing compound metabolic process \\ 
   \hline
\end{longtable}
\end{document}

However, the printed table is shifted to the right, which bothers me. How can I fix this? I tried adding \begin{center} and centering but this did not work.

share|improve this question
    
Have a look at the TeX FAQ how to change margins on the fly. Also, have a look at David Carlisle answer to a similar question. –  Sveinung Aug 28 at 18:26
    
Or Center wide longtable –  Symbol 1 Aug 28 at 18:35

4 Answers 4

up vote 5 down vote accepted

It's not the case that the table is "shifted" to the right in the MWE. Instead, the MWE's table is wider than \textwidth, i..e, the width of the text block. This makes its material stick out to the right of the text block.

Assuming the document's left-hand and right-hand margins are actually 1 inch (2.54 cm) wide each, you can get the longtable environment to (just!) fit inside the textblock if you also lop off the whitespace to the left of the first column and to the right of the final column, i.e., if you specify

\usepackage[margin=1in]{geometry}

in the preamble and

\begin{longtable}{@{} *{7}{c} p{5cm} @{}}

for the longtable itself.

With this setup, however, the text material in the p column will be typeset fully justified, causing occasional very large interword gaps as the column is only 5 cm wide. With such narrow columns, it's preferable to typeset the material ragged-right rather than fully justified. I thus suggest you provide the following commands in the preamble:

\usepackage{longtable}
\usepackage{array}     % for "\newcolumntype" macro
\usepackage{ragged2e}  % for "\RaggedRight" macro
\newcolumntype{P}[1]{>{\RaggedRight\arraybackslash}p{#1}}
\usepackage[margin=1in]{geometry}

and then set up the longtable environment as:

\begin{longtable}{@{} *{7}{c} P{5cm} @{}}

With the P column type, you'll succeed in getting that column's material typeset ragged-right, while still allowing hyphenation. The simple \raggedright macro disables hyphenation; that's why I recommend using \RaggedRight in the definition of the P column type.

Here's the output of the code that implements these suggestions. (The black frame lines on the left and right are drawn because I set the showframe option of the geometry package.)

enter image description here

\documentclass[a4paper]{article}
\usepackage[showframe,margin=1in]{geometry}
\usepackage{longtable}
\usepackage{array}
\usepackage{ragged2e}
\newcolumntype{P}[1]{>{\RaggedRight\arraybackslash}p{#1}}
\begin{document}
\begin{longtable}{@{} *{7}{c} P{5cm} @{}}   
\hline
Row & GO term & P-value & OR & Exp Count & Count & Size  \\  
\hline
\endhead
\hline
\endfoot
1 & GO:0009987 & 0.00 & 1.65 & 1811.17 & 2078 & 11765 & cellular process \\ 
2 & GO:0050794 & 0.00 & 1.57 & 648.88 & 857 & 4215 & regulation of cellular process \\ 
3 & GO:0080090 & 0.00 & 1.67 & 376.24 & 536 & 2444 & regulation of primary metabolic process \\ 
4 & GO:0009889 & 0.00 & 1.69 & 344.84 & 497 & 2240 & regulation of biosynthetic process \\ 
5 & GO:0051252 & 0.00 & 1.72 & 308.20 & 453 & 2002 & regulation of RNA metabolic process \\ 
6 & GO:0006355 & 0.00 & 1.71 & 306.35 & 449 & 1990 & regulation of transcription, DNA-dependent \\ 
7 & GO:2001141 & 0.00 & 1.71 & 306.35 & 449 & 1990 & regulation of RNA biosynthetic process \\ 
8 & GO:0031323 & 0.00 & 1.63 & 378.24 & 531 & 2457 & regulation of cellular metabolic process \\ 
9 & GO:0051171 & 0.00 & 1.66 & 332.52 & 476 & 2160 & regulation of nitrogen compound metabolic process \\ 
10 & GO:0019219 & 0.00 & 1.66 & 328.52 & 469 & 2134 & regulation of nucleobase-containing compound metabolic process \\ 
\end{longtable}
\end{document}
share|improve this answer
    
This solved the question. Simply specifying the margins was sufficient to get the table as required. Many thanks! –  KoenVdB Aug 28 at 19:17
    
@KoenVdB - Glad the suggestions worked for you. :-) –  Mico Aug 28 at 19:25

A solution based on tabularx. I also use the array package, for its *{n}{} and >{…} constructs that saves typing, and booktabsto improve the look of the table. The siunitx package is used to align columns of numbers. Finally, I've automated row numbering and the GO: label.

\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
 \usepackage{geometry}
\usepackage{array, booktabs, siunitx}
\sisetup{table-number-alignment = center, parse-numbers = false}

\usepackage{ltablex}
\usepackage{ragged2e} 
\renewcommand{\tabularxcolumn}[1]{>{\RaggedRight\small\arraybackslash}m{#1}}

\newcounter{rowcnt}

\begin{document}
\mbox{}\vskip 2cm
    \setlength\tabcolsep{3pt}\renewcommand\arraystretch{1.25}\setcounter{rowcnt}{0}
    \begin{tabularx}{\linewidth}{>{\refstepcounter{rowcnt}}c >{GO:}cccS[table-format=4.2]S[table-format=4.0]S[table-format=5.0]X}%
    \toprule
     \multicolumn{1}{c}{Row} & \multicolumn{1}{c}{GO term} & P-value & OR & {Exp Count} & {Count} & {Size} \\
     \cmidrule[\lightrulewidth](l{3pt}r){1-8}\addlinespace
    1 & 0009987 & 0.00 & 1.65 & 1811.17 & 2078 & 11765 & cellular process \\
    2 & 0050794 & 0.00 & 1.57 & 648.88 & 857 & 4215 & regulation of cellular process \\
    3 & 0080090 & 0.00 & 1.67 & 376.24 & 536 & 2444 & regulation of primary metabolic process \\
    4 & 0009889 & 0.00 & 1.69 & 344.84 & 497 & 2240 & regulation of biosynthetic process \\
    5 & 0051252 & 0.00 & 1.72 & 308.20 & 453 & 2002 & regulation of RNA metabolic process \\
    6 & 0006355 & 0.00 & 1.71 & 306.35 & 449 & 1990 & regulation of transcription, DNA-dependent \\
    7 & 2001141 & 0.00 & 1.71 & 306.35 & 449 & 1990 & regulation of RNA biosynthetic process \\
    8 & 0031323 & 0.00 & 1.63 & 378.24 & 531 & 2457 & regulation of cellular metabolic process \\
    9 & 0051171 & 0.00 & 1.66 & 332.52 & 476 & 2160 & regulation of nitrogen compound metabolic process \\
    10 & 0019219 & 0.00 & 1.66 & 328.52 & 469 & 2134 & regulation of nucleobase-containing compound metabolic process \\
       \bottomrule
    \end{tabularx}

\end{document} 

enter image description here

share|improve this answer

The problem is that the table is wider than the textwidth. When you use \usepackage[showframe]{geometry} then you see the borders:

Screenshot

\documentclass[a4paper]{article}
\usepackage{longtable}
\usepackage[showframe]{geometry}


\begin{document}

\begin{longtable}{cccccccp{5cm}}   \hline
 Row & GO term & P-value & OR & Exp Count & Count & Size 
 \\  \hline
1 & GO:0009987 & 0.00 & 1.65 & 1811.17 & 2078 & 11765 & cellular process \\ 
2 & GO:0050794 & 0.00 & 1.57 & 648.88 & 857 & 4215 & regulation of cellular process \\ 
3 & GO:0080090 & 0.00 & 1.67 & 376.24 & 536 & 2444 & regulation of primary metabolic process \\ 
4 & GO:0009889 & 0.00 & 1.69 & 344.84 & 497 & 2240 & regulation of biosynthetic process \\ 
5 & GO:0051252 & 0.00 & 1.72 & 308.20 & 453 & 2002 & regulation of RNA metabolic process \\ 
6 & GO:0006355 & 0.00 & 1.71 & 306.35 & 449 & 1990 & regulation of transcription, DNA-dependent \\ 
7 & GO:2001141 & 0.00 & 1.71 & 306.35 & 449 & 1990 & regulation of RNA biosynthetic process \\ 
   \hline
\end{longtable}

\end{document}

So you should make

  1. the table smaller or
  2. change the page layout (margins) - have a look on teh mentioned goemetry package.
share|improve this answer
    
You are right about that, but how exactly does this answer the question? –  Torbjørn T. Aug 28 at 13:08
    
I do not have much time atm. I think this helps better than no answer. I updated the answer a bit. –  Manuel Kuehner Aug 28 at 13:21
1  
Well, personally I think it would be better to have just posted a comment. –  Torbjørn T. Aug 28 at 13:24
    
I wanted to show the screenshot - that's why I chose the answer. Do you want me to delete the answer? –  Manuel Kuehner Aug 28 at 13:31
    
That's up to you. If you choose to delete it, you can always edit and undelete it later. –  Torbjørn T. Aug 28 at 13:45

As Manuel Kuehner mentioned, your table is wider than the \textwidth. You may either

  1. Enlarge \textwidth by simply \usepackage[cm]{fullpage} or, as mentioned, using geometry.
  2. Add a negative space manually on the left of the table, as following:

\documentclass[a4paper]{article}
\usepackage{longtable}
\begin{document}

\begin{longtable}{cccccccp{5cm}}   \hline
Row & GO term & P-value & OR & Exp Count & Count & Size  \\  \hline
1 & GO:0009987 & 0.00 & 1.65 & 1811.17 & 2078 & 11765 & cellular process
\end{longtable}

\hspace*{-3cm}\begin{minipage}{5cm}\begin{longtable}{cccccccp{5cm}}   \hline
 Row & GO term & P-value & OR & Exp Count & Count & Size  \\  \hline
1 & GO:0009987 & 0.00 & 1.65 & 1811.17 & 2078 & 11765 & cellular process
\end{longtable}\end{minipage}

\end{document}

enter image description here

share|improve this answer
1  
Putting a longtable in a minipage is not a good idea, or ... –  Sveinung Aug 28 at 18:11
    
No it is not. I am so stupid... (At least it works for this MWE) –  Symbol 1 Aug 28 at 18:32

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.